5

I have already written a which has a main method in python which has many operations to create a file. Now i want to SFTP that file and send it to an remote server from an local server. Can I anyone please assist me how to add an method in python which will first execute the main method and then execute the paramiko method for SFTP purpose. Please teach me a simple code for this. I am very much confused!

Helen
  • 69
  • 1
  • 8
  • No actually i have to connect & do SFTP using the public key and private key. Public key is already installed in the remote server just have to put the file to the remote server. – Helen Aug 27 '20 at 09:19
  • You actually have two separate questions. 1) Connecting to SFTP server using public key authentication. 2) Uploading a file to SFTP server. – The 2) is already answered by the question above (and the two answers here). If you want to ask about 1), ask a specific question about that. And do not complicate the question by the upload part. That only makes you get wrong answers. – Martin Prikryl Aug 27 '20 at 09:28

2 Answers2

1
import paramiko


def upload_file(remote_server, ssh_user, ssh_password, local_filepath, remote_path, port_number=22):
    sftp = None
    transport = None

    try:
        # Create transport instance and setup SFTP connection
        transport = paramiko.Transport((remote_server, port_number))
        transport.connect(None, ssh_user, ssh_password)
        sftp = paramiko.SFTPClient.from_transport(transport)

        # Upload file to remote destination
        sftp.put(local_filepath, remote_path)
    except Exception as e:
        print(f'Failed to transfer files: {e}')
        if sftp:
            sftp.close()
        if transport:
            transport.close()

Sample Invocation

upload_file("10.197.15.1", "ssh_user", "ssh_password", "/tmp/myfile.txt", "/home/user/sftp/myfile.txt")
Swadhikar
  • 2,152
  • 1
  • 19
  • 32
0

Below is example of writing content to a file and sftp the same to remote server

import paramiko
import os
paramiko.util.log_to_file("paramiko.log")

# Open a transport
def sftp_transport():
    host,port = "example.com",22
    transport = paramiko.Transport((host,port))

    # Auth
    username,password = "foo","foo"
    transport.connect(None,username,password)

    # Go!
    sftp = paramiko.SFTPClient.from_transport(transport)

    # Upload
    filepath = "/home/foo.jpg"
    localpath = os.path.join('C:\\', 'Users', 'example', 'desktop','example.txt')
    sftp.put(localpath,filepath)

    # Close
    if sftp: sftp.close()
    if transport: transport.close()

def write_file():
    localpath = os.path.join('C:\\', 'Users', 'example', 'desktop','example.txt')
    f = open(localpath, 'w')
    f.write("Now the file has some content!")
    f.close()

write_file()
sftp_transport()
Dhiraj Bansal
  • 417
  • 3
  • 8
  • Could you please help me how to do it by private key public key. Public key is added in the remote server. i want to do sftp using remote server. – Helen Aug 27 '20 at 09:11
  • 2
    @Helen, then sftp can be done using ssh client where you can pass you key file as below: k = paramiko.RSAKey.from_private_key_file(keyfile) ssh = paramiko.SSHClient() ssh.connect( hostname = "www.acme.com", username = "ubuntu", pkey = k ) sftp = ssh.open_sftp() – Dhiraj Bansal Aug 27 '20 at 09:24
  • i am getting no such file or directory error when i run the code. It was the error in the private key file path. i have saved the private key in .ppk. Is it necessary to store the key any particular format. – Helen Aug 27 '20 at 11:53
  • Please check the path or filename if these are correct. I am using same thing and not facing no such file or directory. I have also saved and putty keygen always save private key in .ppk file. – Dhiraj Bansal Aug 27 '20 at 13:01