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

问题描述 投票:0回答:1

我正在尝试编写一些可以与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#端子长度0
  • SW-SM-01#
  • SW-SM-01 #configure terminal
  • 输入配置命令,每行一个。以CNTL / Z结束。
  • SW-SM-01(配置)#结束
  • SW-SM-01#
  • SW-SM-01#!
  • SW-SM-01#
  • SW-SM-01#show run
  • 按enter键退出

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

感谢您提供的任何帮助。

python python-2.7 network-programming telnet cisco
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.