Reputation: 3769
I am new in python and trying different stuff.
Currently trying to copy a text file to_copy.txt
from a remote machine with local ip 192.168.1.101
to my current machine.
What i tried from googling does not seem to work.
import paramiko
from scp import SCPClient
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("[email protected]", password="look420")
print("Connected")
scp = SCPClient(ssh.get_transport())
scp.get("/home/testme/target_folder/to_copy.txt")
scp.close()
But, when i run this i get error;
Traceback (most recent call last):
File "/home/uc/Python_Projects/MoveFileAndFolder/move_remote.py", line 7, in <module>
ssh.connect("[email protected]", password="look420")
File "/usr/local/lib/python3.4/dist-packages/paramiko/client.py", line 296, in connect
to_try = list(self._families_and_addresses(hostname, port))
File "/usr/local/lib/python3.4/dist-packages/paramiko/client.py", line 200, in _families_and_addresses
addrinfos = socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
File "/usr/lib/python3.4/socket.py", line 530, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
What i am doing wrong here?
NOTE: Current machine is running Debian Jessie
and the remote machine runs Ubuntu 14.04.4 LTS
Upvotes: 1
Views: 11663
Reputation: 1
Download file resources from a remote server to a local through paramiko
import paramiko
import os
from stat import S_ISDIR as isdir
def down_from_remote(sftp_obj, remote_dir_name, local_dir_name):
"" "download files remotely" ""
remote_file = sftp_obj.stat(remote_dir_name)
if isdir(remote_file.st_mode):
# Folder, can't download directly, need to continue cycling
check_local_dir(local_dir_name)
print('Start downloading folder: ' + remote_dir_name)
for remote_file_name in sftp.listdir(remote_dir_name):
sub_remote = os.path.join(remote_dir_name, remote_file_name)
sub_remote = sub_remote.replace('\\', '/')
sub_local = os.path.join(local_dir_name, remote_file_name)
sub_local = sub_local.replace('\\', '/')
down_from_remote(sftp_obj, sub_remote, sub_local)
else:
# Files, downloading directly
print('Start downloading file: ' + remote_dir_name)
sftp.get(remote_dir_name, local_dir_name)
def check_local_dir(local_dir_name):
"" "whether the local folder exists, create if it does not exist" ""
if not os.path.exists(local_dir_name):
os.makedirs(local_dir_name)
if __name__ == "__main__":
"" "program main entry" ""
# Server connection information
host_name = 'ipaddress'
user_name = 'username'
password = 'password'
port = 22
# Remote file path (absolute path required)
remote_dir = '/data/nfs/zdlh/pdf/2018/07/31'
# Local file storage path (either absolute or relative)
local_dir = 'file_download/'
# Connect to remote server
t = paramiko.Transport((host_name, port))
t.connect(username=user_name, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
# Remote file start download
down_from_remote(sftp, remote_dir, local_dir)
# Close connection
t.close()
Upvotes: 0
Reputation: 1
"" Download file resources from remote server to local through paramiko author: gxcuizy time: 2018-08-01 """
import paramiko import os from stat import S_ISDIR as isdir
def down_from_remote(sftp_obj, remote_dir_name, local_dir_name): "" "download files remotely" "" remote_file = sftp_obj.stat(remote_dir_name) if isdir(remote_file.st_mode): # Folder, can't download directly, need to continue cycling check_local_dir(local_dir_name) print('Start downloading folder: ' + remote_dir_name)
for remote_file_name in sftp.listdir(remote_dir_name):
sub_remote = os.path.join(remote_dir_name, remote_file_name)
sub_remote = sub_remote.replace('\\', '/')
sub_local = os.path.join(local_dir_name, remote_file_name)
sub_local = sub_local.replace('\\', '/')
down_from_remote(sftp_obj, sub_remote, sub_local)
else:
# Files, downloading directly
print('Start downloading file: ' + remote_dir_name)
sftp.get(remote_dir_name, local_dir_name)
def check_local_dir(local_dir_name): "" "whether the local folder exists, create if it does not exist" "" if not os.path.exists(local_dir_name): os.makedirs(local_dir_name)
if name == "main": "" "program main entry" "" # Server connection information host_name = 'ip' user_name = 'name' password = 'pwd' port = 22 # Remote file path (absolute path required) remote_dir = '/data/nfs/zdlh/pdf/2018/07/31' # Local file storage path (either absolute or relative) local_dir = 'file_download/'
# Connect to remote server
t = paramiko.Transport((host_name, port))
t.connect(username=user_name, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
# Remote file start download
down_from_remote(sftp, remote_dir, local_dir)
# Close connection
t.close()
Upvotes: 0
Reputation: 20349
Did you try
ssh.connect("192.168.1.101", username="testme", password="look420")
Please refer Doc
Upvotes: 2
Reputation: 25331
The port for scp (22) is likely not open on the remote machine. Please check with a command line call to confirm that you can indeed make an ssh or scp connection.
See here for more details
https://help.ubuntu.com/community/SSH/TransferFiles
Upvotes: 1