Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import paramiko
- import getpass
- import os
- def upload_files_from_local_directory(ssh, local_path, remote_path):
- sftp_client= ssh.open_sftp()
- for file in os.listdir(local_path):
- file_remote = remote_path + file
- file_local = local_path + file
- print(file_local + ">>>" + file_remote)
- sftp_client.put(file_local,file_remote)
- sftp_client.close()
- def download_files_from_remote_directory(ssh, local_path, remote_path):
- sftp_client= ssh.open_sftp()
- for file in os.listdir(local_path):
- file_remote = remote_path + file
- file_local = local_path + file
- print(file_remote + ">>>" + file_local)
- sftp_client.get(file_remote,file_local)
- sftp_client.close()
- def ssh_command(ssh,command):
- stdin, stdout, stderr = ssh.exec_command(command)
- print(stdout.read())
- def ssh_connect(host, user, key):
- try:
- ssh = paramiko.SSHClient()
- print('Calling paramiko')
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect(hostname=host, username=user, password=key)
- return ssh
- except Exception as e:
- print('Connection Failed')
- print(e)
- if __name__=='__main__':
- #CHANGE THESE VARIABLES
- host = "SERVER_IP"
- user = "USER_LOGIN"
- key = "KEY_PWD"
- ssh = ssh_connect(host, user, key)
- ssh_command(ssh,'echo "Connected to remote server"')
- # upload_files_from_local_directory(ssh, "./test.txt", "/tmp")
- # download_files_from_remote_directory(ssh, "./", "file.txt")
- ssh.close()
Add Comment
Please, Sign In to add comment