简体   繁体   English

Cisco 设备上的扩展 ping

[英]Extended ping on Cisco Device

I'm newbie in Python.我是 Python 新手。 I try make a script to perform automatic "extended ping".我尝试制作一个脚本来执行自动“扩展 ping”。

The manual cisco flow is:手动思科流程是:

    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####################

I try to use the command: "net_connect.send_command" from Netmiko and doesn't work.我尝试使用来自 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'

can Someone help me?.有人能帮我吗?。 if another method exist please shared me.如果存在另一种方法,请与我分享。

Thanks a lot!非常感谢!

I haven't used that library, so I'm not sure how it works, I'm using paramiko or telnetlib, depending on the available service on the device.我没有使用过那个库,所以我不确定它是如何工作的,我使用的是 paramiko 或 telnetlib,具体取决于设备上的可用服务。
My ping command on Cisco looks something like this:我在 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

For example by calling ping("8.8.8.8", 3, 128, 1, 2, "86.68.86.68") will end up running ping 8.8.8.8 repeat 3 size 128 timeout 2 source 86.68.86.68 validate on the device.例如,通过调用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在设备上。

Side note : instead of calling ping with no arguments and wait for the prompts, try adding "?"旁注:不要不带参数调用 ping 并等待提示,而是尝试添加“?” at end of the line ( ping ? ) in order to discover the available options, pretty much as the bash-completion works with Tab .在行尾( ping ? )以发现可用选项,就像 bash-completion 与Tab 一起工作一样 I mean, from what I've seen on the devices I've worked with, you don't have to follow the flow, you should be able to execute the ping with one single command invocation.我的意思是,从我在我使用过的设备上看到的情况来看,您不必遵循流程,您应该能够通过一个命令调用来执行 ping。

I've taken look at the library you are using, I've noticed that the send_command accepts an argument expect_string you could use it to detect the new/different prompt, I think that your code should be something like this:我看过你正在使用的库,我注意到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])

I've added all the defaults to the list of commands to send.我已将所有默认值添加到要发送的命令列表中。 If you don't want to send them, replace them with "" (empty strings).如果您不想发送它们,请将它们替换为 ""(空字符串)。

I solved the issue and I share with you.我解决了这个问题,我和你分享。

   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]

Best regards此致

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM