11: Scripts
EMG™ Edge Management Gateway User Guide
231
print("Scanning device port", devicePort, "...")
port = str(devicePort)
p=pexpect.spawn('portsession', ['-p', port])
# Login (if required), and wait for the first prompt
p.sendline("")
gotPrompt = False
emgDevice = False
cnt = 1
while not gotPrompt:
i = p.expect([pexpect.TIMEOUT, pexpect.EOF,
'login:', 'Error: (.*)\r\n',
'>'], timeout=10)
if i == 0: # Timeout
cnt = cnt + 1
if cnt == 3:
print("Timeout waiting to connect to DP", devicePort, ".")
p.terminate(True)
p.wait()
break
# may need to send a CR to get prompt
p.send("\r")
elif i == 1: # EOF
print("portsession on DP ", devicePort, "unexpectedly
terminated.")
break
elif i == 2: # login prompt
p.sendline("sysadmin")
p.expect("Password:")
p.sendline("PASS")
emgDevice = True
gotPrompt = True
elif i == 3: # error from portsession
print(p.match.group(1).decode('utf-8'))
p.terminate(True)
p.wait()
break
elif i == 4: # prompt
gotPrompt = True
# end if while not gotPrompt:
if not gotPrompt:
devicePort = devi 1
pList.append('')
continue
# Detect the prompt
devPrompt = ""
p.send("\n")
i = p.expect([pexpect.TIMEOUT, pexpect.EOF, '\r\n(.*)>'],
timeout=10)
if i == 0 or i == 1: # Timeout or EOF
print("Timeout waiting for the prompt on DP", devicePort, ".")
p.terminate(True)