Giter Club home page Giter Club logo

Comments (16)

Luck019 avatar Luck019 commented on August 16, 2024 1

Try using
HOST = line.strip()

It is working for me

from pythonvideos.

dazzy099 avatar dazzy099 commented on August 16, 2024

@davidbombal I am getting the similar problem and error when running the for loop script using myswitch file any suggestion ?

Traceback (most recent call last):
File "diffip.py", line 13, in
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

from pythonvideos.

sudharshans-s avatar sudharshans-s commented on August 16, 2024

Port timeout is happening, can you normally telnet to the switches.

from pythonvideos.

Mandeep4code avatar Mandeep4code commented on August 16, 2024

Hello,

I am new to Python coding and was following David's video on this script.
Even I am getting the same error.

Tried changing the HOST = line.strip() but in vain.

Below is my code:

import getpass
import sys
import telnetlib
import socket

fd = open("C:\Users\user\Desktop\PYTHON\HOME\IP_List.txt")

for line in fd:
user = "mandeep"
password = "cisco"
print "Configuring Routers: " + (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("term len 0\n")
tn.write("sh ip int brief \n")
tn.write("exit\n")

print tn.read_all()
#print (output)

#with open("Health.csv", "wb") as f:
#f.write(output)

from pythonvideos.

DilrajSK avatar DilrajSK commented on August 16, 2024

@Mandeep4code Check if it working with a single Host first.

from pythonvideos.

inggreg avatar inggreg commented on August 16, 2024

Hello, Guys
Try using this form:

It is working for me.

   import getpass
   import telnetlib
   import sys

   user = raw_input("Enter your Telnet Username ")
   password = getpass.getpass()

   f = open('myrouters')
   all_ips = [x.rstrip() for x in f] 
   for line in all_ips:

	print ("Configuring Switch " + (line))
	HOST = line
	tn = telnetlib.Telnet(HOST)
	tn.read_until("Username: ")
	tn.write(user.encode('ascii') + "\n")

	if password:
		tn.read_until("Password: ")
		tn.write(password.encode('ascii') + "\n")

	tn.write("config 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("wr\n")
	tn.write("exit\n")
	print(tn.read_all().decode('ascii'))

from pythonvideos.

raajitengg avatar raajitengg commented on August 16, 2024

I tried everything posted here still same error as martinhoeegh and dazzy099. I tried to manually telnet and it works. Even if I use the same code and with single ip address on the code without using the file open it works fine.
Can someone please help me

from pythonvideos.

SejoRikel avatar SejoRikel commented on August 16, 2024

try HOST=line.strip() it worked for me

from pythonvideos.

str8outtajerzy avatar str8outtajerzy commented on August 16, 2024

Has anyone ever figured this out? I am having the same problem.

from pythonvideos.

arezazadeh avatar arezazadeh commented on August 16, 2024

i had the same issue,

HOST = line.strip() works

from pythonvideos.

arezazadeh avatar arezazadeh commented on August 16, 2024

Has anyone ever figured this out? I am having the same problem.

HOST = line.strip() works

from pythonvideos.

arezazadeh avatar arezazadeh commented on August 16, 2024

does anyone know why we have to use .strip() to get it working?

from pythonvideos.

sagarCCIE avatar sagarCCIE commented on August 16, 2024

To some one who landed to this page like how I did for the code that I was facing the issue.
In short :
HOST = line.strip() works

Here is the right script that works:
#!/usr/bin/env python

import getpass
import sys
import telnetlib

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

f = open ('myswitches')

for line in f:
print "Configuring Switch " + (line)
HOST = line.strip()
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 (2,26):
    tn.write("vlan " + str(n) + "\n")
    tn.write("name Python_VLAN_" + str(n) + "\n")

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

print tn.read_all()

In case of further queries please write to me at [email protected]
Youtube channel for Network automations - https://www.youtube.com/playlist?list=PLtFe0r3PejgnDmMTbFupLxlXTMkFtZGwh

from pythonvideos.

xxCRONOxx avatar xxCRONOxx commented on August 16, 2024

Tried line.strip () and received the same error, copied the exact code from sagar and tested and same error

Traceback (most recent call last):
File "./teststuff", line 15, in
tn = telnetlib.Telnet(HOST)
File "/usr/lib/python2.7/telnetlib.py", line 211, in init
self.open(host, port, timeout)
File "/usr/lib/python2.7/telnetlib.py", line 227, in open
self.sock = socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -3] Temporary failure in name resolution
root@UbuntuDockerGuest-1:~#

from pythonvideos.

arezazadeh avatar arezazadeh commented on August 16, 2024

Have you tried with a single host first?

from pythonvideos.

mirosein avatar mirosein commented on August 16, 2024

Try using
HOST = line.strip()

It is working for me

yes, thank you
it works \m/

from pythonvideos.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.