-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_ssh.py
100 lines (63 loc) · 2.69 KB
/
reverse_ssh.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
import argparse
import thread
import subprocess
from socketIO_client import SocketIO, LoggingNamespace
def main():
global commandArgs
global robotID
global appServerSocketIO
parser = argparse.ArgumentParser(description='start robot control program')
parser.add_argument('robot_id')
parser.add_argument('--reverse-ssh-key-file', default='/home/pi/reverse_ssh_key1.pem')
parser.add_argument('--reverse-ssh-host', default='ubuntu@52.52.204.174')
commandArgs = parser.parse_args()
print commandArgs
robotID = commandArgs.robot_id
print "connecting to app server socket.io"
appServerSocketIO = SocketIO('letsrobot.tv', 8022, LoggingNamespace)
print "finished connecting to app server"
appServerSocketIO.on('connect', onHandleAppServerConnect)
appServerSocketIO.on('reconnect', onHandleAppServerReconnect)
appServerSocketIO.on('disconnect', onHandleAppServerDisconnect)
appServerSocketIO.on('reverse_ssh_8872381747239', startReverseSshProcess)
appServerSocketIO.on('end_reverse_ssh_8872381747239', endReverseSshProcess)
while True:
appServerSocketIO.wait(seconds=1)
def startReverseSshProcess(*args):
thread.start_new_thread(handleStartReverseSshProcess, args)
def endReverseSshProcess(*args):
thread.start_new_thread(handleEndReverseSshProcess, args)
def handleStartReverseSshProcess(args):
print "starting reverse ssh"
appServerSocketIO.emit("reverse_ssh_info", "starting")
returnCode = subprocess.call(["/usr/bin/ssh",
"-X",
"-i", commandArgs.reverse_ssh_key_file,
"-N",
"-R", "2222:localhost:22",
commandArgs.reverse_ssh_host])
appServerSocketIO.emit("reverse_ssh_info", "return code: " + str(returnCode))
print "reverse ssh process has exited with code", str(returnCode)
def handleEndReverseSshProcess(args):
print "handling end reverse ssh process"
resultCode = subprocess.call(["killall", "ssh"])
print "result code of killall ssh:", resultCode
def onHandleAppServerConnect(*args):
print
print "chat socket.io connect"
print
identifyRobotID()
def onHandleAppServerReconnect(*args):
print
print "app server socket.io reconnect"
print
identifyRobotID()
def onHandleAppServerDisconnect(*args):
print
print "app server socket.io disconnect"
print
def identifyRobotID():
"""tells the server which robot is using the connection"""
print "sending identify robot id messages"
appServerSocketIO.emit('identify_robot_id', robotID);
main()