使用Python Exscript需要为cisco交换机的set_prompt()创建正则表达式

2024-04-19 15:04:26 发布

您现在位置:Python中文网/ 问答频道 /正文

很难弄明白如何用python/exscript编写正则表达式,以便在运行“copy run tftp”时提示与输出相匹配。。。在

例如,提示更改为。。。在

“远程主机[]的地址或名称?”在

然后。。。在

“目标文件名[lab-3560.confg]?”在

我知道在执行命令之前需要设置“set_prompt()”连接执行('copy run tftp')只是不知道正确的语法


Tags: run名称目标远程文件名地址lab语法
1条回答
网友
1楼 · 发布于 2024-04-19 15:04:26

有很多方法可以做到这一点,下面是一个例子:

因此,您只需解析返回的提示/text,下面是一个来自link的小示例:

import pexpect

switch_ip = "10.0.0.1"
switch_un = "user"
switch_pw = "s3cr3t"
switch_port = "Gi2/0/2"
switch_vlan = 300
config = "lab-3560.confg"

child = pexpect.spawn('ssh %s@%s' % (switch_un, switch_ip))
child.logfile = sys.stdout
child.timeout = 4
child.expect('Address or name of remote host []?')
child.sendline(switch_ip)
child.expect('Destination filename [lab-3560.confg]?')
child.sendline(config)

相关问题 更多 >