-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh-rcmd.py
38 lines (31 loc) · 1.11 KB
/
ssh-rcmd.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
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, passphrase=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 = getpass.getuser()
password = getpass.getpass()
ip = input("Enter server IP: ")
port = input('Enter Port: ')
ssh_command(ip, port, user, password, 'ClientConnected')