使用Python 2.7打开用户指定的文件以在Telnet会话中使用

时间:2017-12-28 14:23:06

标签: python python-2.7 network-programming telnet cisco

我尝试编写一些可以与Cisco交换机建立Telnet会话的代码,并为命令使用单独的文本文件。 (交换机位于卫星链路的另一端,因此需要一些延迟)。

我的代码如下:

import telnetlib
import time
import sys

#Open telnet connection to devices
def open_telnet_conn(ip):
    try:
        #Define telnet parameters
        username = 'admin'
        password = 'password'
        telnet_port = 23
        telnet_timeout = 30
        reading_timeout = 30

        #Logging into device
        connection = telnetlib.Telnet(ip, telnet_port, telnet_timeout)

        #Waiting for the username prompt and sending the username
        output = connection.read_until("Username:", reading_timeout)
        connection.write(username + "\n")
        time.sleep(2)

        #Waiting for the password prompt and sending the password
        output = connection.read_until("Password:", reading_timeout)
        connection.write(password + "\n")
        time.sleep(2)   

        #Setting terminal length for entire output - no pagination
        connection.write("terminal length 0\n")
        time.sleep(2)

        #Entering global config mode
        connection.write("\n")
        connection.write("configure terminal\n")
        time.sleep(2)

        #Open user selected file for reading
        selected_cmd_file = open(raw_input('Please enter command file name with extension: '), 'r')

        #Starting from the beginning of the file
        selected_cmd_file.seek(0)

        #Writing each line in the file to the device
        for each_line in selected_cmd_file.readlines():
            connection.write(each_line + '\n')
            time.sleep(2)

        #Closing the file
        selected_cmd_file.close()

        #Test for reading command output
        switch_output = connection.read_very_eager()
        print switch_output

        #Closing the connection
        connection.close()

    except IOError:
        print "Input parameter error! Please check username, password and file name."

#Calling the Telnet function
open_telnet_conn('192.168.0.10')
raw_input('Press enter to exit')
sys.exit('Goodbye')

在输入带扩展名的文件名后,我一直在触及IOError。

这可能与文本文档的位置有关吗?目前,它与应用程序位于同一文件夹中。

我是通过命令提示符在Windows机器上运行它。

---更新29/12/17 ---

所以我设法让python读取文本文件,(我必须更改它所在的目录)。但是现在,一旦脚本运行了来自文本文档(show running-config)的命令,它似乎并没有按照需要收集输出。

这是我从Python终端获得的输出:

  • 请输入扩展名为telnetcommands.txt
  • 的命令文件名
  • SW-SM-01#terminal length 0
  • SW-SM-01#
  • SW-SM-01 #configure terminal
  • 输入配置命令,每行一个。以CNTL / Z结束。
  • SW-SM-01(配置)#END
  • SW-SM-01#
  • SW-SM-01#!
  • SW-SM-01#
  • SW-SM-01 #show run
  • 按enter键退出

我试图让它打印出正在运行的配置。

感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:0)

您能否显示telnetcommands.txt文件的内容?如果它只包含一个命令show running-config,如果您在telnet会话中手动输入这些命令,是否可以确认这些命令是否正常工作?

configure terminal
show running-config

因为某些交换机在配置模式下可能不允许show running-config

<强>更新

看起来您只需要使用expect()功能。

而不是:

    for each_line in selected_cmd_file.readlines():
        connection.write(each_line + '\n')
        time.sleep(2)

尝试使用:

    connection.read_very_eager()    # discard previous output
    for each_line in selected_cmd_file.readlines():
        connection.write(each_line.rstrip() + '\n')   # rstrip() for consistency
        index, match, text = connection.expect(['#'],30)
        time.sleep(1)    # wait for some extra output just in case
        print text + connection.read_very_eager()

在此之后,不需要以下几行:

    switch_output = connection.read_very_eager()
    print switch_output