|
|
|
@ -15,13 +15,13 @@ class SSHFeatures(BasePlugin):
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.c = None
|
|
|
|
|
self.connection = None
|
|
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
|
""" Connect to a machine """
|
|
|
|
|
|
|
|
|
|
if self.c:
|
|
|
|
|
return self.c
|
|
|
|
|
if self.connection is not None:
|
|
|
|
|
return self.connection
|
|
|
|
|
|
|
|
|
|
retries = 10
|
|
|
|
|
retry_sleep = 10
|
|
|
|
@ -31,7 +31,7 @@ class SSHFeatures(BasePlugin):
|
|
|
|
|
if self.config.os() == "linux":
|
|
|
|
|
uhp = self.get_ip()
|
|
|
|
|
self.vprint(f"Connecting to {uhp}", 3)
|
|
|
|
|
self.c = Connection(uhp, connect_timeout=timeout)
|
|
|
|
|
self.connection = Connection(uhp, connect_timeout=timeout)
|
|
|
|
|
|
|
|
|
|
if self.config.os() == "windows":
|
|
|
|
|
args = {}
|
|
|
|
@ -43,15 +43,15 @@ class SSHFeatures(BasePlugin):
|
|
|
|
|
self.vprint(args, 3)
|
|
|
|
|
uhp = self.get_ip()
|
|
|
|
|
self.vprint(uhp, 3)
|
|
|
|
|
self.c = Connection(uhp, connect_timeout=timeout, user=self.config.ssh_user(), connect_kwargs=args)
|
|
|
|
|
self.connection = Connection(uhp, connect_timeout=timeout, user=self.config.ssh_user(), connect_kwargs=args)
|
|
|
|
|
except (paramiko.ssh_exception.SSHException, socket.timeout):
|
|
|
|
|
self.vprint(f"Failed to connect, will retry {retries} times. Timeout: {timeout}", 0)
|
|
|
|
|
retries -= 1
|
|
|
|
|
timeout += 10
|
|
|
|
|
time.sleep(retry_sleep)
|
|
|
|
|
else:
|
|
|
|
|
self.vprint(f"Connection: {self.c}", 3)
|
|
|
|
|
return self.c
|
|
|
|
|
self.vprint(f"Connection: {self.connection}", 3)
|
|
|
|
|
return self.connection
|
|
|
|
|
|
|
|
|
|
self.vprint("SSH network error", 0)
|
|
|
|
|
raise NetworkError
|
|
|
|
@ -71,11 +71,12 @@ class SSHFeatures(BasePlugin):
|
|
|
|
|
|
|
|
|
|
self.vprint("Running SSH remote run: " + cmd, 3)
|
|
|
|
|
self.vprint("Disown: " + str(disown), 3)
|
|
|
|
|
# self.vprint("Connection: " + self.connection, 1)
|
|
|
|
|
result = None
|
|
|
|
|
retry = 2
|
|
|
|
|
while retry > 0:
|
|
|
|
|
try:
|
|
|
|
|
result = self.c.run(cmd, disown=disown)
|
|
|
|
|
result = self.connection.run(cmd, disown=disown)
|
|
|
|
|
print(result)
|
|
|
|
|
# paramiko.ssh_exception.SSHException in the next line is needed for windows openssh
|
|
|
|
|
except (paramiko.ssh_exception.NoValidConnectionsError, UnexpectedExit, paramiko.ssh_exception.SSHException):
|
|
|
|
@ -113,7 +114,7 @@ class SSHFeatures(BasePlugin):
|
|
|
|
|
timeout = 30
|
|
|
|
|
while retries:
|
|
|
|
|
try:
|
|
|
|
|
res = self.c.put(src, dst)
|
|
|
|
|
res = self.connection.put(src, dst)
|
|
|
|
|
except (paramiko.ssh_exception.SSHException, socket.timeout, UnexpectedExit):
|
|
|
|
|
self.vprint(f"PUT Failed to connect, will retry {retries} times. Timeout: {timeout}", 3)
|
|
|
|
|
retries -= 1
|
|
|
|
@ -144,7 +145,7 @@ class SSHFeatures(BasePlugin):
|
|
|
|
|
retry = 2
|
|
|
|
|
while retry > 0:
|
|
|
|
|
try:
|
|
|
|
|
res = self.c.get(src, dst)
|
|
|
|
|
res = self.connection.get(src, dst)
|
|
|
|
|
except (paramiko.ssh_exception.NoValidConnectionsError, UnexpectedExit):
|
|
|
|
|
if retry <= 0:
|
|
|
|
|
raise NetworkError
|
|
|
|
@ -163,6 +164,6 @@ class SSHFeatures(BasePlugin):
|
|
|
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
|
""" Disconnect from a machine """
|
|
|
|
|
if self.c:
|
|
|
|
|
self.c.close()
|
|
|
|
|
self.c = None
|
|
|
|
|
if self.connection:
|
|
|
|
|
self.connection.close()
|
|
|
|
|
self.connection = None
|
|
|
|
|