Python paramiko copy file

Copying files between two remote Linux server via SCP using Python Paramiko

Copy files between TWO remote computers Secure copy to an EC2 instance without password copy file from ssh to local copy from remote to local ssh download file copy from remote server In my case I am able to write code to copy files from local machine to remote using Paramiko.

Copying files between two remote Linux server via SCP using Python Paramiko

My code is running in Server A. I want to ssh to Server B and then copy files to Server C. I want to use password of Server B and Server C and NOT keys.

In my case I am able to write code to copy files from local machine to remote using Paramiko. I looked for many solutions on stackoverflow like below:

import paramiko from scp import SCPClient def createSSHClient(server, port, user, password): client = paramiko.SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(server, port, user, password) return client ssh = createSSHClient(server, port, user, password) scp = SCPClient(ssh.get_transport()) cp.get(r'/nfs_home/appers/xxxx/test2.txt', r'C:\Users\xxxx\Desktop\MR_Test') 

But again files are getting copying from local (where code is run) to remote and not remote to remote.

You cannot use SCPClient class for this.

You have to login to Server B and run scp command-line client there to upload a (local — as of server B) file to Server C.

Читайте также:  Тег IMG

See python paramiko run command.

You will have problems passing password to scp . You better use keys. If you do not want to use keys, you have to use some of the hacks described for example here:

(and in zillions of other similar questions of desperates who insist on not using keys)

Copy files to server ssh Code Example, All Languages >> Shell/Bash >> copy files to server ssh “copy files to server ssh” Code Answer’s. copy file from ssh to local . shell by Bug Killer on Jun 17 2021 Donate Comment

Copying protected files between servers in one line?

I’d like to copy squid.conf from one server to another.

  • The servers don’t talk to each other. I’d like to go through my workstation.
  • Both servers have the file, so it will be overwritten on the target.
  • The files have 600 permission and are owned by root.
  • root login via ssh is disabled ( PermitRootLogin no ).
  • I’d like to do it in one line, if possible, since it will be a part of a setup guide.
ssh source 'tar czpf - -C /etc/squid/ squid.conf' | \ ssh target 'tar xzpf - -C /etc/squid/' 

to copy files between servers and preserve permissions. However, in this case I will get «Permission denied».

I also know I can do this:

ssh -t source 'sudo cat /etc/squid/squid.conf' 

This way the -t allows sudo to ask for the admin password before outputing the content of the file.

The problem is, I don’t know how to combine those techniques into something that will ask for the sudo password on each server, and transfer the file to its destination. Is this possible?

UPDATE : Here’s the best I could come up with:

ssh -t source 'sudo tar czf /tmp/squid.tgz -C /etc/squid squid.conf' && \ ssh source 'cat /tmp/squid.tgz' | \ ssh target 'cat >/tmp/squid.tgz' && \ ssh -t source 'sudo rm /tmp/squid.tgz' && \ ssh -t target \ 'sudo tar xzf /tmp/squid.tgz -C /etc/squid && sudo rm /tmp/squid.tgz' 

Calling this a one-liner seems like a stretch. I think I’ll just break it down to separate steps in the setup guide.

It’s easier to chain ssh with ssh than to chain ssh with sudo. So changing the ssh server configuration is ok, I suggest opening up ssh for root of each server, but only from localhost. You can do this with a Match clause in sshd_config :

PermitRootLogin no Match Host localhost PermitRootLogin yes 

Then you can set up a key-based authentication chain from remote user to local user and from local user to root. You still have an authentication trail so your logs tell you who logged in as root, and the authentication steps are the same as if sudo was involved.

To connect to a server as root, define an alias in ~/.ssh/config like this:

Host server-root HostName server.example.com User root ProxyCommand "ssh server.example.com nc %h %p" 

If you insist on using sudo , I believe you’ll need separate commands, as sudo insists on reading from a terminal (even if it has a ticket for your account)¹, and none of the usual file copying methods (scp, sftp, rsync) support interacting with a remote terminal.

Sticking with ssh and sudo, your proposed commands could be simplified. On each side, if you have sudo set up not to ask a password again, you can run it once to get over with the password requirement and another time to copy the file. (You can’t easily copy the file directly because the password prompt gets in the way.)

ssh -t source 'sudo true' ssh -t target 'sudo true' ssh -t source 'sudo cat squid.conf' | ssh -t target 'sudo tee /etc/squid/squid.conf' 

¹ unless you have NOPASSWD , but then you wouldn’t be asking this.

You can set up sudo to not ask password next way:

