-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMyNointeractive_SSH.py
64 lines (56 loc) · 2.3 KB
/
MyNointeractive_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
import argparse
import datetime
import paramiko
import platform
import re
ip = "192.168.148.128"
port = "22"
username = "root"
password = "981129"
SSH_CON_TIMEOUT = 10
def Nointeractive_SSH(ip, port, username, password, SSH_CON_TIMEOUT=5):
ssh = paramiko.SSHClient()
try:
print('[-] try login ssh {}@{}:{} .....'.format(username, ip, port))
# 创建一个ssh的白名单 paramiko.AutoAddPolicy()
# #加载创建的白名单
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# compress 启用压缩
ssh.connect(ip, port=port, username=username, password=password, compress=True, timeout=SSH_CON_TIMEOUT)
print('[+] Login successfully')
print('[+] username: ', username, ' ,password: ', password)
while True:
print()
cmd = input("shell>")
if cmd == "exit":
exit(0)
else:
stdin, stdout, stderr = ssh.exec_command(cmd) # stdin为输入,stdout为正确输出,stderr为错误输出,同时只有一个变量有值。
printdata(stdout.read().decode('utf-8'))
except Exception as e:
print("Error:", e)
ssh.close()
def printdata(data):
if platform.system() == "Windows":
dataa = re.sub(r'\x1b\[.*?m', '', data, flags=re.M)
print(dataa, end='')
elif platform.system() == "Linux":
print(data, end='')
if __name__ == '__main__':
usage = """
Non Interactive SSH connection
Input exit to quit.
"""
parser = argparse.ArgumentParser(usage=usage, description="des")
parser.add_argument("-H", "--host", type=str, dest="host", required=True, help="Hosts to scan")
parser.add_argument("--port", type=int, dest="port", default=22, help="ports to scan")
parser.add_argument("-t", "--timeout", type=int, default=10, dest="timeout", help="Request timeout")
parser.add_argument("-u", "--user", type=str, required=True, dest="username", help="Username")
parser.add_argument("-p", "--pass", type=str, required=True, dest="password", help="Password")
args = parser.parse_args()
host = args.host
port = args.port
username = args.username
password = args.password
SSH_CON_TIMEOUT = args.timeout
Nointeractive_SSH(host, port, username, password, SSH_CON_TIMEOUT)