Asked  7 Months ago    Answers:  5   Viewed   42 times

I'm trying to get started with the Paramiko library, but the library is throwing an exception as soon as I try to connect with the following simple program:

import paramiko
ssh = paramiko.SSHClient()
ssh.connect('127.0.0.1', username='boatzart', password='mypassword')

The error I get is:

Traceback (most recent call last):
File "test.py", line 6, in <module>
ssh.connect('127.0.0.1')
File "build/bdist.macosx-10.7-intel/egg/paramiko/client.py", line 316, in connect
File "build/bdist.macosx-10.7-intel/egg/paramiko/client.py", line 85, in missing_host_key
paramiko.SSHException: Unknown server 127.0.0.1

This occurs no matter which server I try.

 Answers

36

The exception was raised because you are missing a host key, the rather cryptic "Unknown server" is the clue - since the exception was raised from missing_host_key

Try this instead:

import paramiko

paramiko.util.log_to_file('ssh.log') # sets up logging

client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect('127.0.0.1', username=username, password=password)
stdin, stdout, stderr = client.exec_command('ls -l')
Tuesday, June 1, 2021
 
jcubic
answered 7 Months ago
90

Paramiko by default verifies a size of the uploaded file after the upload.

If the file is moved away immediately after upload, the check fails.

To avoid the check, set confirm parameter of SFTPClient.put to False.

sftp_client.put(localpath, remotepath, confirm=False)

I believe the check is redundant anyway, see
How to perform checksums during a SFTP file transfer for data integrity?


For a similar question about pysftp (what is a wrapper around Paramiko), see:
Python pysftp.put raises "No such file" exception although file is uploaded

Friday, July 30, 2021
 
sober
answered 4 Months ago
60

You're on the right track with your second example, but it isn't quite formatted right. You're actually getting a 2 character string there.

SSH.send("x03") should do the trick.

However, I'd probably have used this instead.

SSH.send(chr(3))

Friday, August 6, 2021
 
Justin Charles
answered 4 Months ago
19

Should have used as sshout.read() and rather I used sshout only while printing.

Tuesday, August 17, 2021
 
laukok
answered 4 Months ago
33

So this question has been up for almost 24 hours on 3 different websites and not one true answer was givin related to this. I am kind of shocked. I tried running the script as a subprocess and then piping the output to stdout. It worked:

import subprocess
proc = subprocess.Popen(['python','tail_try.py'],stdout=subprocess.PIPE)
for line in iter(proc.stdout.readline, ''):
        print line

I can now control the output for every line printed proc.stdout.readline.

Such a simple answer really.

Wednesday, August 25, 2021
 
dirigibleplum
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share