Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading a file
#1
I trying connect into different switches using a python script, but I cant read properly the file that contains the ip address of the switches.If someone could help me figure out how I can read this file, I would be glad...Cheers



import sys
import telnetlib
 
user ="teste"
password ="teste"
 
# here is my problem
for line in open('ips.txt'):
    HOST = line
    tn = telnetlib.Telnet(HOST)
    tn.read_until("Username: ")
    tn.write(user + "\n")
    tn.read_until("Password: ")
    tn.write(password + "\n")
 
    tn.write("enable\n")
    tn.write("cisco\n")
    tn.write(" conf t \n")
    tn.write("int loop 0 \n")
    tn.write("int ip add 1.1.1.1 255.255.255.255 \n")
    tn.write("exit \n")
 
#tn.write(" hostname legal" + str(n) + "\n")
print("executado com sucesso")
My file looks like this:
10.0.0.1
10.0.0.2
10.0.0.4
Reply
#2
If I have to guess, the problem is the lineterminator at the end of line
try
HOST = line.strip()
Reply
#3
(Sep-24-2017, 05:25 PM)buran Wrote: If I have to guess, the problem is the lineterminator at the end of line
try
HOST = line.strip()

Thank you,but still something weird,
each ip that I put on the second position on the file, my script can not create a telnet connection.
For example:
10.0.0.1
10.0.0.2
10.0.0.3

The connection is created for 10.0.0.1 and 10.0.0.3

Other example:
10.0.0.2
10.0.0.1
10.0.0.3

The connection will be created for 10.0.0.2 and 10.0.0.3.

But If I printed out,show all the ips in the right sequence... Weird ? :D
Reply
#4
Don't know about your problem, but have an advise to proper handle the file

with open('ips.txt') as f:
    for line in f:
        HOST = line.strip()
        tn = telnetlib.Telnet(HOST)
        tn.read_until("Username: ")
        tn.write(user + "\n")
        tn.read_until("Password: ")
        tn.write(password + "\n")
        tn.write("enable\n")
        tn.write("cisco\n")
        tn.write(" conf t \n")
        tn.write("int loop 0 \n")
        tn.write("int ip add 1.1.1.1 255.255.255.255 \n")
        tn.write("exit \n")
Reply
#5
(Sep-24-2017, 07:13 PM)buran Wrote: Don't know about your problem, but have an advise to proper handle the file

with open('ips.txt') as f:
    for line in f:
        HOST = line.strip()
        tn = telnetlib.Telnet(HOST)
        tn.read_until("Username: ")
        tn.write(user + "\n")
        tn.read_until("Password: ")
        tn.write(password + "\n")
        tn.write("enable\n")
        tn.write("cisco\n")
        tn.write(" conf t \n")
        tn.write("int loop 0 \n")
        tn.write("int ip add 1.1.1.1 255.255.255.255 \n")
        tn.write("exit \n")

I did the change but now it is just getting the last ip address, but I did a counter and printed out the
"HOST" and is showing the 3 ip address, and the counter is starting from 1 until 3...
so the reading is correct.
But Why is just creating one telnet connection ? the last one

I did a debung on two devices, one that is working and other that it is not.
How you can see, both devices show and create a telnet session, but just one is changing the hostname
to "foda".

[Image: t0NAo]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad problems with reading csv file. MassiJames 3 666 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 936 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,132 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 1,122 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 1,011 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 907 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 1,003 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Failing reading a file and cannot exit it... tester_V 8 1,853 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Reading .csv file doug2019 4 1,722 Apr-29-2022, 09:55 PM
Last Post: deanhystad
  Reading an Input File DaveG 1 1,267 Mar-27-2022, 02:08 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020