napalm-automation / napalm-base

Apache License 2.0
33 stars 48 forks source link

napalm --optional_args, ValueError: malformed string #309

Open pierky opened 6 years ago

pierky commented 6 years ago

Hello,

I'm getting "ValueError: malformed string" when using --optional_args to connect to a device via telnet. I'm using the develop branches of both napalm-base (e21f10f5f26b02d15ed0e73fe4e6476638d991e7) and napalm-ios.

I can make it work by removing the ast.literal_eval() call here: https://github.com/napalm-automation/napalm-base/blob/e21f10f5f26b02d15ed0e73fe4e6476638d991e7/napalm_base/clitools/helpers.py#L124-L125

Python 2.7.6:

$ napalm -u user --vendor ios 192.0.2.1 --optional_args "transport=telnet,port=23" call get_arp_table
Enter password:
Traceback (most recent call last):
  File "/home/pierky/.virtualenvs/project/bin/napalm", line 11, in <module>
    sys.exit(main())
  File "/home/pierky/napalm-base/napalm_base/clitools/cl_napalm.py", line 285, in main
    run_tests(args)
  File "/home/pierky/napalm-base/napalm_base/clitools/cl_napalm.py", line 252, in run_tests
    optional_args = helpers.parse_optional_args(args.optional_args)
  File "/home/pierky/napalm-base/napalm_base/clitools/helpers.py", line 125, in parse_optional_args
    for x in optional_args.split(',')}
  File "/home/pierky/napalm-base/napalm_base/clitools/helpers.py", line 125, in <dictcomp>
    for x in optional_args.split(',')}
  File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib/python2.7/ast.py", line 79, in _convert
    raise ValueError('malformed string')
ValueError: malformed string

Python 3.4.0:

$ napalm -u user --vendor ios 192.0.2.1 --optional_args "transport=telnet,port=23" call get_arp_table
Enter password:
Traceback (most recent call last):
  File "/home/pierky/.virtualenvs/project/bin/napalm", line 11, in <module>
    sys.exit(main())
  File "/home/pierky/napalm-base/napalm_base/clitools/cl_napalm.py", line 285, in main
    run_tests(args)
  File "/home/pierky/napalm-base/napalm_base/clitools/cl_napalm.py", line 252, in run_tests
    optional_args = helpers.parse_optional_args(args.optional_args)
  File "/home/pierky/napalm-base/napalm_base/clitools/helpers.py", line 125, in parse_optional_args
    for x in optional_args.split(',')}
  File "/home/pierky/napalm-base/napalm_base/clitools/helpers.py", line 125, in <dictcomp>
    for x in optional_args.split(',')}
  File "/usr/lib/python3.4/ast.py", line 84, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib/python3.4/ast.py", line 83, in _convert
    raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Name object at 0x7fdecf726b70>
dbarrosop commented 6 years ago

Interesting... Could you send a PR, please?

pierky commented 6 years ago

Done #310

ctopher78 commented 5 years ago

Just in case anyone else hits this error while running the napalm cli. There is a simple fix - optional args that are of a string value must be quoted:

This will throw an exception:

napalm --user vagrant --password vagrant --vendor junos --optional_args "ssh_config_file=spine_ssh_config" spine1 call get_bgp_neighbors

This will work:

napalm --user vagrant --password vagrant --vendor junos --optional_args "ssh_config_file='spine_ssh_config'" spine1 call get_bgp_neighbors

Notice that spine_ssh_config is quoted.

As @pierky pointed out, this is related to ast.literal_eval(). Specifically, the literal_eval function is expecting a string that only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. See doc here.

So, in the case of the cli --optional_args flag, it's looking for a string inside a string -- or an int, depending on the arg you are dealing with.

Example:

--optional_args "ssh_config_file='spine_ssh_config'"
--optional_args "port=1234"
ktbyers commented 5 years ago

@ctopher78 FYI, this repository is deprecated. If you have issues or updates, you should make those against the main napalm repository.

https://github.com/napalm-automation/napalm

ctopher78 commented 5 years ago

Thanks @ktbyers. I'll see if this issue exists in that repo and submit a PR.