I'm trying to retrieve files from an ubuntu server from a win 7 machine with Paramiko module
I'm getting a 'Permission denied' error when I set a local path name of my windows file system with the get method (Paramiko). Has anyone encountered this or know how to resolve it? I am logged in as the administrator. Below is the code and traceback error. Thanks!
Code:
import paramiko
import sys
import os
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('10.5.48.72', username='root', password='******')
apath = '/var/log'
apattern = '"*.log"'
rawcommand = 'find {path} -name {pattern}'
command = rawcommand.format(path=apath, pattern=apattern)
stdin, stdout, stderr = ssh.exec_command(command)
filelist = stdout.read().splitlines()
This is the correct code. It searches an Ubuntu server at the ro开发者_运维技巧ot for all log and text files, and retrieves the files and saves them to a local directory (c:\Extracted):
sim_ip = raw_input('Host: ')
pattern = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
if re.match(pattern, sim_ip):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(sim_ip, username='root', password='*****')
apath = '/'
apattern = '"*.txt" -o -name "*.log"'
rawcommand = 'find {path} -name "*.txt" -o -name "*.log"' #{pattern}
command = rawcommand.format(path=apath, pattern=apattern)
stdin, stdout, stderr = ssh.exec_command(command)
filelist = stdout.read().splitlines()
ftp = ssh.open_sftp()
for afile in filelist:
(head, filename) = os.path.split(afile)
print(filename)
ftp.get(afile, 'c:\\Extracted\\' + filename) #'./'+filename)
ftp.close()
ssh.close()
else:
print "You entered an invalid IP Address!!!"
精彩评论