Search notes:

Python standard library: netrc

netrc reads and parses files adhering to the netrc format (like ~/.netrc)
import netrc
import os
import paramiko

server_name='milky.way'

netrc_file = os.path.join(os.path.expanduser('~'), '.netrc')
authenticators = netrc.netrc(netrc_file).authenticators(server_name)

if authenticators:
    username, _, password = authenticators

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    ssh.connect(server_name, port=2121, username=username, password=password)

  # do stuff

    ssh.close()
else:
    print(f'Credentials for {server_name} not found in {netrc_file}')

See also

standard library

Index