4
votes

I am able to ssh to a Linux box B from Linux box A using the command

ssh [email protected] 

This does not require a password for the connection. I am trying to automate the same with Python Paramiko. Here is the code

import os
import paramiko
ssh_client =paramiko.SSHClient()
ssh_client.connect(
    hostname="myhost.com",
    username="jagan",
    password=None,
    look_for_keys=False
)

It gives the following error:

/lib/python3.6/site-packages/paramiko/client.py in connect(self, hostname, port, username, password, pkey, key_filename, timeout, allow_agent, look_for_keys, compress, sock, gss_auth, gss_kex, gss_deleg_creds, gss_host, banner_timeout, auth_timeout, gss_trust_dns, passphrase)
    422             username, password, pkey, key_filenames, allow_agent,
    423             look_for_keys, gss_auth, gss_kex, gss_deleg_creds, t.gss_host,
--> 424             passphrase,
    425         )
    426 

/Application/DataScience/Anaconda/anaconda3/envs/hub/lib/python3.6/site-packages/paramiko/client.py in _auth(self, username, password, pkey, key_filenames, allow_agent, look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host, passphrase)
    713         if saved_exception is not None:
    714             raise saved_exception
--> 715         raise SSHException('No authentication methods available')
    716 
    717     def _log(self, level, msg):

SSHException: No authentication methods available

Is this the right way to connect to a server without passphrase or anything is missing


The authentication mechanism is host based one. In the /etc/ssh/ssh_config:

HostbasedAuthentication yes
EnableSSHKeySign yes setting

I believe that is the reason it is not asking for password.

2

2 Answers

0
votes

Paramiko does not support SSH "hostbased" authentication.

Consider switching to a regular public key authentication.

Note that some consider "IP-based authentication" (= authentication that does not require anything from the client) to be the SSH "hostbased" authentication. It's not. For "IP-based" authentication" you do not need any setup on the client side.

-1
votes

Had a similar issue, as I wanted to perform a secured copy (scp) using the transport layer of the paramiko ssh.

Took me hours till I found a solution from the documentation of scp python lib, and I managed to connect to a host that uses no key and no password. Maybe this transport layer can also be used on for rhe SSH client. Follows my example code:

import paramiko
from scp import SCPClient
import sys

def transfer_image(source, destination):
    def progress(file, size, transferred):
        sys.stdout.flush()
        sys.stdout.write("\rTransferred {0:.2f}Mb of {1:.2f} Mb total. {2:.2f}%"
              .format(transferred / 1048576, size / 1048576, transferred * 100 / size))

    hostname = '192.168.1.100'
    username = "root"
    port = 22

    try:
        print('Trying to connect...')

        t = paramiko.Transport((hostname, port))
        t.connect()
        t.auth_none(username)

        scp = SCPClient(t, progress=progress)
        print('Connected, sending file {}'.format(source))
        scp.put(source, destination)

        scp.close()
    except:
        print('Error transferring files.')