pythonpython-3.xnetmiko

How to use Netmiko send_command to read an output from a file?


I wrote a small python script that logs into the switch, gets an output from a file stored in the switch. the output that it gets is after running a regex on the contents of the file. when i run the command on the switch linux prompt it takes around 100 seconds to get the output. so i tried to use netmiko + send_command with delay_factor as 10 to store the output on to a variable. But when i used that , it is not producing any output. am i missing anything basic here ? kindly let me know.

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 )

if this is not possible, should i consider downloading the file and analyse it locally? kindly let me know.


Solution

  • ACtually this thread helped me. ..

    Netmiko OSError: Search pattern never detected in send_command:

    in summary, we can either set fast_cli to 'False' or instead of send_command we can use send_command_timing to get such huge output.