-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn900vnc
executable file
·85 lines (75 loc) · 2.74 KB
/
n900vnc
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
#!/usr/bin/python3
# n900vnc Copyright (C) 2011, 2012 Stuart Pook (http://www.pook.it/)
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from optparse import OptionParser
from random import SystemRandom
import subprocess
import time
import sys
remote_command = "x11vnc"
parser = OptionParser()
parser.add_option("-a", "--address", dest="address", default="172.16.47.3", help="address to connect to and listen on [%default]")
parser.add_option("-t", "--timeout", "-d", "--delay", metavar="TIMEOUT", type="int", dest="delay", default=3, help="how long the remote " + remote_command + " waits for a connection [%default]")
(options, args) = parser.parse_args()
rnd = SystemRandom()
passwd = bytearray()
for i in range(8):
passwd.append(rnd.randint(ord('!') , ord('z')))
passwd.append(ord(b'\n'))
tight = subprocess.Popen(["tightvncpasswd", "-f"], stdin= subprocess.PIPE, stdout= subprocess.PIPE, close_fds=True)
tight.stdin.write(passwd)
tight.stdin.close()
encoded = tight.stdout.read()
tight.stdout.close()
if tight.wait() != 0:
sys.exit("password encyption failed")
# should use "-allow" option to x11vnc
remote = subprocess.Popen(["ssh", "-xak", "-l", "user", options.address,
"exec", remote_command,
"-ncache", "0",
"-quiet",
"-timeout", str(options.delay),
"-nocmds",
"-cursor", "arrow",
"-safer",
"-display", ":0",
"-listen", options.address,
"-passwdfile", "/dev/stdin"
], close_fds=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
remote.stdin.write(passwd)
remote.stdin.close()
for l in iter(remote.stdout.readline, ''):
if not l:
sys.exit("eof reading connection to " + options.address)
prefix = b"PORT=59"
if l.find(prefix) == 0:
port = l[len(prefix):].strip().decode()
break
print(b">> " + l)
viewer = subprocess.Popen(["xvnc4viewer", "--PasswordFile=/dev/stdin", options.address + ":" + port], close_fds = True, stdin= subprocess.PIPE)
viewer.stdin.write(encoded)
viewer.stdin.close()
while True:
b = remote.stdout.read()
if not b:
break
print(b"}} " + b)
result = 0
r = viewer.wait()
if r != 0:
# print >> sys.stderr, "local viewer failed", r
# result = 1
pass
if remote.wait() != 0:
print("remote server failed", file=sys.stderr)
result = 1
sys.exit(result)