CI: adding retry strategy when connecting to remote server
Signed-off-by: Raphael Defosseux <raphael.defosseux@eurecom.fr>
This commit is contained in:
+36
-27
@@ -90,38 +90,47 @@ class SSHConnection():
|
|||||||
self.htmlUEConnected = 0
|
self.htmlUEConnected = 0
|
||||||
|
|
||||||
def open(self, ipaddress, username, password):
|
def open(self, ipaddress, username, password):
|
||||||
self.ssh = pexpect.spawn('ssh', [username + '@' + ipaddress], timeout = 5)
|
count = 0
|
||||||
self.sshresponse = self.ssh.expect(['Are you sure you want to continue connecting (yes/no)?', 'password:', 'Last login', pexpect.EOF, pexpect.TIMEOUT])
|
connect_status = False
|
||||||
if self.sshresponse == 0:
|
while count < 4:
|
||||||
self.ssh.sendline('yes')
|
self.ssh = pexpect.spawn('ssh', [username + '@' + ipaddress], timeout = 5)
|
||||||
self.ssh.expect('password:')
|
self.sshresponse = self.ssh.expect(['Are you sure you want to continue connecting (yes/no)?', 'password:', 'Last login', pexpect.EOF, pexpect.TIMEOUT])
|
||||||
self.ssh.sendline(password)
|
|
||||||
self.sshresponse = self.ssh.expect(['\$', 'Permission denied', 'password:', pexpect.EOF, pexpect.TIMEOUT])
|
|
||||||
if self.sshresponse == 0:
|
if self.sshresponse == 0:
|
||||||
pass
|
self.ssh.sendline('yes')
|
||||||
|
self.ssh.expect('password:')
|
||||||
|
self.ssh.sendline(password)
|
||||||
|
self.sshresponse = self.ssh.expect(['\$', 'Permission denied', 'password:', pexpect.EOF, pexpect.TIMEOUT])
|
||||||
|
if self.sshresponse == 0:
|
||||||
|
count = 10
|
||||||
|
connect_status = True
|
||||||
|
else:
|
||||||
|
logging.debug('self.sshresponse = ' + str(self.sshresponse))
|
||||||
|
elif self.sshresponse == 1:
|
||||||
|
self.ssh.sendline(password)
|
||||||
|
self.sshresponse = self.ssh.expect(['\$', 'Permission denied', 'password:', pexpect.EOF, pexpect.TIMEOUT])
|
||||||
|
if self.sshresponse == 0:
|
||||||
|
count = 10
|
||||||
|
connect_status = True
|
||||||
|
else:
|
||||||
|
logging.debug('self.sshresponse = ' + str(self.sshresponse))
|
||||||
|
elif self.sshresponse == 2:
|
||||||
|
# Checking if we are really on the remote client defined by its IP address
|
||||||
|
self.command('stdbuf -o0 ifconfig | egrep --color=never "inet addr:"', '\$', 5)
|
||||||
|
result = re.search(str(ipaddress), str(self.ssh.before))
|
||||||
|
if result is None:
|
||||||
|
self.close()
|
||||||
|
else:
|
||||||
|
count = 10
|
||||||
|
connect_status = True
|
||||||
else:
|
else:
|
||||||
|
# debug output
|
||||||
|
logging.debug(str(self.ssh.before))
|
||||||
logging.debug('self.sshresponse = ' + str(self.sshresponse))
|
logging.debug('self.sshresponse = ' + str(self.sshresponse))
|
||||||
sys.exit('SSH Connection Failed')
|
count += 1
|
||||||
elif self.sshresponse == 1:
|
if connect_status:
|
||||||
self.ssh.sendline(password)
|
|
||||||
self.sshresponse = self.ssh.expect(['\$', 'Permission denied', 'password:', pexpect.EOF, pexpect.TIMEOUT])
|
|
||||||
if self.sshresponse == 0:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
logging.debug('self.sshresponse = ' + str(self.sshresponse))
|
|
||||||
sys.exit('SSH Connection Failed')
|
|
||||||
elif self.sshresponse == 2:
|
|
||||||
# Checking if we are really on the remote client defined by its IP address
|
|
||||||
self.command('stdbuf -o0 ifconfig | egrep --color=never "inet addr:"', '\$', 5)
|
|
||||||
result = re.search(str(ipaddress), str(self.ssh.before))
|
|
||||||
if result is None:
|
|
||||||
sys.exit('SSH Connection Failed: TIMEOUT !!!')
|
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
# debug output
|
sys.exit('SSH Connection Failed')
|
||||||
logging.debug(str(self.ssh.before))
|
|
||||||
logging.debug('self.sshresponse = ' + str(self.sshresponse))
|
|
||||||
sys.exit('SSH Connection Failed!!!')
|
|
||||||
|
|
||||||
def command(self, commandline, expectedline, timeout):
|
def command(self, commandline, expectedline, timeout):
|
||||||
logging.debug(commandline)
|
logging.debug(commandline)
|
||||||
|
|||||||
Reference in New Issue
Block a user