ktbyers / netmiko_tools

Command line tools built on Netmiko to simplify information gathering
Apache License 2.0
93 stars 32 forks source link

Fails with passwords that use the backslash character #24

Closed awfki closed 7 years ago

awfki commented 7 years ago

In the YAML file there doesn't appear to be a way to include a password with a backslash ().

My current password is not "pass\word" but that's close enough for testing. I tried various combinations of quotes and \ in YAML file and none of them work properly.

File "/Users/dnoonan/.pyenv/versions/3.5.2/lib/python3.5/site-packages/yaml/scanner.py", line 1220, in scan_flow_scalar_non_spaces
    "found unknown escape character %r" % ch, self.get_mark())
yaml.scanner.ScannerError: while scanning a double-quoted scalar
  in ".netmiko.yml", line 6, column 13
found unknown escape character 'w'
  in ".netmiko.yml", line 6, column 19

Additionally it doesn't fail cleanly, that is, there's no error message or timeout. The SSH fails (%%%failed%%% in the tmp file) with no error message and the call to proc.communicate() (line 48 of netmiko-grep) never returns. When I hit control-c it gives this message:

^CTraceback (most recent call last):
  File "./netmiko-grep", line 218, in <module>
    sys.exit(main(sys.argv[1:]))
  File "./netmiko-grep", line 201, in main
    grepx(my_files, pattern, grep_options)
  File "./netmiko-grep", line 68, in grepx
    proc.communicate()
  File "/Users/dnoonan/.pyenv/versions/3.5.2/lib/python3.5/subprocess.py", line 1064, in communicate
    self.wait()
  File "/Users/dnoonan/.pyenv/versions/3.5.2/lib/python3.5/subprocess.py", line 1658, in wait
    (pid, sts) = self._try_wait(0)
  File "/Users/dnoonan/.pyenv/versions/3.5.2/lib/python3.5/subprocess.py", line 1608, in _try_wait
    (pid, sts) = os.waitpid(self.pid, wait_flags)
KeyboardInterrupt

If I insert return 0 at line 48 (in netmiko-grep) right before proc.communicate() then the script finishes with this output:

--------------------
Failed devices:
  PCM-US1P

~/github/netmiko_tools/netmiko_tools $ grep: (standard input): Input/output error
ktbyers commented 7 years ago

@awfki This should work in the YAML file (I just tested it).

password: 'pass\word'

In your comment above, you are probably misinterpreting Python's internal storage of it (i.e. internally Python will store this as 'pass\word' as it has to backslash escape the backslash. To verify it properly do a

print password

and look at it on the screen.

>>> a = r'pass\word'
>>> print a
pass\word
>>> a
'pass\\word'