-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh_cmd.py
33 lines (27 loc) · 1.04 KB
/
ssh_cmd.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
import paramiko
import getpass
def ssh_command(ip, port, user, passwd):
client = paramiko.SSHClient()
# Because we’re controlling both ends of this connection
# we set the policy to accept the SSH key for the SSH server we’re connecting to and make the connection
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, port=int(port), username=user,
password=passwd, timeout=1)
while True:
cmd = input('Enter command or <CR>: ')
if not cmd.strip():
return
_, stdout, stderr = client.exec_command(cmd)
output = stdout.readlines() + stderr.readlines()
if output:
print('--- Output ---')
for line in output:
print(line.strip())
if __name__ =='__main__':
# user = getpass.getuser()
user = input('Username: ')
password = getpass.getpass()
ip = input('Enter server IP: ')
port = input('Enter port or <CR>: ')
while True:
ssh_command(ip, port, user, password)