2020-11-11 12:18:48 -06:00
|
|
|
#!/usr/bin/env python3
|
2018-11-21 00:32:40 -06:00
|
|
|
|
|
|
|
import argparse
|
2020-11-11 12:18:48 -06:00
|
|
|
import pexpect
|
2018-11-21 00:32:40 -06:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
2020-11-11 12:18:48 -06:00
|
|
|
|
2018-11-21 00:32:40 -06:00
|
|
|
def main():
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='test_login cmdline parser')
|
|
|
|
parser.add_argument('-u', default="admin", help='login user name')
|
|
|
|
parser.add_argument('-P', default="YourPaSsWoRd", help='login password')
|
|
|
|
parser.add_argument('-p', type=int, default=9000, help='local port')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
KEY_UP = '\x1b[A'
|
|
|
|
KEY_DOWN = '\x1b[B'
|
|
|
|
KEY_RIGHT = '\x1b[C'
|
|
|
|
KEY_LEFT = '\x1b[D'
|
|
|
|
|
|
|
|
login_prompt = 'sonic login:'
|
|
|
|
passwd_prompt = 'Password:'
|
2020-11-11 12:18:48 -06:00
|
|
|
cmd_prompt = "{}@sonic:~\$ $".format(args.u)
|
2018-11-21 00:32:40 -06:00
|
|
|
grub_selection = "The highlighted entry will be executed"
|
2022-09-08 16:23:50 -05:00
|
|
|
firsttime_prompt = 'firsttime_exit'
|
2018-11-21 00:32:40 -06:00
|
|
|
|
2019-12-03 15:54:57 -06:00
|
|
|
i = 0
|
|
|
|
while True:
|
|
|
|
try:
|
2020-11-11 12:18:48 -06:00
|
|
|
p = pexpect.spawn("telnet 127.0.0.1 {}".format(args.p), timeout=600, logfile=sys.stdout, encoding='utf-8')
|
2019-12-03 15:54:57 -06:00
|
|
|
break
|
|
|
|
except Exception as e:
|
2020-11-11 12:18:48 -06:00
|
|
|
print(str(e))
|
2019-12-03 15:54:57 -06:00
|
|
|
i += 1
|
|
|
|
if i == 10:
|
|
|
|
raise
|
|
|
|
time.sleep(1)
|
2018-11-21 00:32:40 -06:00
|
|
|
|
|
|
|
# select ONIE embed
|
|
|
|
p.expect(grub_selection)
|
|
|
|
p.sendline(KEY_DOWN)
|
|
|
|
|
|
|
|
# install sonic image
|
|
|
|
while True:
|
2022-09-08 16:23:50 -05:00
|
|
|
i = p.expect([login_prompt, passwd_prompt, firsttime_prompt, grub_selection, cmd_prompt])
|
2018-11-21 00:32:40 -06:00
|
|
|
if i == 0:
|
|
|
|
# send user name
|
|
|
|
p.sendline(args.u)
|
|
|
|
elif i == 1:
|
|
|
|
# send password
|
|
|
|
p.sendline(args.P)
|
|
|
|
elif i == 2:
|
2022-09-08 16:23:50 -05:00
|
|
|
# fix a login timeout issue, caused by the login_prompt message mixed with the output message of the rc.local
|
|
|
|
time.sleep(1)
|
|
|
|
p.sendline()
|
|
|
|
elif i == 3:
|
2018-11-21 00:32:40 -06:00
|
|
|
# select onie install
|
|
|
|
p.sendline()
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
# check version
|
|
|
|
time.sleep(5)
|
|
|
|
p.sendline('show version')
|
|
|
|
p.expect([cmd_prompt])
|
2018-11-22 14:09:21 -06:00
|
|
|
p.sendline('show ip bgp sum')
|
|
|
|
p.expect([cmd_prompt])
|
|
|
|
p.sendline('sync')
|
|
|
|
p.expect([cmd_prompt])
|
2018-11-21 00:32:40 -06:00
|
|
|
|
2020-12-03 17:57:50 -06:00
|
|
|
|
2018-11-21 00:32:40 -06:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|