Question

I have the following call which works (actually raises a paramiko.AuthenticationException, which is fine):

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('example.com',username='root', password="aaa", look_for_keys=False, timeout=5)

I wanted to convert the parameters into a dict:

params = {
    'hostname': 'example.com',
    'port': 22,
    'username': 'root',
    'look_for_keys': False,
    'timeout': 5
    }
ssh.connect(params)

This raises a TypeError: getaddrinfo() argument 1 must be string or None. I checked the arguments to ssh.connect

>>> inspect.getargspec(ssh.connect)
ArgSpec(args=['self', 'hostname', 'port', 'username', 'password', 'pkey', 'key_filename', 'timeout', 'allow_agent', 'look_for_keys', 'compress', 'sock'], varargs=None, keywords=None, defaults=(22, None, None, None, None, None, True, True, False, None))

and my dict looks good to me.

Nevertheless, since the first call is fine and the error is socket related I tried

ssh.connect('example.com', 22, params)

which raises paramiko.SSHException: No authentication methods available. I interpret this as connect not having any password or key to test.

What should I do so that ssh.connect accepts a dict as parameter (or actually - how different should the dict be compared to mine)?

Or is there another pythonic way to pass parameters, short of building the parameters string "manually" (by concatenating strings, which looks awful to me)?

Was it helpful?

Solution

A dictionary can be "unpacked" and be used as though keyword arguments had been declared explicitly by using ** in the function call, eg:

ssh.connect('example.com', 22, **params)

You just have to be careful that position arguments are fulfilled and not duplicated in the dict, eg: the rules for a normal function call...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top