cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1711
Views
5
Helpful
6
Replies

Sending special characters to devices using Python scripts

hfakoor222
Spotlight
Spotlight

I'm writing some scripts, and i noticed when sending special characters, namely the dash sign "-", to show commands on Cisco devices I am getting bad outputs.

 

for example:

sh_data_corr = "show data-corruption"
 
try:
corruption = ConnectHandler.send_command(sh_data_corr)
 
 
the output is:
% Invalid input detected at '^' marker.
 
 
This is due to the dash sign. I forget how the commands are sent over the wire to the cisco device. Was it in hex? How should i convert my above command to make it work then?
 
Thanks
 
 
 
 
1 Accepted Solution

Accepted Solutions

Hello,

 Did you check if you are on the right prompt inside the Cisco device?

 This error could happen if you were in 'config t'  even if the command were right.

View solution in original post

6 Replies 6

Hello,

 Did you check if you are on the right prompt inside the Cisco device?

 This error could happen if you were in 'config t'  even if the command were right.

All my commands are show commands, the rest seem to work fine, except the

 

| include   -    symbol 

commands below don't return any output, and 

show data-corruption     returns the error at mark output.
 
 
sh_bfd = "show bfd neighbors"
sh_bootvar = "sh bootvar | include BOOT variable does not exist|CONFIG_FILE does not exist"
# shows if we have boot system image & boot config file saved in memory caches
sh_cap_route = "sh capability feature routing | section Disabled"
# outputs disabled routing features
sh_clock = "show clock" # default cisco clock is 24Hour"
sh_cpu_report = "sh monitor event-trace cpu-report brief back 110:00"
# needs to be configured with process cpu on node or return empty string
sh_data_corr = "show data-corruption" # checks data inconsistency errors
sh_logging = "show logging | include [A-Z]+-[0-4]" #shows log severity 3 or above
#we will extract logging level 0-4 in the last 5 days, let the node parse the regex
sh_mem_failure = "sh memory failures alloc | include \S+.*[0-9]"
#the [0-9] returns any memory failures detected, or an empty string
sh_int_down = "sh ip int brief" # regex 'if err-disabled'
sh_cef = "show cef state"
sh_mem_total = "show memory free totals | include (Processor).*[0-9]" # parses second line of memory cache
 
 
I changed PyCharms global and file encodings to both ASCII and UT-8 and it doesn't seem to solve it.

also sh_mem_total   seems to work as the dash symbol is inside brackets:  [-]

but 

sh_logging = "show logging | include [A-Z]+-[0-4]

returns error at mark output  as dash symbol is outside of brackets. I'm thinking it  might be parsing the - symbol as a range?

I think i saw this or something like it years back with Napalm. In the end i created an alias command on the device. As you noted the other stuff works, and i found work arounds for the pipe command. Dirty trick, but it did work.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

That's really interesting. For now I want to avoid aliases. I might try plain Python IDE instead of Pycharm if I can't find a way for it to work.

 

 

I tried it in the Python console from the system, and same exact issue. The best guess I have on my mind right now is default encodings in windows not doing what i want it to do. Not being able to use special characters takes a lot of usability of of the scripts, so I have to find a workaround

After all that work and yes, I should've been under enable. it is working now.