-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
executable file
·82 lines (68 loc) · 2.64 KB
/
run.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import paramiko
import sys
import Client
import time
import getopt
usage = """
Usage:
\tpython3 run.py -b port_begin -e port_end -i ip_address
Example:
\ttpython3 run.py -b1024 -e65535 -i19.18.8.9"""
opts, args = getopt.getopt(sys.argv[1:], 's:c:p:b:e:i:z:h', ["server_timeout=", "client_time_out=",
"ssh_port=", "begin_port=", "end_port=",
"ip_address=", "chunk_size=", "help"])
port_begin: int = 1024
port_end: int = 65535
port: int = 22
FL: list = []
chunk: int = 500
srv_timeout: int = 5
host: str = '127.0.0.1'
user = 'root'
password = ''
client_timeout: int = 5
for o, a in opts:
if o in ('-b', '--port_begin'): port_begin = int(a)
if o in ('-e', '--port_end'): port_end = int(a)
if o in ('-p', '--ssh_port'): port = int(a)
if o in ('-z', '--chunk_size'): chunk = int(a)
if o in ('-s', '--server_timeout'): srv_timeout = int(a)
if o in ('-c', '--client_timeout'): client_timeout = int(a)
if o in ('-i', '--ip_address'): host = a
if o in ('-u', '--user'): user = a
if o in ('-w', '--password'): password = a
if o in ('-h', '--help'):
print(usage)
sys.exit(0)
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=host, port=port, username=user, password=password, look_for_keys=True, allow_agent=True)
ftp_client = ssh_client.open_sftp()
ftp_client.put('Server.py', '/tmp/Server.py')
ftp_client.close()
full = (port_end - port_begin) // chunk
tale = (port_end - port_begin) % chunk
print("full {} tale {} {} {}".format(full, tale, port_end, port_begin) )
for i in range(0, full):
chunk_begin = port_begin+(chunk*i)
chunk_end = port_begin+(chunk*i)+chunk
print("Port range",chunk_begin , chunk_end-1)
stdin, stdout, stderr = ssh_client.exec_command(
"python3 /tmp/Server.py {0} {1} {2} &".format(chunk_begin, chunk_end, srv_timeout))
time.sleep(3);
FL = Client.scanRange(chunk_begin, chunk_end, host, client_timeout)
if tale > 0:
chunk_begin = chunk_end if full > 0 else port_begin
chunk_end = chunk_end + tale if full > 0 else port_end
print("Port range",chunk_begin , chunk_end, tale )
stdin, stdout, stderr = ssh_client.exec_command(
"python3 /tmp/Server.py {0} {1} {2} &".format(chunk_begin, chunk_end, srv_timeout))
time.sleep(3);
FL = Client.scanRange(chunk_begin, chunk_end, host, client_timeout)
F = open("openports.txt", "a")
FL = sorted(FL)
F.writelines(list("%s\n" % item for item in FL))
F.close()
ssh_client.close()