-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathsshRunCmd.py
83 lines (78 loc) · 3.23 KB
/
sshRunCmd.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
import paramiko
import sys
def sshcheck(hostname, port, username, password, cmd):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(hostname, port, username, password, timeout=10)
print("[+] Valid: %s %s"%(username,password))
if cmd == 'shell':
while(1):
cmd = input("#")
if cmd == 'exit':
print("[*] Exit.")
ssh.close();
return
stdin, stdout, stderr = ssh.exec_command(cmd)
print(stdout.read().decode())
result = stdout.read()
else:
stdin, stdout, stderr = ssh.exec_command(cmd)
print(stdout.read().decode())
result = stdout.read()
ssh.close();
except paramiko.AuthenticationException:
print("[!] Authentication failed")
except Exception:
print("[!] Connection Failed")
except paramiko.SSHException:
print("[!] Unable to establish SSH connection: %s"%(sshException))
def sshcheckfile(hostname, port, username, keyfile, cmd):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
key=paramiko.RSAKey.from_private_key_file(keyfile)
try:
ssh.connect(hostname, port, username, pkey=key, timeout=2)
print("[+] Valid: %s %s"%(username,keyfile))
if cmd == 'shell':
while(1):
cmd = input("#")
if cmd == 'exit':
print("[*] Exit.")
ssh.close();
return
stdin, stdout, stderr = ssh.exec_command(cmd)
print(stdout.read().decode())
result = stdout.read()
else:
stdin, stdout, stderr = ssh.exec_command(cmd)
print(stdout.read().decode())
result = stdout.read()
ssh.close();
except paramiko.AuthenticationException:
print("[!] Authentication failed")
except Exception:
print("[!] Connection Failed")
except paramiko.SSHException:
print("[!] Unable to establish SSH connection: %s"%(sshException))
if __name__ == "__main__":
if len(sys.argv)!=7:
print('[!]Wrong parameter')
print('sshRunCmd')
print('Remote command execution via SSH(Support password and privatekeyfile)')
print('Author:3gstudent')
print('Usage:')
print('%s <host> <port> <mode><user> <password> <cmd>'%(sys.argv[0]))
print('<mode>:')
print('- plaintext')
print('- keyfile')
print('If the <cmd> is shell,you will get an interactive shell')
print('Eg.')
print('%s 192.168.1.1 22 plaintext root toor shell'%(sys.argv[0]))
print('%s 192.168.1.1 22 keyfile root id_rsa ps'%(sys.argv[0]))
sys.exit(0)
else:
if sys.argv[3] == 'plaintext':
sshcheck(sys.argv[1], int(sys.argv[2]), sys.argv[4], sys.argv[5], sys.argv[6])
elif sys.argv[3] == 'keyfile':
sshcheckfile(sys.argv[1], int(sys.argv[2]), sys.argv[4], sys.argv[5], sys.argv[6])