user ALL=NOPASSWD:/usr/bin/tee 
ssh source 'sudo cat /test' | ssh target 'sudo tee /test' 

But I recommend to use something like puppet. It’s much better and easier solves your problem with config files distribution.

PS. By the way, if you’ll set up sudo to ask password from user, the string with [sudo] password for user will apear in target file.

Instead of using ssh you can use scp to transfer the file between the servers.

Log into the target server :

Change to the target dir where you want to copy the file.

#scp -r -p -P 22 root@source-ipaddress:/source-path-file-to-copy . 

r — recursive p — Preserves modification times, access times, and modes from the original file

Without changing ssh configuration, you can create two ssh tunnels host->server1 and server2->host through the ssh connection to server2. Connect these two tunnels on the host machine (same port). And run sudo on server2 to retrieve data from connected tunnels on server1 and save them on server2.

ssh -L60000:$:22 -R60000:localhost:60000 -t $ 'sudo bash -c "ssh -p 60000 '$(whoami)'@localhost \"cd /path/to/dir; tar -czf - files\"|tar -C/path/to/target -xzf -"' 

The idea is: 1- to create a local tunnel from your machine to the source machine on port 60000

1b- Create a remote tunnel to reach back to your machine

2- connect to target machine

3- run all as root on target machine for writing

4- connect to the source machine through the tunnel. whoami and on localhost meaning the localhost on the $ machine.

ssh -p 60000 '$(whoami)'@localhost 

5- package the remote file(s) and send it zipped to stdout

cd /path/to/dir; tar -czf - file 

6- receive the package through stdout and extract files accordingly on the /path/to/target directory

Note: You may receive up to 3 sshkey confirmations and 3 password requests. But files will be copied over.

How To Remotely Copy Files Over SSH Without Entering, The next step is to copy the public key file to your remote computer. You can use scp to do this: The destination for your public key is on the remote server, in the following file: ~/.ssh/authorized_keys2. Subsequent public keys can be appended to this file, much like the ~/.ssh/known_hosts file. This means that …

SCP: Sending files between servers using SSH

SCP or Secure Copy is a useful program for sending files between two or more computers over a secure shell (SSH) connection. In this post, I’ll show you how to use SCP to send files from your local computer to a remote, copy files from a remote computer to your local or copy files between two servers.

An SCP command has the following basic structure:

scp [OPTION] [user@src_host:] file1 [user@dest_host"] file2 

Where OPTION refers to the optional flags you can pass to scp such as the following:

  • -P remote host SSH port
  • -p preserve file modification and access times
  • -q quiet
  • -C compress
  • -r copy directories recursively

The colon ( : ) that comes after the host IP address or domain name is important.

Sending a file from the local computer to a remote computer

scp file.txt remote_username@host1.com:/path/to/remote/dir

Copy a file from the remote computer to the local computer

scp remote_username@host1.com:/path/to/file.txt /local/directory

Copy files between TWO remote computers

scp user1@host1.com:/path/to/file.txt user2@host2.com:/path/to/file/location

Secure copy to an EC2 instance without password

scp -i mykey.pem somefile.txt root@my.ec2.id.amazonaws.com:/

Linux — How to copy files over ssh, To use full power of scp you need to go through next steps: Setup public key authentication. Create ssh aliases. Then, for example if you’ll have this ~/.ssh/config: Host test User testuser HostName test-site.com Port 22022 Host prod User produser HostName production-site.com Port 22022. you’ll save yourself from password entry and simplify scp

Copy file from server to local ssh

copy file from ssh to local

scp username@remoteHost:/remote/dir/file.txt /local/dir/

Источник

mariusavram91 / copy_remote_files.py

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 os
import paramiko
paramiko . util . log_to_file ( ‘/tmp/paramiko.log’ )
paramiko . util . load_host_keys ( os . path . expanduser ( ‘~/.ssh/known_hosts’ ))
host = ‘local’
port = 22
username = ‘user’
files = [ ‘file1’ , ‘file2’ , ‘file3’ , ‘file4’ ]
remote_images_path = ‘/remote_path/images/’
local_path = ‘/tmp/’
ssh = paramiko . SSHClient ()
ssh . set_missing_host_key_policy (
paramiko . AutoAddPolicy ())
ssh . connect ( hostname = host , port = port , username = username )
sftp = ssh . open_sftp ()
for file in files :
file_remote = remote_images_path + file
file_local = local_path + file
print file_remote + ‘>>>’ + file_local
sftp . get ( file_remote , file_local )
sftp . close ()
ssh . close ()

Источник

Оцените статью