/
gsh
executable file
·88 lines (66 loc) · 2.39 KB
/
gsh
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
#
"""
gsh: Execute remote command
Example:
gsh tty1 pwd
gsh /local/tty[ab] ls
"""
from __future__ import absolute_import, print_function
import os
import sys
try:
import gterm
except ImportError:
import graphterm.bin.gterm as gterm
usage = "usage: %prog [-n] path command_name arg1 ..."
form_parser = gterm.FormParser(usage=usage, title="Send command input to terminal", command="gsh")
form_parser.add_argument(label="Terminal path: ", help="Terminal path [/host/]tty")
form_parser.add_argument(label="Command: ", help="Command to be executed on terminal")
form_parser.add_option("noline", False, short="n", help="Suppress newline appended after command")
form_parser.add_option("multiple", False, short="m", help="Multiple paths specified (command via stdin)")
form_parser.add_option("ctrlc", False, short="c", help="Interrupt")
form_parser.add_option("ctrld", False, short="d", help="End of file")
form_parser.add_option("self", False, short="s", help="Self included")
offset = 0
for arg in sys.argv[1:]:
if not arg.startswith("-"):
break
offset += 1
args = sys.argv[1:2+offset]
cmd_args = sys.argv[2+offset:]
(options, pargs) = form_parser.parse_args(args=args or None)
if options.multiple:
paths = pargs + cmd_args
cmd_args = []
else:
paths = [pargs[0]]
command = ""
if cmd_args:
command = " ".join(cmd_args)
if not options.noline:
command += "\n"
elif not options.ctrlc and not options.ctrld:
try:
command = sys.stdin.read()
except (EOFError, KeyboardInterrupt):
command = ""
if not command:
print("Error in reading from stdin", file=sys.stderr)
sys.exit(1)
if options.ctrld:
command = "\x04" + command
# NOTE: A leading space seems to be required on Linux after Ctrl-C, because the first character is chewed up
if options.ctrlc:
command = "\x03" + (" "+command if command else "")
for path in paths:
if "/" not in path:
path = "/" + gterm.env("PATH").split("/")[1] + "/" + path
elif not path.startswith("/"):
print("Invalid terminal path %s; must begin with /" % path, file=sys.stderr)
sys.exit(1)
params = {"include_self": "yes" if options.self else "", "path": path[1:], "command": command}
headers = {"x_gterm_response": "remote_command",
"x_gterm_parameters": params
}
gterm.wrap_write("", headers=headers)