The following sample Python script uses different CLI functions to configure and print show commands:
Device# more flash:sample_script.py import sys import cli intf= sys.argv[1:] intf = ''.join(intf[0]) print "\n\n *** Configuring interface %s with 'configurep' function *** \n\n" %intf cli.configurep(["interface loopback55","ip address 10.55.55.55 255.255.255.0","no shut","end"]) print "\n\n *** Configuring interface %s with 'configure' function *** \n\n" cmd='interface %s,logging event link-status ,end' % intf cli.configure(cmd.split(',')) print "\n\n *** Printing show cmd with 'executep' function *** \n\n" cli.executep('show ip interface brief') print "\n\n *** Printing show cmd with 'execute' function *** \n\n" output= cli.execute('show run interface %s' %intf) print (output) print "\n\n *** Configuring interface %s with 'cli' function *** \n\n" cli.cli('config terminal; interface %s; spanning-tree portfast edge default' %intf) print "\n\n *** Printing show cmd with 'clip' function *** \n\n" cli.clip('show run interface %s' %intf) To run a Python script from the Guest Shell, execute the guestshell run python /bootflash/guest-share/script.py command at the device prompt.
The following example shows how to run a Python script from the Guest Shell: Device# guestshell run python /bootflash/guest-share/sample_script.py loop55 *** Configuring interface loop55 with 'configurep' function *** Line 1 SUCCESS: interface loopback55 Line 2 SUCCESS: ip address 10.55.55.55 255.255.255.0 Line 3 SUCCESS: no shut Line 4 SUCCESS: end *** Configuring interface %s with 'configure' function *** *** Printing show cmd with 'executep' function *** Interface IP-Address OK?
Method Status Protocol Vlan1 unassigned YES NVRAM administratively down down GigabitEthernet0/0 192.0.2.1 YES NVRAM up up GigabitEthernet1/0/1 unassigned YES unset down down GigabitEthernet1/0/2 unassigned YES unset down down GigabitEthernet1/0/3 unassigned YES unset down down : : : Te1/1/4 unassigned YES unset down down Shells and Scripting Python Script Programmability Configuration Guide, Cisco IOS XE 17.16.x 138