-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_remote3_ssh.py
executable file
·130 lines (99 loc) · 3.81 KB
/
get_remote3_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
import requests
import httplib2
from urllib.request import urlopen
from json import dumps
import getpass
import json
from utils import *
import argparse
import os
jsl = os.environ['JSONLOGINS']
apiMethod="https://"
apiServer="api.remot3.it"
apiVersion= "/apv/v23.5"
def get_key(field='key'):
_log_d = filejson2dictionary(jsl+'/remot3.json')
developerKey="\""+_log_d[field]+"\""
return developerKey
def get_token():
developerkey=get_key()
developerkey=get_key()
pw = getpass.getpass()
#url = "https://api.remot3.it/apv/v23.5/user/login"
url = apiMethod + apiServer + apiVersion +"/user/login"
payload = "{ \"username\": " + get_key(field="username") + ", \"password\":\"" + pw + "\" }"
headers = {
'developerkey': developerkey,
'content-type': "application/json",
'cache-control': "no-cache"
}
#print(headers)
#
response = requests.request("POST", url, data=payload, headers=headers)
response_dict = json.loads(response.text)
print(response_dict['token'])
return response_dict['token']
def get_devices_list(token):
developerkey=get_key()
deviceListURL = apiMethod + apiServer + apiVersion + "/device/list/all"
content_type_header = "application/json"
deviceListHeaders = {
'Content-Type': content_type_header,
'developerkey': developerkey,
'token': token,
}
httplib2.debuglevel = 0
http = httplib2.Http()
response, content = http.request( deviceListURL,
'GET',
headers=deviceListHeaders)
dict_connected = json.loads(content)
return dict_connected
def proxyConnect(UID, token):
httplib2.debuglevel = 0
http = httplib2.Http()
content_type_header = "application/json"
# this is equivalent to "whatismyip.com"
# in the event your router or firewall reports a malware alert
# replace this expression with your external IP as given by
# whatismyip.com
#_log_d = filejson2dictionary('/Users/roberto/json_logins/remot3.json')
#developerKey="\""+_log_d['key']+"\""
developerKey=get_key()
my_ip = urlopen('http://ip.42.pl/raw').read()
print(my_ip)
proxyConnectURL = apiMethod + apiServer + apiVersion + "/device/connect"
proxyHeaders = {
'Content-Type': content_type_header,
'developerkey': developerKey,
'token': token
}
proxyBody = {
'deviceaddress': UID,
'hostip': str(my_ip,"utf8"),
'wait': "true"
}
response, content = http.request( proxyConnectURL,
'POST',
headers=proxyHeaders,
body=dumps(proxyBody),
)
try:
data = json.loads(content)["connection"]["proxy"]
fields = data.split(":")
host=fields[1].replace("/","")
print("ssh -l pi "+host+" -p "+fields[2])
except KeyError:
print("Key Error exception!")
print(content)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--piname", default="RPi-Roberto-SSH", help="Service name to be searches for")
args = parser.parse_args()
piname=args.piname
token = get_token()
dict_connected = get_devices_list(token)
print ([ d['devicealias']+' '+d['deviceaddress']for d in dict_connected["devices"] if d['devicestate'] == "active" ])
UIDs = [ d['deviceaddress'] for d in dict_connected["devices"] if d['devicestate'] == "active" and d['devicealias'] == piname ]
proxyConnect(str(UIDs[0]), token)