Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python读取变量_Python_Networking - Fatal编程技术网

Python读取变量

Python读取变量,python,networking,Python,Networking,我有一个python脚本,可以从txt文件(cisco_input.txt)读取交换机主机名,并在每个交换机上运行命令 if __name__ == '__main__': username = raw_input('Enter Your username: ') password = getpass.getpass('Password:') ip_file = "cisco_input.txt" #LOG FILE NAME sys.stdo

我有一个python脚本,可以从txt文件(cisco_input.txt)读取交换机主机名,并在每个交换机上运行命令

if __name__ == '__main__':
    username = raw_input('Enter Your username: ')
    password = getpass.getpass('Password:')


    ip_file = "cisco_input.txt"
        #LOG FILE NAME
    sys.stdout = Logger("cisco_output.txt")
    with open(ip_file) as bf:
        ip_list = bf.read().splitlines()
    for ip in ip_list:

        #START LOOP TO LOGIN TO DEVICES
        # Create instance of SSHClient object
        remote_conn_pre = paramiko.SSHClient()

        # Automatically add untrusted hosts 
        remote_conn_pre.set_missing_host_key_policy(
             paramiko.AutoAddPolicy())

        # initiate SSH connection
        remote_conn_pre.connect(ip, username=username, password=password, look_for_keys=False, allow_agent=False)
        # print "SSH connection established to %s" % ip

        # Use invoke_shell to establish an 'interactive session'
        remote_conn = remote_conn_pre.invoke_shell()
        # print "Interactive SSH session established"

        # Strip the initial router prompt
        output = remote_conn.recv(500)

        # Turn off paging
        disable_paging(remote_conn)

        #COMMANDS TO SEND
        time.sleep(1)
        remote_conn.send("\n")
        remote_conn.send("en\n")
        remote_conn.send(password)
        remote_conn.send("\n")
        time.sleep(2)
        remote_conn.send("show interfaces counters errors | in 1/0/22\n")
        time.sleep(3)
        remote_conn.send("logout\n")
        time.sleep(.5)
        output = remote_conn.recv(15000)
        print output
        #RESTART LOOP
但是,我需要能够读取接口的第二个变量,我不确定如何读取。在这一行中,我需要能够调用接口的第二个变量:

remote_conn.send("show interfaces counters errors | in $INTERFACE\n")
澄清: 当前,脚本读取要在其上运行命令的交换机名称。现在我有一个文本文件,看起来像: 开关1 开关2 开关3

当我需要在其上运行命令的端口保持不变时,这非常有效。但是每个交换机的端口都会发生变化

所以我需要一个输入,比如: 开关1 Gi1/1 开关2 Gi1/15 开关3 Gi1/20

然后我需要能够将接口用作我发送的命令的变量: 远程连接发送(“在$INTERFACE中显示接口计数器错误\n”)


谢谢。

假设您的文件格式如下(csv,逗号分隔值):

然后,您可以使用以下内容在中阅读:

然而,这有一个问题。如果文件很大,一次将其全部加载到内存中可能会有问题。下面的方法抛弃了列表,只是在csv.reader上迭代,一次只加载一行到内存中

with open(file) as bf:
    reader = csv.reader(
    for switch, interface in reader:
        # use switch and interface

我不明白你的问题。您的投入和预期产出是什么?例如,文件内容是什么样子的,所需变量的内容是什么样子的?您的意思是类似于
cmd=“show interfaces…{}\n”。format(switch_name)
?前面设置了开关名称的地方?添加了一些说明。Eric@-交换机名称仅用于SSH到设备中。我需要格式(接口),但不确定如何从文件输入到命令。谢谢Jesse。这非常有帮助
with open(file) as bf:
    reader = csv.reader(bf)
    ip_list = list(reader)

for switch, interface in ip_list:
    # use switch and interface
with open(file) as bf:
    reader = csv.reader(
    for switch, interface in reader:
        # use switch and interface