无法将python列表中的第2项(路由器命令)传递给网络设备

问题描述 投票:1回答:1
  1. 我有一个文本文件,其中包含路由器IP(ips.txt)
  2. 我有一个文本文件,其中包含需要传递给路由器的命令(magiccmd.txt)

我打开了文件ips.txt和magiccmd.txt文件,并将字符串转换为列表。所以现在我的IP地址保存为列表'ip_add_list',我的命令保存为列表'magic_list'。

现在使用'for'循环我可以SSH到第一个路由器并执行第一个命令。类似地,脚本生成第二个路由器并执行第一个命令。使用'for'循环,我可以从'ip_add_list'中选择项目。但我只能从列表'magic_list'中选择第一项。它无法从列表'magic_list'中选择第二项

有人能让我知道我的脚本中缺少什么吗?为什么它不从列表中执行第二个命令?

我已经阅读了特定于从.txt文件中选择命令并传递给路由器的不同文章。我可以看到特定于paramiko的文章,但没有找到与'pexpect'相关的内容。

#!/usr/local/bin python3.6
import pexpect
import sys
import time
import re
ssh_un = "remotessh"
def dologinRG(child):
    print ('logging into RG')
    # Enter Password
    child.expect ('password:')
    child.sendline ('test')
    return
def domagiccmd(child):
    print ('issue magic commands')
    for magic_cmds in magic_list:
        print (magic_cmds)
        child.expect ('NOS/')
        child.sendline ('magic')
        child.expect ('NOS/')
        child.sendline (magic_cmds)
        child.expect ('NOS/')
        return      
for ip_string in open("ips.txt"):
    ip_add_list = ip_string.strip().split(',')
    print (ip_add_list)
    for magic_string in open('magiccmd.txt'):
        magic_list = magic_string.strip().split(',')
        print (magic_list)
        for linux_string in open('linuxcmd.txt'):
            linux_list = linux_string.strip().split(',')
for wan_ip in ip_add_list:
    print (wan_ip)
    child = pexpect.spawnu ('ssh %s@%s' % (ssh_un, wan_ip), logfile=sys.stdout, timeout = None)
    dologinRG(child)
    domagiccmd(child)
python-3.x pexpect
1个回答
0
投票

我有这个工作:

import pexpect
import sys
import time
import re

ssh_un = "remotessh"

def dologinRGmagic(child):
    print ('logging into RG')
    prompt = child.expect(['password:', r"yes/no",pexpect.EOF])
    print (prompt)
    if prompt == 0:
        child.sendline ('Alcatel')
        child.expect ('NOS/')
        child.sendline ('magic')
        child.expect ('NOS/')
    elif prompt == 1:
        child.sendline ('yes')
        child.expect ('password:')
        child.sendline ('Alcatel')
        child.expect ('NOS/')
        child.sendline ('magic')
        child.expect ('NOS/')
    return

def dologinRGlinux(child):
    print ('logging into RG linux prompt')
    child.sendline ('!')
    child.expect ('#')
    return

def domagiccmd(child):
    print ('issue magic commands')
    for magic_cmds in magic_list:
        child.sendline (magic_cmds)
        child.expect ('NOS/')

def dolinuxcmd(child):
    print ('issue linux commands')
    for linux_cmds in linux_list:
        child.sendline (linux_cmds)
        child.expect ('#')

for ip_string in open("ips.txt"):
    ip_add_list = ip_string.strip().split(',')
    print (ip_add_list)
    for magic_string in open('magiccmd.txt'):
        magic_list = magic_string.strip().split(',')
        print (magic_list)
        for linux_string in open('linuxcmd.txt'):
            linux_list = linux_string.strip().split(',')
            print (linux_list)

for wan_ip in ip_add_list:
    print ('logging into RG', wan_ip)
    child = pexpect.spawnu ('ssh %s@%s' % (ssh_un, wan_ip),
                            logfile = open('/tmp/rglogs.txt','a'),
                            timeout = None)
    dologinRGmagic(child)
    domagiccmd(child)
    dologinRGlinux(child)
    dolinuxcmd(child)
© www.soinside.com 2019 - 2024. All rights reserved.