Python 获取套接字。错误:[Errno-2]名称或服务未知
我有一项任务是从许多Cisco设备获取信息,根据下面的脚本,我试图远程登录到路由器,但每次都会收到Python 获取套接字。错误:[Errno-2]名称或服务未知,python,python-3.x,telnetlib,Python,Python 3.x,Telnetlib,我有一项任务是从许多Cisco设备获取信息,根据下面的脚本,我试图远程登录到路由器,但每次都会收到socket.gaierroerror import telnetlib import os cwd = os.getcwd () # Get the current working directory (cwd) files = os.listdir (cwd) # Get all the files in that directory print ("Files in %r: %s
socket.gaierro
error
import telnetlib
import os
cwd = os.getcwd () # Get the current working directory (cwd)
files = os.listdir (cwd) # Get all the files in that directory
print ("Files in %r: %s" % (cwd, files))
with open ('/home/user1/Ahmed_Yousry/telnet_ip.txt') as f:
Devices = f.read ().splitlines ()
username = []
password = []
for device in Devices:
print ('connecting to ' + HOST)
HOST = device
try:
username = "xxxxx"
password = "xxxxx"
except:
try:
username = "xxxxx"
password = "xxxxx"
except:
try:
username = "xxxxx"
password = "xxxxx"
except:
print ('Unable to connect!' + HOST)
continue
tn = telnetlib.Telnet (HOST)
tn.read_until (b"username: ")
tn.write (username.encode ('ascii') + b"\n")
if password:
tn.read_until (b"password: ")
tn.write (password.encode ('ascii') + b"\n")
output = tn.write (b"show run | i username\n")
print (output)
socket.gaierror: [Errno -2] Name or service not known