blob: ad5aaa0739cecb3310dca5b4837c3964b9df545f [file] [log] [blame]
import os
import socket
from time import sleep
from paramiko.ssh_exception import SSHException
from mdt import config
from mdt import console
from mdt import discoverer
from mdt import sshclient
class NetworkCommand:
def __init__(self):
self.config = config.Config()
self.discoverer = discoverer.Discoverer(self)
self.device = self.config.preferredDevice()
self.address = None
def add_device(self, hostname, address):
if not self.device:
self.device = hostname
self.address = address
elif self.device == hostname:
self.address = address
def run(self, args):
if not self.preConnectRun(args):
return 1
if not self.address:
if self.device:
print('Waiting for device {0}...'.format(self.device))
else:
print('Waiting for a device...')
while not self.address:
sleep(0.1)
client = None
try:
print('Connecting to {0} at {1}'.format(self.device, self.address))
client = sshclient.SshClient(self.device, self.address)
return self.runWithClient(client, args)
except sshclient.KeyPushError as e:
print("Unable to push keys to the device: {0}".format(e))
return 1
except sshclient.DefaultLoginError as e:
print("Can't login using default credentials: {0}".format(e))
return 1
except SSHException as e:
print("Couldn't establish ssh connection to device: {0}".format(e))
return 1
except socket.error as e:
print("Couldn't establish ssh connection to device: socket error: {0}".format(e))
return 1
except console.SocketTimeoutError as e:
print("\r\nConnection to {0} at {1} closed: socket timeout".format(self.device, self.address))
return 1
except console.ConnectionClosedError as e:
if e.exit_code:
print("\r\nConnection to {0} at {1} closed "
"with exit code {2}".format(self.device, self.address, e.exit_code))
else:
print("\r\nConnection to {0} at {1} closed".format(self.device, self.address))
return e.exit_code
finally:
if client:
client.close()
def runWithClient(self, client, args):
return 1
def preConnectRun(self, args):
return True