-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-ssh-show.py
More file actions
36 lines (31 loc) · 1.12 KB
/
function-ssh-show.py
File metadata and controls
36 lines (31 loc) · 1.12 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
35
36
import paramiko
import time
def connect(server_ip, server_port, user, passwd):
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print(f'connecting to {server_ip}')
ssh_client.connect(hostname=server_ip, port=server_port, username=user, password=passwd, look_for_keys=False,
allow_agent=False)
return ssh_client
def get_shell(ssh_client):
shell = ssh_client.invoke_shell()
return shell
def send_command(shell, command, timeout=1):
print(f'sending command: {command}')
shell.send(command +'\n')
time.sleep(timeout)
def show(shell, n=100000):
output = shell.recv(n)
return output.decode()
def close(ssh_client):
if ssh_client.get_transport().is_active() == True:
print('closing connection')
ssh_client.close()
if __name__ == '__main__':
router1 = {'server_ip': '10.1.1.1', 'server_port': '22', 'user':'admin', 'passwd':'admin'}
client = connect(**router1)
shell = get_shell(client)
send_command(shell,'term len 0')
send_command(shell,'show ver')
output = show(shell)
print(output)