Python Forum
telnet stuck when get Press any key to continue - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: telnet stuck when get Press any key to continue (/thread-7245.html)



telnet stuck when get Press any key to continue - anna - Dec-30-2017

telnet stuck when command output get " Press any key to continue, 'e' to exit, 'n' for nopause"


#!/usr/bin/python
import paramiko
import sys
import os
import xlrd
import unidecode
import telnetlib
import time

def telNetConnection(column1,column2,column3):
 host = [ (column1),]
 for host in host:
  print 'Processing'+' '+column1
  user  = (column2)
  password = (column3)
  telnet  = telnetlib.Telnet(host)
  telnet.read_until('User name: ', 3)
  telnet.write(user.encode('ascii') + '\r')
  telnet.read_until('Password: ', 3)
  telnet.write(password.encode('ascii') + '\r')
  telnet.read_until('>',3)
  telnet.write('statistics mac 1~48' + '\r\n')
  #telnet.read_until("    Press any key to continue, 'e' to exit, 'n' for nopause")
  #telnet.write('n')
  telnet.read_until('>')
  telnet.write('exit' + '\r')
  output=telnet.read_all()
  f = open('macportdetail.txt', 'w')
  f.write(str(output))


os.chdir("/home/anna/scripts")
workbook = xlrd.open_workbook('zyxeldslams.xlsx')
worksheet = workbook.sheet_by_name('Sheet1')

try:
    for x in range(1,4):
        column1 = (worksheet.cell(x,0).value).decode('ascii')
        column2 = (worksheet.cell(x,1).value).decode('ascii')
        column3 = str(worksheet.cell(x,2).value).decode('utf-8')
        telNetConnection(column1,column2,column3)
except:
       print "Connection time out"

sample.. telnet session
device> statistics mac 1~48
Port: 3
index vid mac
----- ---- -----------------
1 3624 00:17:7c:6d:fd:84
Port: 6
index vid mac
----- ---- -----------------
2 3624 ec:22:80:f1:67:ce
Port: 7
index vid mac
----- ---- -----------------
3 3624 ec:08:6b:96:94:36
Port: 10
index vid mac
----- ---- -----------------
4 3624 0c:d2:b5:58:cf:40
Port: 11
index vid mac
----- ---- -----------------
5 3624 48:ee:0c:d0:da:15
Port: 14
index vid mac
----- ---- -----------------
6 3624 00:17:7c:7c:8e:aa
Port: 18
index vid mac
----- ---- -----------------
7 3624 48:ee:0c:bd:bf:c5
Port: 20
index vid mac
----- ---- -----------------
8 3624 00:17:7c:75:96:55
Port: 27
index vid mac
----- ---- -----------------
9 3624 48:ee:0c:a8:d6:73
Port: 28
index vid mac
----- ---- -----------------
10 3624 10:62:eb:6a:da:d6

Press any key to continue, 'e' to exit, 'n' for nopause


RE: telnet stuck when get Press any key to continue - anna - Dec-30-2017

Hi There,

Manage to previous issue, however, telnet timeout taking too much time. what could be the issue?

#!/usr/bin/python
import paramiko
import sys
import os
import xlrd
import unidecode
import telnetlib
import time

def telNetConnection(column1,column2,column3):
 host = [ (column1),]
 for host in host:
  print 'Processing'+' '+column1
  user  = (column2)
  password = (column3)
  telnet  = telnetlib.Telnet(host,23,2)
  telnet.read_until('User name:',3)
  telnet.write(user.encode('ascii') + '\r')
  telnet.read_until('Password:',3)
  telnet.write(password.encode('ascii') + '\r')
  #telnet.read_until('>',3)
  telnet.write('statistics mac 1~48' + '\r\r\r')
  #telnet.read_until("    Press any key to continue, 'e' to exit, 'n' for nopause")
  #telnet.write('n')
  telnet.read_until('>',3)
  telnet.write('exit' + '\r')
  output=telnet.read_all()
  f = open('macportdetail.txt', 'w')
  f.write(host+" "+str(output))


os.chdir("/home/anna/scripts")
workbook = xlrd.open_workbook('zyxeldslams.xlsx')
worksheet = workbook.sheet_by_name('Sheet1')

#try:
for x in range(1,375):
    column1 = (worksheet.cell(x,0).value).decode('ascii')
    column2 = (worksheet.cell(x,1).value).decode('ascii')
    column3 = str(worksheet.cell(x,2).value).decode('utf-8')
    try:
        telNetConnection(column1,column2,column3)
    except:
         print "Connection time out"
         pass



RE: telnet stuck when get Press any key to continue - anna - Dec-31-2017

This script is taking IP and credentials are excel file and telnet and print output. This file is having morethan 1000+ devices

required support for below issues

1) telnetlib.Telnet timeout is taking two much time, i have provide timeout as 2 second.
2) i am able to print output to single file

current output is


Port: 3
index vid mac
----- ---- -----------------
1 3624 00:17:7c:6d:fd:84
Port: 6
index vid mac
----- ---- -----------------
2 3624 ec:22:80:f1:67:ce
Port: 7
index vid mac
----- ---- -----------------
3 3624 ec:08:6b:96:94:36
Port: 10
index vid mac
----- ---- -----------------
4 3624 0c:d2:b5:58:cf:40
Port: 11
index vid mac
----- ---- -----------------
5 3624 48:ee:0c:d0:da:15

i want to print this as below
1) Sr.No---- incremental serial number
2) hostip --- to whom script is telnetting
3) port, index, vid and mac from output

Sr.No hostip port index vid mac
1 10.10.10.1 3 1 3624 00:17:7c:6d:fd:84
2 10.10.10.1 6 2 3624 ec:22:80:f1:67:ce
3 10.10.10.1 7 3 3624 ec:08:6b:96:94:36
4 10.10.10.1 10 4 3624 0c:d2:b5:58:cf:40