forked from GDGVIT/ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsender.py
More file actions
97 lines (82 loc) · 3.03 KB
/
sender.py
File metadata and controls
97 lines (82 loc) · 3.03 KB
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
__author__ = 'Ujjwal'
import os
import subprocess
import socket
from scanner import scan_ports, __PORT__
try:
__USER__ = os.environ.copy()['SUDO_USER']
if __USER__ == 'root':
__USER__ = '../root'
except:
print('Run as sudo')
exit(0)
def send():
hosts = scan_ports()
while True:
print('ft> ', end='')
try:
cmd = input()
except:
print('')
exit(0)
if cmd == 'list':
print('-----HOSTS-----')
for i, host in enumerate(hosts, start=1):
print(str(i) + ': ' + str(host[0]) + '@' + host[1])
elif 'select' in cmd:
host = int(cmd.replace('select ', '')) - 1
if host >= len(hosts) or host < 0:
print('Invalid selection')
continue
else:
transfer(hosts[host][1])
elif cmd == 'refresh':
hosts = scan_ports()
elif cmd == 'quit':
exit(0)
else:
print('Invalid command')
def transfer(host):
file = input('Enter file path: ')
try:
port = 9999
s = socket.socket()
except socket.error as msg:
print("Socket creation error " + str(msg))
return
try:
s.connect((host, port))
except socket.error as msg:
print("socket connect error: " + str(msg) + "\n")
return
a = str(__USER__.lstrip('../')) + '@' + str(s.getsockname()[0]) + ' wants to connect with you'
s.send(str.encode(a))
try:
response = str(s.recv(1024), encoding='utf-8')
if response == '':
raise Exception
except:
print("Receiver declined")
return
subprocess.call(['ssh-keygen', '-t', 'rsa', '-q', '-f', '/home/' + __USER__ + '/.ssh/temp_id', '-N', ''])
public_key = subprocess.check_output(['cat', '/home/' + __USER__ + '/.ssh/temp_id.pub'])
try:
s.send(public_key)
uname = str(s.recv(256), encoding='utf-8')
if uname == 'root':
subprocess.call(['rsync', '-aHAXxv', '--append-verify', '--progress', '-e',
'ssh -p ' + str(__PORT__) + ' -T -c arcfour -o Compression=no -o StrictHostKeyChecking=no -x -i /home/' + __USER__ +
'/.ssh/temp_id', file, uname + '@' + str(host) + ':/root/Downloads/'])
else:
subprocess.call(['rsync', '-aHAXxv', '--append-verify', '--progress', '-e',
'ssh -p ' + str(__PORT__) + ' -T -c arcfour -o Compression=no -o StrictHostKeyChecking=no -x -i /home/' + __USER__ +
'/.ssh/temp_id', file, uname + '@' + str(host) + ':/home/' + uname + '/Downloads/'])
s.send(str.encode("close"))
os.remove('/home/' + __USER__ + '/.ssh/temp_id')
os.remove('/home/' + __USER__ + '/.ssh/temp_id.pub')
except:
os.remove('/home/' + __USER__ + '/.ssh/temp_id')
os.remove('/home/' + __USER__ + '/.ssh/temp_id.pub')
print("Error while sending file")
if __name__ == '__main__':
send()