Commit 5c45163d authored by Raphael Defosseux's avatar Raphael Defosseux

CI: adding retry strategy when connecting to remote server

Signed-off-by: default avatarRaphael Defosseux <raphael.defosseux@eurecom.fr>
parent 67c089cd
...@@ -90,38 +90,47 @@ class SSHConnection(): ...@@ -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')
else: self.ssh.expect('password:')
logging.debug('self.sshresponse = ' + str(self.sshresponse)) self.ssh.sendline(password)
sys.exit('SSH Connection Failed') self.sshresponse = self.ssh.expect(['\$', 'Permission denied', 'password:', pexpect.EOF, pexpect.TIMEOUT])
elif self.sshresponse == 1: if self.sshresponse == 0:
self.ssh.sendline(password) count = 10
self.sshresponse = self.ssh.expect(['\$', 'Permission denied', 'password:', pexpect.EOF, pexpect.TIMEOUT]) connect_status = True
if self.sshresponse == 0: else:
pass 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 == 2: if connect_status:
# 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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment