-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathdevice_connection.py
More file actions
71 lines (61 loc) · 2.82 KB
/
Copy pathdevice_connection.py
File metadata and controls
71 lines (61 loc) · 2.82 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
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
import paramiko
import logging
import socket
from paramiko.ssh_exception import BadHostKeyException, AuthenticationException, SSHException
logger = logging.getLogger(__name__)
DEFAULT_CMD_EXECUTION_TIMEOUT_SEC = 10
class DeviceConnection:
'''
DeviceConnection uses Paramiko module to connect to devices
Paramiko module uses fallback mechanism where it would first try to use
ssh key and that fails, it will attempt username/password combination
'''
def __init__(self, hostname, username, password=None):
'''
Class constructor
@param hostname: hostname of device to connect to
@param username: username for device connection
@param password: password for device connection
'''
self.hostname = hostname
self.username = username
self.password = password
def execCommand(self, cmd, timeout=DEFAULT_CMD_EXECUTION_TIMEOUT_SEC):
'''
Executes command on remote device
@param cmd: command to be run on remote device
@param timeout: timeout for command run session
@return: stdout, stderr, value
stdout is a list of lines of the remote stdout gathered during command execution
stderr is a list of lines of the remote stderr gathered during command execution
value: 0 if command execution raised no exception
nonzero if exception is raised
'''
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if isinstance(cmd, list):
cmd = ' '.join(cmd)
stdOut = stdErr = []
retValue = 1
try:
client.connect(self.hostname, username=self.username, password=self.password, allow_agent=False)
si, so, se = client.exec_command(cmd, timeout=timeout)
stdOut = so.readlines()
stdErr = se.readlines()
retValue = 0
except SSHException as sshException:
logger.error('SSH Command failed with message: %s' % sshException)
except AuthenticationException as authenticationException:
logger.error('SSH Authentiaction failure with message: %s' % authenticationException)
except BadHostKeyException as badHostKeyException:
logger.error('SSH Authentiaction failure with message: %s' % badHostKeyException)
except socket.timeout as e:
# The ssh session will timeout in case of a successful reboot
logger.error('Caught exception socket.timeout: {}, {}, {}'.format(repr(e), str(e), type(e)))
retValue = 255
except Exception as e:
logger.error('Exception caught: {}, {}, type: {}'.format(repr(e), str(e), type(e)))
logger.error(sys.exc_info())
finally:
client.close()
return stdOut, stdErr, retValue