Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passwordless SSH using paramiko

Tags:

Issue : I'm a noob with Paramiko, trying to run some commands from a python script (on personal machine) on a remote server. The remote server doesn't need a password to connect to.

For example, if I do root@[IPaddress] on my Mac, I'm successfully able to connect to the remote server via MacbookPro terminal.

However, I'm trying to do this inside a Python script using Paramiko, and no matter what I do, I get an Authentication error or No Authentication methods available.

I went through Paramiko AuthenticationException issue but the answers there are vague for me to implement without significant experience with Paramiko. Help?

This is my code:

import paramiko
import os
from paramiko import SSHClient

#Borrowed from the linked post 
class SSHClient_noauth(SSHClient):
    def _auth(self, username, *args):
        self._transport.auth_none(username)
        return

#How do I implement? 
ssh =  SSHClient()
sshc = SSHClient_noauth()._auth(username="root") #Where's the ssh obj passed?
sshc.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshc.connect("10.xxx.xxx.xxx") 
like image 293
Raaj Avatar asked Jan 05 '18 11:01

Raaj


1 Answers

Well, not to let the negative vote bog me down.

Tried doing sshc.connect(remoteIP, username=username, password="") and it worked. In case someone has been stuck for over an hour or two trying to get this working, especially for work, you might want to try putting in a "" instead of None.

like image 65
Raaj Avatar answered Sep 20 '22 12:09

Raaj