Cisco设备上的扩展ping

问题描述 投票:-1回答:2

我是Python的新手。我尝试制作一个脚本来执行自动“扩展ping”。

手动cisco流程是:

    switch#**ping**
    Protocol [ip]:
    Target IP address: **X.X.X.X**
    Repeat count [5]: **1000**
    Datagram size [100]: **1500**
    Timeout in seconds [2]:
    Extended commands [n]:
    Sweep range of sizes [n]:
    ####################Command Start####################

我尝试使用Netmiko的命令:“net_connect.send_command”并不起作用。

    Ping_Extended = [ 'ping','\n','X.X.X.X','1000','1500','\n','\n','\n' ]

    Ping_TASA = net_connect.send_command(Ping_Extended)

    Error: Traceback (most recent call last):
    File "VLAN1.py", line 124, in <module>
    Ping_Extended = Ping_Extended.rstrip()
    AttributeError: 'list' object has no attribute 'rstrip'

有人能帮我吗?。如果存在其他方法,请与我分享。

非常感谢!

python cisco
2个回答
0
投票

我没有使用该库,所以我不确定它是如何工作的,我使用的是paramiko或telnetlib,具体取决于设备上的可用服务。 我对Cisco的ping命令看起来像这样:

def ping(dest, count=5, size=None, interval=None, timeout=None, source=None):
    # ignore the "interval" it's there in order to have the same signature 
    # on all devices, Cisco doesn't accept interval parameter 
    cmd = "ping %s repeat %s" % (dest, count)
    for x in [("size", size), ("timeout", timeout), ("source", source)]:
        if x[1]:
            cmd += " %s %s" % x
    cmd += " validate"
    # the "validate" seemed to be required in order to get extra statistics 
    # run the command, get the output, parse it

例如,通过调用ping("8.8.8.8", 3, 128, 1, 2, "86.68.86.68")将最终在设备上运行ping 8.8.8.8 repeat 3 size 128 timeout 2 source 86.68.86.68 validate

旁注:不要在没有参数的情况下调用ping并等待提示,请尝试添加“?”在行尾(ping ?),以发现可用的选项,就像使用Tab的bash-completion一样。我的意思是,从我在我使用的设备上看到的,你不必遵循流程,你应该能够通过一个命令调用来执行ping。

我已经看过你正在使用的库,我注意到send_command接受了一个参数expect_string你可以用它来检测新的/不同的提示,我认为你的代码应该是这样的:

cmds = ['ping', 'ip', 'X.X.X.X','1000','1500','2','n','n' ]
for cmd in cmd[:-1] : 
    net_connect.send_command(cmd, expect_string='\] ?: ?')
output = net_connect.send_command(cmds[-1])

我已将所有默认值添加到要发送的命令列表中。如果您不想发送它们,请用“”(空字符串)替换它们。


0
投票

我解决了这个问题,并与您分享。

   output = [net_connect.send_command("ping", expect_string='#?'),
             net_connect.send_command("ip", expect_string=':?'),
             net_connect.send_command("192.168.1.254", expect_string=':?'),
             net_connect.send_command("1000", expect_string=':?'),
             net_connect.send_command("1500", expect_string=':?'),
             net_connect.send_command("2", expect_string=':?'),
             net_connect.send_command("n", expect_string=':?'),
             net_connect.send_command("n", expect_string=':?', delay_factor=140)]

print output[-1]

最好的祝福

© www.soinside.com 2019 - 2024. All rights reserved.