Python Forum

Full Version: telnet unexpected output printing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there, manual telnet is showing below output, however my program is not showing telnet output, please suggest changes.

Manual Telnet

Connected to 10.17.1.99.
Escape character is '^]'.
Login: admin
Password:

Host> dsl -c

Connect to DSL Console...
Press <Ctrl-\> or <Ctrl-Z> to return.

$get bridge port forwarding

Port Id : 3 vlan id : 3241
Mac Addr : E0:5F:B9:99:20:C1
Status : Learned

Port Id : 5 vlan id : 3604
Mac Addr : 0C:D2:B5:58:C4:4E
Status : Learned

Port Id : 6 vlan id : 3604
Mac Addr : 0C:D2:B5:61:EF:A4
Status : Learned

Port Id : 9 vlan id : 3604
Mac Addr : E4:6F:13:86:C7:D1
Status : Learned

Port Id : 10 vlan id : 3604
Mac Addr : 00:17:7C:75:93:E5
Status : Learned

Port Id : 14 vlan id : 3604
Mac Addr : 00:17:7C:7C:B1:D2
Status : Learned

Port Id : 19 vlan id : 3604
Mac Addr : 00:17:7C:7D:07:62
Status : Learned

Port Id : 24 vlan id : 3604
Mac Addr : 1C:5F:2B:55:59:A6
Status : Learned

Port Id : 193 vlan id : 1
Mac Addr : 00:1A:A2:26:72:2C
Status : Learned

Port Id : 193 vlan id : 3241
Mac Addr : 00:90:1A:A3:7C:DD
Status : Learned

Port Id : 193 vlan id : 3604
Mac Addr : 40:7C:7D:1F:89:73
Status : Learned
$
Host>

but telnet script is not showing output.

#!/usr/bin/python
import paramiko
import xlwt
import sys
import os
import xlrd
import unidecode
import telnetlib
import time
import re
from time import sleep
user = 'admin'
password = '1234'

with open('ut.txt','r') as ipfile:
    for sr_no, line in enumerate(ipfile, start=1):
        host = line.strip()
        try:
            print 'Connecting .....'+str(host)
            telnet = telnetlib.Telnet(host, 23, 2)
            telnet.read_until('Login:', 3)
            telnet.write(user.encode('ascii') + '\r')
            telnet.read_until('Password:', 3)
            telnet.write(password.encode('ascii') + '\r')
            print 'username password accepted'
            telnet.read_until('>')
            telnet.write('dsl -c' + '\r')
            telnet.read_until('$')
            print 'dsl prompt'
            telnet.write('get bridge port forwarding'+'\r')
            telnet.read_until('$')
            print 'Mac Details'
            telnet.write('\x1A' +'\r')
            print 'Control Z send'
            telnet.read_until('>')
            telnet.write('exit'+ '\r')
            output = telnet.read_all()
            print (output)
        except Exception as excp:
            print('Request Time out')
Script output

Connecting .....10.17.1.99
username password accepted
dsl prompt
Mac Details
Control Z send

and not showing output.
please support.
Why '\r' instead of '\n'? You write an empty string.? Why do you send a special symbol at all?
there are two prompts

1) >
2) $
MAC details is getting on second prompt,to come out of $ prompt need to send Ctrl+z, so sending special character.

#!/usr/bin/python
import paramiko
import xlwt
import sys
import os
import xlrd
import unidecode
import telnetlib
import time
import re
from time import sleep
user = 'admin'
password = '1234'

with open('ut.txt','r') as ipfile:
    for sr_no, line in enumerate(ipfile, start=1):
        host = line.strip()
        try:
            print 'Connecting .....'+str(host)
            telnet = telnetlib.Telnet(host, 23, 2)
            telnet.read_until('Login:', 3)
            telnet.write(user.encode('ascii') + '\n')
            telnet.read_until('Password:', 3)
            telnet.write(password.encode('ascii') + '\n')
            print 'username password accepted'
            telnet.read_until('>')
            print 'Outer Prompt'
            telnet.write('dsl -c' + '\n')
            print 'dsl -c'
            telnet.read_until('$')
            print 'dsl prompt'
            telnet.write('get bridge port forwarding'+'\n')
            sleep(1)
            #telnet.read_until('$')
            print 'Mac Details'
            telnet.write('\x1A')
            print 'Control Z send'
            telnet.read_until('>')
            telnet.write('exit'+ '\n')
            output = telnet.read_all()
            print (output)
        except Exception as excp:
            print('Request Time out')
tried with '\n' instead of '\r' but same result.