Python script to get text from telnet-router.
posted by FranciscoT on 2009-07-22 21:35:25
Hello,
I'm trying a python script to get ip from my router using telnet. When it's calls the function get_ip, the script blocks up. I think the problem is with buf and splitlines.
The text out from telnet to buf should be:
VCC Con. Catego. Service Interface Proto. IGMP QoS State Status IP
ID Name Name address
0.8.35 1 UBR br_8_35 nas_8_35 Bridge N/A Enable Enable Up
0.8.35 2 UBR pppoe_8_35_2 ppp_8_35_2 PPPoE Disable Enable Enable Up xx.xx.xx.xx
Hit to continue
Sorry for my english.
Thanks.
I'm trying a python script to get ip from my router using telnet. When it's calls the function get_ip, the script blocks up. I think the problem is with buf and splitlines.
The text out from telnet to buf should be:
VCC Con. Catego. Service Interface Proto. IGMP QoS State Status IP
ID Name Name address
0.8.35 1 UBR br_8_35 nas_8_35 Bridge N/A Enable Enable Up
0.8.35 2 UBR pppoe_8_35_2 ppp_8_35_2 PPPoE Disable Enable Enable Up xx.xx.xx.xx
Hit
#! /usr/bin/env python # -*- coding: utf-8 -*- import getpass import sys import telnetlib def get_ip(tn): tn.write("3\n") tn.write("3\n") print "\nEntrando en WAN..." buf = tn.read_until("Hit <enter> to continue") buf = buf.splitlines() print buf[1] ip='None' for l in range(len(buf)): if buf[l].find("PPPoE") >= 0: ip = buf[l].strip().rsplit()[10] print ip return ip def connect(): HOST = "192.168.1.1" user = "user" password = "password" tn = telnetlib.Telnet(HOST, 23) tn.set_debuglevel(0) tn.read_until("Login: ") print "\nLogin..." tn.write(user + "\n") tn.read_until("Password: ") print"\nPass..." tn.write(password + "\n") return tn def main(): tn = connect() tn.set_debuglevel(0) print "\nConectado!" print "\nIp actual: "+get_ip(tn) tn.close() main()
Thanks.

0
I suspect the problem is with rsplit:If I change rsplit()[10] to [5] I get Ip=PPPoE"
The text from telnet is:
for l in range(len(buf)): if buf[l].find("PPPoE") >= 0: ip = buf[l].strip().rsplit()[10]
The text from telnet is:
WAN Menu 1. Configure 2. Delete 3. Show 4. Exit / WAN -> 3 VCC Con. Catego. Service Interface Proto. IGMP QoS State Status IP ID Name Name address 0.8.35 1 UBR br_8_35 nas_8_35 Bridge N/A Enable Enable Up 0.8.35 2 UBR pppoe_8_35_2 ppp_8_35_2 PPPoE Disable Enable Enable Up 87.220.69.103 Hit <enter> to continue

12
To get the ip address in a string likeyou should do:Tell me if you have any other problem.
0.8.35 2 UBR pppoe_8_35_2 ppp_8_35_2 PPPoE Disable Enable Enable Up 87.220.69.103
ip = buf[l].strip().split()[-1]

0
Thank you very much. Now it's working perfectly.
Answer the question
Top Users
- dail (12)
- livin52 (3)
- camu (3)
- softweb (2)
- Nadine (1)
- Josware (1)
- lfelipecr (1)
- gregoriohc (1)
- Mitu (1)
- ryan714 (1)