Nexus 3K|9K: Python API - cli() module not running command/providing output
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-30-2019 03:39 PM - edited 04-30-2019 03:41 PM
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?
- Labels:
-
Open Source and Open Standards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2019 02:55 AM
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.
