Skip to content
This repository has been archived by the owner on Sep 17, 2019. It is now read-only.

napalm --optional_args, ValueError: malformed string #309

Open
pierky opened this issue Sep 20, 2017 · 5 comments
Open

napalm --optional_args, ValueError: malformed string #309

pierky opened this issue Sep 20, 2017 · 5 comments

Comments

@pierky
Copy link
Contributor

pierky commented Sep 20, 2017

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 (e21f10f) and napalm-ios.

I can make it work by removing the ast.literal_eval() call here:

return {x.split('=')[0]: ast.literal_eval(x.split('=')[1])
for x in optional_args.split(',')}

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
Copy link
Member

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

pierky added a commit to pierky/napalm-base that referenced this issue Sep 20, 2017
@pierky
Copy link
Contributor Author

pierky commented Sep 20, 2017

Done #310

@ctopher78
Copy link

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
Copy link
Contributor

ktbyers commented Feb 6, 2019

@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
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants