Python Forum

Full Version: Reading a file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
If I have to guess, the problem is the lineterminator at the end of line
try
HOST = line.strip()
(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
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")
(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]