child = pexpect.spawn("telnet {} {}".format(ip, port))
child.logfile = sys.stdout.buffer
child.delaybeforesend = None
try:
child.expect([r"Escape character is '\^\]'",
r".*Connection refused\r\n"], timeout=30)
child.sendline("\n")
child.sendline("\n")
index = child.expect([r"Username: ", ".*?>", ".*?#"], timeout=30)
if index == 1:
time.sleep(2)
child.sendline("en")
child.expect("Password: ", timeout=10)
time.sleep(1)
child.sendline("password")
child.expect(#"), timeout=10)
time.sleep(2)
child.sendline("cmd")
child.expect(#"), timeout=10)
time.sleep(3)
child.sendline("cmd")
child.expect(#"), timeout=10)
time.sleep(4)
child.sendline("cmd")
child.expect(#"), timeout=10)
time.sleep(3)
child.sendline("cmd")
child.expect("#", timeout=10)
time.sleep(2)
child.sendline("cmd")
print("3rd block executed")
print("interface set to default)
time.sleep(10)
child.expect("#", timeout=10)
child.sendline("\n")
child.sendline("some cmd")
time.sleep(10)
sh_line = child.before.decode('utf-8').splitlines()
for line in sh_line:
print(line)
child.close()
locally it is working fine but in the pipeline seeing below
22:33:59 Trying 10.102.126.141...
22:33:59 Connected to 10.102.126.141.
22:33:59 Escape character is '^\]'.
22:33:59
22:33:59
22:33:59
22:33:59
22:33:59
22:33:59
22:33:59
22:33:59
these many newline i have not given in my code, expected patterns [r"Username: ", ".*?\>", ".*?#"] not at all displaying but in the local expected patterns are displaying correctly after hit the telnet, can some one please help to fix this pipeline problem
I have tried by given time and also by passing newline(why because if we hit enter atleast it should display expected pattern.
expected :
22:33:59 Trying 10.102.126.141...
22:33:59 Connected to 10.102.126.141.
22:33:59 Escape character is '^\]'.
after this it should display expected patterns.