-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh_connection.py
72 lines (46 loc) · 2.1 KB
/
ssh_connection.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# ssh_connection.py
# Byers Applied Python example with mistakes or additions by Phil (2015)
import paramiko
import time
import pysftp
class SSHConnection(object):
# Using base ssh class to support Cisco
# and subclasses for other vendors.
# We want to:
# - initialize with a network device object
# - obtain ssh credentials
# - establish our connection
# - pass commands to eliminate LF and unwanted prompts
# - eventually enable and config
#
def __init__(self, net_device):
self.net_device = net_device
self.ip = net_device.ip_address
print "SSHConnection package reporting." #not seeing this
# We have passed in the device and saved local attributes
if net_device.ssh_port:
self.port = net_device.ssh_port
else:
self.port = 22
self.username = net_device.credentials.username
self.password = net_device.credentials.password
def establish_connection(self):
print
print "." * 80
# Use paramiko to create local sshclient object
self.remote_conn_pre = paramiko.SSHClient()
# Allow untrusted hosts to be added automically
self.remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Initiate SSH connection and map login parameters to paramiko
print "SSH connecton established to {}:{}".format(self.ip, self.port)
self.remote_conn_pre.connect(hostname=self.ip, port=self.port, username=self.username, password=self.password)
# Use invoke_shell to establish interactive session
self.remote_conn = self.remote_conn_pre.invoke_shell()
print "Interactice SSH session establishe..."
# Strip initial router prompt
time.sleep(3) # Added for Arista because prompt did not arrive quickly
output = self.remote_conn.recv(1000)
# View output
print output
print "."*80
print