Skip to content

switchloopfile.py - Telnet error #1

@martinhoeegh

Description

@martinhoeegh

I'm getting an error when I try to run your script and can not figure out what's going wrong

$ python pythonR1script5.py

Enter your remote account: david
Password: cisco
Configuring Router 192.168.1.101

Traceback (most recent call last):
  File "pythonR1script5.py", line 13, in <module>
    tn = telnetlib.Telnet(HOST)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/telnetlib.py", line 211, in __init__
    self.open(host, port, timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/telnetlib.py", line 227, in open
    self.sock = socket.create_connection((host, port), timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 557, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

The code:

import getpass
import sys
import telnetlib

user = raw_input("Enter your remote account: ")
password = getpass.getpass()

f = open ('myrouters')

for line in f:
    print "Configuring Router " + (line)
    HOST = line
    tn = telnetlib.Telnet(HOST)

    tn.read_until("Username: ")
    tn.write(user + "\n")
    if password:
        tn.read_until("Password: ")
        tn.write(password + "\n")

    tn.write("conf t\n")

    for n in range (0,2):
        tn.write("interface loopback " + str(n) + "\n")
        tn.write("ip address 1.1." + str(n) + ".2 255.255.255.0\n")

    tn.write("end\n")
    tn.write("exit\n")

    print tn.read_all()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions