Using keybasd ssh to login to a remote linux box

Hello all,

Pretty new to nornir here. I am trying to login to a remote linux server with a password protected keybased ssh. My current attempt is the following code

from nornir import InitNornir
from nornir.core.deserializer.inventory import InventoryElement
from nornir.plugins.functions.text import print_result
from nornir.plugins.tasks import commands
import json
h = '''                                                                                           
{                                                                                                 
    "arista1": {                                                                                  
        "hostname": "192.168.3.17",                                                               
        "port": 22,                                                                               
        "username": "root",                                                                       
        "platform": "linux",                                                                      
        "connection_options": {                                                                   
            "netmiko": {                                                                          
                "extras": {                                                                       
                    "use_keys": true,                                                             
                    "session_log": "my_out.txt"                                                   
                }                                                                                 
            }                                                                                     
                                                                                                  
        }                                                                                         
    }                                                                                             
}'''
host = json.loads(h)
group = {}
nr = InitNornir(
    core={"num_workers": 100},
    inventory={
        "plugin": "nornir.plugins.inventory.simple.SimpleInventory",
        "options": {
            "hosts": host,
            "groups": group
        }
    }
)
h = nr.inventory.get_hosts_dict()
print (json.dumps(h))
g = nr.inventory.get_groups_dict()
print (json.dumps(g))
# remote_hosts =  nr.inventory.hosts                                                              
remote_hosts = nr.filter(platform="linux")
result = remote_hosts.run(task=commands.remote_command, command="ls -la /tmp")
print_result(result, vars=["stdout"])

Obviously there is some problem with this code, which I am unable to figure out. If I put the password in the host config, it will work for password based login, but I need to get ssh working.

Figured it out!

I need to use getpass() to get the ssh key to unlock the ssh key. I hope this is the recommended way.

Glad you figure it out already. Yes, if you want the user to input the password that’s the recommended way.