-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_utils.py
51 lines (36 loc) · 2.56 KB
/
run_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Utility commands for running stuff in a terminal."""
import subprocess
import shlex
import logging
logging.basicConfig(level=logging.DEBUG)
term_ssh = "xterm"
term_rsync = "xterm"
def run_term_cmd(cmd):
"""Run command locally, opening a local terminal."""
term_cmd = f"bash -c {shlex.quote(cmd)}"
logging.info(term_cmd)
return subprocess.check_output([term_ssh, "-geometry", "100x32", "-e", term_cmd])
def run_term_ssh_cmd(hostname, username, password, ssh_cmd):
"""Run command remotely, opening a local terminal."""
term_cmd = f"sshpass -p {shlex.quote(password)} ssh -q -t -oUserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no {shlex.quote(username)}@{shlex.quote(hostname)} {shlex.quote(ssh_cmd)} ; read -n 1 -p 'Command finished. Press any key to continue.'"
logging.info(term_cmd)
subprocess.Popen([term_ssh, "-geometry", "100x32", "-e", term_cmd])
def run_term_ssh(hostname, username, password, remote_path):
"""Run command remotely, opening a local terminal."""
cmd = f"cd {shlex.quote(remote_path)}; bash --login"
term_cmd = f"sshpass -p {shlex.quote(password)} ssh -q -t -oUserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no {shlex.quote(username)}@{shlex.quote(hostname)} {shlex.quote(cmd)}"
logging.info(term_cmd)
subprocess.Popen([term_ssh, "-geometry", "100x32", "-e", term_cmd])
def run_term_rsync_up(hostname, username, password, local_path, remote_path):
"""Run command remotely, opening a local terminal."""
term_cmd = f"sshpass -p {shlex.quote(password)} rsync -e 'ssh -q -oUserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' --stats --progress {shlex.quote(local_path)}/ -aXxv {shlex.quote(username)}@{shlex.quote(hostname)}:{shlex.quote(remote_path)} ; read -n 1 -p 'Command finished. Press any key to continue.'"
logging.info(term_cmd)
subprocess.Popen([term_rsync, "-geometry", "100x32", "-e", term_cmd])
def run_term_rsync_down(hostname, username, password, local_path, remote_path):
"""Run command remotely, opening a local terminal."""
term_cmd = f"sshpass -p {shlex.quote(password)} rsync -e 'ssh -q -oUserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' -aXxv --stats --progress {shlex.quote(username)}@{shlex.quote(hostname)}:{shlex.quote(remote_path)}/ {shlex.quote(local_path)} ; read -n 1 -p 'Command finished. Press any key to continue.'"
logging.info(term_cmd)
subprocess.Popen([term_rsync, "-geometry", "100x32", "-e", term_cmd])
def run_filebrowser(local_path):
"""Run command remotely, opening a local terminal."""
subprocess.Popen(["thunar", local_path])