cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1227
Views
0
Helpful
1
Replies

Nexus 3K|9K: Python API - cli() module not running command/providing output

r.guillory
Level 1
Level 1

I am in the process of writing a post validation script utilizing the Pythong API that runs natively on NXOS and am having some issues using the "cli()" module. 

 

This command works perfectly fine when ran from the  nexus CLI and gives me the output I desire:

 

Command:

show cdp neighbors | exclude 'Cap|Switch|VoIP|STP|Tot' | awk '{print $1}' | awk 'BEGIN{ RS = "" ; FS = "\n" }{print $1,"\t"$2,"\n"$3,"\t"$4,"\n"$5,"\t""\t""\t"$6"\n"$7,"\t""\t""\t"$8}'

 

But, when ran from the Python API using the cli() module I am unable to print the results of the command. 

 

Sample Code: 

 

#!/isan/bin/python
import cisco
from cli import *
import re

 

cdp_nei_state = cli("show cdp neighbors | exclude 'Cap|Switch|VoIP|STP|Tot' | awk '{print $1}' | awk 'BEGIN{ RS = ""; FS = "\n" }{print $1,"\t"$2,"\n"$3,"\t"$4,"\n"$5,"\t""\t""\t"$6"\n"$7,"\t""\t""\t"$8}'")

 

print cdp_nei_state

 

Error message:

 

File "/bootflash/cdp_test.py", line 6
cdp_nei_state = cli("show cdp neighbors | exclude 'Cap|Switch|VoIP|STP|Tot' | awk '{print $1}' | awk 'BEGIN{ RS = ""; FS = "\n" }{print $1,"\t"$2,"\n"$3,"\t"$4,"\n"$5,"\t""\t""\t"$6"\n"$7,"\t""\t""\t"$8}'")

SyntaxError: unexpected character after line continuation character

 

Is there a limitation within the cli() module that is prohibiting the command from being ran?

 

 

 

 

 

 

1 Reply 1

Seb Rupik
VIP Alumni
VIP Alumni

Hi there,

You need to escape all those double quotes. Try using this command string:

 

show cdp neighbors | exclude 'Cap|Switch|VoIP|STP|Tot' | awk '{print $1}' | awk 'BEGIN{ RS = \"\"; FS = \"\n\" }{print $1,\"\t\"$2,\"\n\"$3,\"\t\"$4,\"\n\"$5,\"\t\"\"\t\"\"\t\"$6\"\n\"$7,\"\t\"\"\t\"\"\t\"$8}'

...the string should then correctly parse in the interpreter.

 

cheers,

Seb.