napalm

using NAPALM to connect to Cisco Router using SSH Key not working


im trying to get netbox to get configs using NAPALM, but even the basic connection using napalm isnt working, always errors around the key file, which we need to use (cannot use password).

napalm==3.4.1
python=3.8

referred to the documentation at https://napalm.readthedocs.io/en/latest/support/ for use of arguments

I can directly SSH to the router using the key in ~/.ssh/ for the user netbox

netbox@netbox:~/.ssh$ ssh -c aes128-cbc netbox@172.1.1.1
The authenticity of host '172.1.1.1 (172.1.1.1)' can't be established.
RSA key fingerprint is SHA256:<fingerprinthere>.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.1.1.1' (RSA) to the list of known hosts.
PHC-SW01>

Prompts for password and fails

napalm --user netbox --vendor ios --optional_args 'use_keys=true, secret=passwordhere' --debug 172.1.1.1 call get_facts

/usr/local/lib/python3.8/dist-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 0.18ubuntu0.18.04.1 is an invalid version and will not be supported in a future release
  warnings.warn(
/usr/local/lib/python3.8/dist-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 0.18ubuntu0.18.04.1 is an invalid version and will not be supported in a future release
  warnings.warn(
Enter password:
2022-08-24 13:03:01,926 - napalm - DEBUG - Starting napalm's debugging tool
2022-08-24 13:03:01,926 - napalm - DEBUG - Gathering napalm packages
2022-08-24 13:03:01,926 - napalm - DEBUG - napalm==3.4.1
2022-08-24 13:03:01,926 - napalm - DEBUG - get_network_driver - Calling with args: ('ios',), {}
2022-08-24 13:03:01,927 - napalm - DEBUG - get_network_driver - Successful
Traceback (most recent call last):
  File "/usr/local/bin/napalm", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/cl_napalm.py", line 308, in main
    run_tests(args)
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/cl_napalm.py", line 269, in run_tests
    optional_args = helpers.parse_optional_args(args.optional_args)
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/helpers.py", line 131, in parse_optional_args
    return {
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/helpers.py", line 132, in <dictcomp>
    x.split("=")[0]: ast.literal_eval(x.split("=")[1])
  File "/usr/lib/python3.8/ast.py", line 99, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib/python3.8/ast.py", line 98, in _convert
    return _convert_signed_num(node)
  File "/usr/lib/python3.8/ast.py", line 75, in _convert_signed_num
    return _convert_num(node)
  File "/usr/lib/python3.8/ast.py", line 66, in _convert_num
    _raise_malformed_node(node)
  File "/usr/lib/python3.8/ast.py", line 63, in _raise_malformed_node
    raise ValueError(f'malformed node or string: {node!r}')
ValueError: malformed node or string: <_ast.Name object at 0x7fa719a35af0>

napalm --user netbox --password '' --vendor ios --optional_args 'use_keys=true, secret=passwordhere' --debug 172.1.1.1 call get_facts

  warnings.warn(
/usr/local/lib/python3.8/dist-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 0.18ubuntu0.18.04.1 is an invalid version and will not be supported in a future release
  warnings.warn(
2022-08-24 13:05:13,330 - napalm - DEBUG - Starting napalm's debugging tool
2022-08-24 13:05:13,330 - napalm - DEBUG - Gathering napalm packages
2022-08-24 13:05:13,331 - napalm - DEBUG - napalm==3.4.1
2022-08-24 13:05:13,331 - napalm - DEBUG - get_network_driver - Calling with args: ('ios',), {}
2022-08-24 13:05:13,331 - napalm - DEBUG - get_network_driver - Successful
Traceback (most recent call last):
  File "/usr/local/bin/napalm", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/cl_napalm.py", line 308, in main
    run_tests(args)
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/cl_napalm.py", line 269, in run_tests
    optional_args = helpers.parse_optional_args(args.optional_args)
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/helpers.py", line 131, in parse_optional_args
    return {
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/helpers.py", line 132, in <dictcomp>
    x.split("=")[0]: ast.literal_eval(x.split("=")[1])
  File "/usr/lib/python3.8/ast.py", line 99, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib/python3.8/ast.py", line 98, in _convert
    return _convert_signed_num(node)
  File "/usr/lib/python3.8/ast.py", line 75, in _convert_signed_num
    return _convert_num(node)
  File "/usr/lib/python3.8/ast.py", line 66, in _convert_num
    _raise_malformed_node(node)
  File "/usr/lib/python3.8/ast.py", line 63, in _raise_malformed_node
    raise ValueError(f'malformed node or string: {node!r}')
ValueError: malformed node or string: <_ast.Name object at 0x7f86f4974b80>

napalm --user netbox --vendor ios --optional_args 'alt_host_keys=True, alt_key_file=/home/netbox/.ssh/id_rsa' --debug 172.1.1.1 call get_facts

/usr/local/lib/python3.8/dist-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 0.18ubuntu0.18.04.1 is an invalid version and will not be supported in a future release
  warnings.warn(
/usr/local/lib/python3.8/dist-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 0.18ubuntu0.18.04.1 is an invalid version and will not be supported in a future release
  warnings.warn(
Enter password:
2022-08-24 13:14:59,741 - napalm - DEBUG - Starting napalm's debugging tool
2022-08-24 13:14:59,741 - napalm - DEBUG - Gathering napalm packages
2022-08-24 13:14:59,741 - napalm - DEBUG - napalm==3.4.1
2022-08-24 13:14:59,741 - napalm - DEBUG - get_network_driver - Calling with args: ('ios',), {}
2022-08-24 13:14:59,742 - napalm - DEBUG - get_network_driver - Successful
Traceback (most recent call last):
  File "/usr/local/bin/napalm", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/cl_napalm.py", line 308, in main
    run_tests(args)
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/cl_napalm.py", line 269, in run_tests
    optional_args = helpers.parse_optional_args(args.optional_args)
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/helpers.py", line 131, in parse_optional_args
    return {
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/helpers.py", line 132, in <dictcomp>
    x.split("=")[0]: ast.literal_eval(x.split("=")[1])
  File "/usr/lib/python3.8/ast.py", line 59, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/lib/python3.8/ast.py", line 47, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    /home/netbox/.ssh/id_rsa
    ^
SyntaxError: invalid syntax

napalm --user netbox --vendor ios --optional_args "hostkey_verify=False, look_for_keys=true, key_file=~/.ssh/id_rsa" --debug 172.1.1.1 call get_facts

/usr/local/lib/python3.8/dist-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 0.18ubuntu0.18.04.1 is an invalid version and will not be supported in a future release
  warnings.warn(
/usr/local/lib/python3.8/dist-packages/pkg_resources/__init__.py:123: PkgResourcesDeprecationWarning: 0.18ubuntu0.18.04.1 is an invalid version and will not be supported in a future release
  warnings.warn(
Enter password:
2022-08-24 13:18:37,500 - napalm - DEBUG - Starting napalm's debugging tool
2022-08-24 13:18:37,501 - napalm - DEBUG - Gathering napalm packages
2022-08-24 13:18:37,501 - napalm - DEBUG - napalm==3.4.1
2022-08-24 13:18:37,501 - napalm - DEBUG - get_network_driver - Calling with args: ('ios',), {}
2022-08-24 13:18:37,501 - napalm - DEBUG - get_network_driver - Successful
Traceback (most recent call last):
  File "/usr/local/bin/napalm", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/cl_napalm.py", line 308, in main
    run_tests(args)
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/cl_napalm.py", line 269, in run_tests
    optional_args = helpers.parse_optional_args(args.optional_args)
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/helpers.py", line 131, in parse_optional_args
    return {
  File "/usr/local/lib/python3.8/dist-packages/napalm/base/clitools/helpers.py", line 132, in <dictcomp>
    x.split("=")[0]: ast.literal_eval(x.split("=")[1])
  File "/usr/lib/python3.8/ast.py", line 99, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib/python3.8/ast.py", line 98, in _convert
    return _convert_signed_num(node)
  File "/usr/lib/python3.8/ast.py", line 75, in _convert_signed_num
    return _convert_num(node)
  File "/usr/lib/python3.8/ast.py", line 66, in _convert_num
    _raise_malformed_node(node)
  File "/usr/lib/python3.8/ast.py", line 63, in _raise_malformed_node
    raise ValueError(f'malformed node or string: {node!r}')
ValueError: malformed node or string: <_ast.Name object at 0x7ff3a5c5e6d0>

If i specify --password '' it doesnt prompt for the password. If i use "alt_key_file" or "key_file" always has the same error: SyntaxError: invalid syntax

running napalm as user 'netbox' accessing the keyfile should work.

Update: tried putting quotes within quotes re: https://github.com/napalm-automation/napalm-base/issues/309#issuecomment-461185980 but didnt work

--optional_args "hostkey_verify=False, look_for_keys=true, key_file='~/.ssh/id_rsa'"

Solution

  • by putting quotes within each variable i was able to get the command to work

    napalm --user netbox --password '' --vendor ios --optional_args "alt_host_keys='True',alt_key_file='/home/netbox/.ssh/id_rsa'" --debug 172.16.1.1 call get_facts

    its failing on the enable after connection, but its connecting using the SSH Key now which is what this was about.