Created
April 10, 2012 16:11
-
-
Save batok/2352501 to your computer and use it in GitHub Desktop.
Paramiko example using private key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import paramiko | |
k = paramiko.RSAKey.from_private_key_file("/Users/whatever/Downloads/mykey.pem") | |
c = paramiko.SSHClient() | |
c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
print "connecting" | |
c.connect( hostname = "www.acme.com", username = "ubuntu", pkey = k ) | |
print "connected" | |
commands = [ "/home/ubuntu/firstscript.sh", "/home/ubuntu/secondscript.sh" ] | |
for command in commands: | |
print "Executing {}".format( command ) | |
stdin , stdout, stderr = c.exec_command(command) | |
print stdout.read() | |
print( "Errors") | |
print stderr.read() | |
c.close() |
Converting the key
resolves the error
ssh-keygen -p -m PEM -f ~/.ssh/id_rsa
thanks.
if your private key is in .ppk, you need convert to .pem.
I resolve this problem with version paramiko-2.10.4
I see this error:
ModuleNotFoundError: No module named 'paramiko'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not working for me, while use the key using ssh client it works
Traceback (most recent call last):
File "/Users/xx/code/compute-ops/scripts/launch_intance_benchmark.py", line 105, in
k = paramiko.RSAKey.from_private_key_file("/Users/xx/.ssh/id_rsa.pem")
File "/Users/xx/code/compute-ops/venv3/lib/python3.7/site-packages/paramiko/pkey.py", line 206, in from_private_key_file
key = cls(filename=filename, password=password)
File "/Users/xx/code/compute-ops/venv3/lib/python3.7/site-packages/paramiko/rsakey.py", line 48, in init
self._from_private_key_file(filename, password)
File "/Users/xx/code/compute-ops/venv3/lib/python3.7/site-packages/paramiko/rsakey.py", line 169, in _from_private_key_file
data = self._read_private_key_file('RSA', filename, password)
File "/Users/xx/code/compute-ops/venv3/lib/python3.7/site-packages/paramiko/pkey.py", line 279, in _read_private_key_file
data = self._read_private_key(tag, f, password)
File "/Users/xx/code/compute-ops/venv3/lib/python3.7/site-packages/paramiko/pkey.py", line 289, in _read_private_key
raise SSHException('not a valid ' + tag + ' private key file')
paramiko.ssh_exception.SSHException: not a valid RSA private key file