简体   繁体   English

python telnetlib.telnet() 未使用变量连接

[英]python telnetlib.telnet() not connecting using variables

I am working on a script to pull configs from Cisco devices in GNS3.我正在编写一个脚本来从 GNS3 中的 Cisco 设备中提取配置。 The script should be looping through a text file and slicing the IP & Port from each line into variables.该脚本应该循环通过一个文本文件并将 IP & Port 从每一行分割成变量。 These variable are then fed into the telnet connection as the IP & Port parameters of Telnetlib .然后将这些变量作为 Telnetlib 的Telnetlib和端口参数输入 telnet 连接。

import telnetlib

#Open file with list of switches
f = open ("C:\ProgramData\ports.txt")

#Telnet to each switch and configure it
for line in f:
    linePort = line[10:]
    lineIP = line[:-6]
    print "Getting running-config " + lineIP + " " + linePort
    tn = telnetlib.Telnet(lineIP,linePort)

However using the variables always ends up in an error being thrown (see below) but if I hard code the same values I am able to create the connection without issue.但是,使用变量总是会导致抛出错误(见下文),但如果我对相同的值进行硬编码,我可以毫无问题地创建连接。 As it works with a hard coded value I tried forcing a string type with str() on the two variables but it didn't change the result and it still throws the below error.由于它适用于硬编码值,我尝试在两个变量上强制使用str()字符串类型,但它没有改变结果,它仍然抛出以下错误。

C:\Users\SomeUser>python %userprofile%\desktop\config.py
Getting running-config 127.0.0.1  5000

Traceback (most recent call last):
  File "C:\Users\Michael\desktop\config.py", line 11, in <module>
    tn = telnetlib.Telnet(lineIP,linePort)
  File "C:\python27amd64\lib\telnetlib.py", line 211, in __init__
    self.open(host, port, timeout)
  File "C:\python27amd64\lib\telnetlib.py", line 227, in open
    self.sock = socket.create_connection((host, port), timeout)
  File "C:\python27amd64\lib\socket.py", line 557, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 10109] getaddrinfo failed

The error being thrown is socket.gaierrr: [Errno 10109] getaddrinfo failed which I have tried looking into but have not been able to find a resolution for that works for me.抛出的错误是socket.gaierrr: [Errno 10109] getaddrinfo failed我曾尝试调查但无法找到适合我的解决方案。 Given that this process is supposed to be automated and in a loop it is vital to get it working with variable.鉴于这个过程应该是自动化的并且在一个循环中,让它与变量一起工作是至关重要的。 As such any help that you all could provide would be much appreciated.因此,您可以提供的任何帮助将不胜感激。

I stumbled across the answer to this in another question and it makes me feel like an idiot for the oversight.我在另一个问题中偶然发现了这个问题的答案,这让我觉得自己像个白痴一样疏忽大意。

When using variables for the parameters of Telnet the port needs to be an integer.当使用Telnet参数的变量时,端口需要是 integer。 As such the solution was to force it by using int(var) and then it connected without issue.因此,解决方案是通过使用int(var)强制它,然后毫无问题地连接。 The now working code is as follows.现在工作的代码如下。

import telnetlib

#Open file with list of switches
f = open ("C:\ProgramData\ports.txt")

#Telnet to each switch and configure it
for line in f:
    linePort = line[10:]
    lineIP = line[:-6]
    print "Getting running-config " + lineIP + " " + linePort
    tn = telnetlib.Telnet(lineIP,int(linePort))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM