This question already has answers here:
How to ssh connect through Python Paramiko with ppk public key
(4 answers)
Closed 2 years ago.
I am trying to create a python script with Paramiko Lib to upload a file on sftp which uses a "ppk" file and a passphrase to connect.
Unfortunately I cant crack the document or found anything which can connect sftp with ppk files.
Additional details:
SFTP can manually be connected with Filezilla, WinSCP is not allowing it.
Here is the code I can go upto only. Please help!
k = paramiko.RSAKey.from_private_key_file("/key.ppk")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect( hostname = "ftp.example.com", username = "user", pkey = k,passphrase="somephrase" )
Well that's the least of the problems, I need to upload afterwards when it gets connected.
i suggest that you convert .ppk to .pem !
see :
Conver ppk to pem
then like this :
import paramiko
k = paramiko.RSAKey.from_private_key_file("mykey.pem")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print "connecting"
c.connect( hostname = "www.host.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()
Related
This question already has answers here:
Multi-factor authentication (password and key) with Paramiko
(3 answers)
Closed last year.
Trying to use pysftp to pull files from an sFTP server that requires both ssh key & password for authentication without much luck. Can use pysftp with just key and just password, but it breaks when I attempt to use both. Hoping someone has some experience with this. Open to using a different library if that works better.
Error output is:
paramiko.ssh_exception.BadAuthenticationType: Bad authentication type; allowed types: ['publickey']
import pysftp
connection_host = '1.1.1.1'
connection_user = 'username'
connection_password = 'password'
connection_private_key = '/path/to/key'
connection_dir='/dir/on/remote/host'
with pysftp.Connection(host=connection_host, username=connection_user, password=connection_password, private_key=connection_private_key) as sftp:
files = sftp.listdir(remotepath=connection_dir)
for file in files:
print("found the following file: {}".format(file))
with sftp.cd(connection_dir):
sftp.get(file)
Working code if anyone runs into the same issue:
connection_host = '0.0.0.0'
connection_user = 'username'
connection_password = 'password'
connection_private_key = '/path/to/key.pem'
connection_dir='/remote/path'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh.connect(connection_host, username=connection_user, password=connection_password, key_filename=connection_private_key)
sftp_client = ssh.open_sftp()
files = sftp_client.listdir(connection_dir)
sftp_client.chdir(connection_dir)
for file in files:
print("found the following file {}".format(file))
sftp_client.get(file,file)
if sftp_client:
sftp_client.close()
if ssh:
ssh.close()
It seems very simple, but I searched multiple resources but could not find an answer on how to change a remote Linux system password using Python and with SFTP.
def changepwd():
sftp_client = ssh.open_sftp()
#change password of root on remote server
Are there any built-in modules that I can use to change the password?
Thanks in advance.
Thanks for all you help. This is how I changed the passwd for 'root'.
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username=Name, password=Pwd)
print "Connection succesfully established ...with %s " % hostname
stdin, stdout, stderr = ssh.exec_command('echo -e "newpasswd\newPasswd" | passwd')
print "stderr: ", stderr.readlines()
print "pwd: ", stdout.readlines()
ssh.close()
You cannot change password with SFTP protocol.
You can change password with SSH protocol. But the SSH protocol API for changing a password is not support by the most widespread SSH server – OpenSSH. Nor it is supported by the most widespread Python SSH library – Paramiko. So this most likely won't work for you anyway.
So in the end the only viable option is to execute a relevant shell command (passwd or chpasswd) via SSH (e.g. using Paramiko).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need get the list of files from a remote host directory, running the code in my local machine.
Is something like os.listdir() at remote host machine, NOT is os.lisdir() in the local machine that runs the python code.
In bash this command works
ssh user#host "find /remote/path/ -name "pattern*" -mmin -15" > /local/path/last_files.txt
Your best option for running commands on a remote machine is via ssh with paramiko.
A couple of examples of how to use the library and issue a command to the remote system:
import base64
import paramiko
# Let's assign an RSA SSH key to the 'key' variable
key = paramiko.RSAKey(data=base64.b64decode(b'AAA...'))
# And create a client instance.
client = paramiko.SSHClient()
# Create an object to store our key
host_keys = client.get_host_keys()
# Add our key to 'host_keys'
host_keys.add('ssh.example.com', 'ssh-rsa', key)
# Connect to our client; you will need
# to know/use for the remote account:
#
# IP/Hostname of target
# A username
# A password
client.connect('IP_HOSTNAME', username='THE_USER', password='THE_PASSWORD')
# Assign our input, output and error variables to
# to a command we will be issuing to the remote
# system
stdin, stdout, stderr = client.exec_command(
'find /path/data/ -name "pattern*" -mmin -15'
)
# We iterate over stdout
for line in stdout:
print('... ' + line.strip('\n'))
# And finally we close the connection to our client
client.close()
As pointed out by the OP, if we already have a known hosts file locally we can do things slightly different:
import base64
import paramiko
# And create a client instance.
client = paramiko.SSHClient()
# Create a 'host_keys' object and load
# our local known hosts
host_keys = client.load_system_host_keys()
# Connect to our client; you will need
# to know/use for the remote account:
#
# IP/Hostname of target
# A username
# A password
client.connect('IP_HOSTNAME', username='THE_USER', password='THE_PASSWORD')
# Assign our input, output and error variables to
# to a command we will be issuing to the remote
# system
stdin, stdout, stderr = client.exec_command(
'find /path/data/ -name "pattern*" -mmin -15'
)
# We iterate over stdout
for line in stdout:
print('... ' + line.strip('\n'))
# And finally we close the connection to our client
client.close()
This question already has answers here:
Environment variable differences when using Paramiko
(2 answers)
Closed 3 years ago.
On Linux Machine A, i have a script running which is supposed to ssh into Linux Machine B, sync the p4 workspace on B and then execute a piece of code.
I've set up Perforce on B and I'm able to manually execute p4 client and other commands on B and they work.
However, when i attempt to do the same thing while sshing from Machine A, i get the following error:
Perforce client error:
Connect to server failed; check $P4PORT.
TCP connect to perforce:1666 failed.
Temporary failure in name resolution
Here's my piece of code:
def ssh_connect(ip,user,pwd):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=user, password=pwd)
return ssh
def execute_command(device_details, command):
try:
ip = device_details.get('ip')
username = device_details.get('username')
password = device_details.get('password')
ssh_ns_obj = ssh_connect(ip, username, password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh_ns_obj.exec_command(command)
print ssh_stdout.read()
print ssh_stderr.read()
print ssh_stdin.read()
return ssh_stdout.read()
except Exception as e:
print_exception_details()
perforce_sync_command = "cd /root/hello/;chmod -R 444 *;p4 sync ...;chmod -R 755 *"
output = execute_command(device_details, perforce_sync_command)
What am i missing?
Perforce needs a set of environment variables to run - on ssh, the environment variables were not visible on the ssh session.
To solve this, i edited the file /etc/environment and added the P4PORT, P4USER, P4CLIENT, P4PASSWD vars in the file.
I know that Paramiko supports Pageant under Windows, but it doesn't work by default.
I am looking for an example of connecting using the key that is loaded in Pageant.
This is what I am using to connect and do an automated login using Pageant to store my key, and connecting to it from within my Python script. It counts on Pageant already being loaded, (and I haven't found a good reliable way to launch it and load the key (prompt for key password)) but the below works for now.
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
host = 'somehost.com'
port = 22
ssh.connect(host, port=port, username='user', allow_agent=True)
stdin,stdout,stderr = ssh.exec_command("ps -ef")
print stdout.read()
print stderr.read()