-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparamiko
51 lines (38 loc) · 1.65 KB
/
paramiko
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Paramiko - SSH Library
import paramiko
import time
ip_addr = '10.11.12.55'
username = 'myuser'
password = 'mypass'
# Port only required if not default 22
port = 8022
# Create the SSH Client Object
remote_conn_pre = paramiko.SSHClient()
# Tell paramiko to blindly accept any host keys (not good security)
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# An alternative to the 'accept any keys' is you can use your system host keys
remote_conn_pre.load_system_host_keys()
# Or to load ssh keys from a specific path
remote_conn_pre.load_host_keys("/home/myuser/.ssh/known_host")
# Create SSH Connection
remote_conn_pre.connect(ip_addr, username=username, password=password, look_for_keys=False, allow_agents=False, port=port)
# Invoke Shell, creates interactive shell when interacting with the device
remote_conn = remote_conn_pre.invoke_shell()
# Collect Output, receive up to 5000 bytes
output = remote_conn.recv(5000)
# To disable paging
output = remote_conn.send("terminal length 0\n")
# To send a command down the channel and print it
remote_conn.send("show ip int brief")
output = remote_conn.recv(5000)
print output
# If there is no data it will just sit there and eventually timeout
# You can define the timeout like this, 6 seconds
remote_conn.settimeout(6.0)
# To see if there is data ready to be read
remote_conn.recv_ready() # Returns boolean
# For really long output the max you can receive is 65535 bytes
remote_conn.recv(65535)
# If you need to exceed this limit you can do it via a loop, checking for recv_ready()
# When building this into a python script, you need to introduce delay to to give the router time to respond
time.sleep(2)