如何使用Netmiko send_command从文件中读取输出



我写了一个小python脚本,该脚本登录到交换机,从存储在交换机中的文件中获取输出。它得到的输出是在对文件内容运行regex之后。当我在switch-linux提示符下运行该命令时,大约需要100秒才能得到输出。所以我尝试使用netmiko + send_commanddelay_factor as 10来将输出存储到一个变量中。但当我使用它时,它并没有产生任何输出。我是不是遗漏了一些基本的东西?请告诉我。

def getSIPsFromFile(log, hdl, device_type):
file_name = 'file_1_' + device_type
print('n *** Value of File name is : {0}  ***n'.format(file_name))
# cmd = "grep -o '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' {0}".format(file_name)
cmd = 'cat {0} | grep '[0-9]{{1,3}}.[0-9]{{1,3}}.[0-9]{{1,3}}.[0-9]{{1,3}}' | awk '{{print $4}}''.format(file_name)
print('n   *** Value of cmd is :  {0} **** n'.format(cmd))
hdl.send_command('start-shell' , expect_string = "$")
hdl.send_command('cd /tmp' , expect_string = "$")
print(' *** Prompt is : {0}  *** '.format(hdl.find_prompt()))
res = hdl.send_command(cmd, expect_string = "$" , delay_factor = 10)
time.sleep(60)
print(res)
vsx_pri_hdl = ConnectHandler( device_type='cisco_nxos', ip=args.vsx_primary_mgmt_ip, 
username=args.username, password=args.password, blocking_timeout=500 )

如果这不可能,我应该考虑下载文件并在本地进行分析吗?请告诉我。

A实际上这个线程帮助了我…

Netmiko OSError:在send_command:中从未检测到搜索模式

总之,我们可以将fastcli设置为"False",也可以使用sendcommand-timing来获得如此巨大的输出,而不是sendcommand。

最新更新