问题描述
我有一项任务要从许多思科设备获取信息,根据以下脚本,我试图通过telnet到路由器,但是每次都会出现socket.gaierro
错误。
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
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)