python - How can I get all .log and .txt files when I SSH into a server -
python - How can I get all .log and .txt files when I SSH into a server -
i'm using paramiko module log server (ssh on , sftp on others). can text , log files specific folders on server no problem. there many sub-directories have .txt , .log files. read method not take (*.txt). know way around this. here code i'm using log server , specific log:
import paramiko import sys import os ssh = paramiko.sshclient() ssh.set_missing_host_key_policy(paramiko.autoaddpolicy()) ssh.connect('10.5.48.74', username='root', password='******') ftp = ssh.open_sftp() ftp.get('/var/opt/crindbios/log/crindbios.log', '.') ftp.close()
acquire list of files next script. iterate on list ftp.get
import paramiko import os ssh = paramiko.sshclient() ssh.set_missing_host_key_policy(paramiko.autoaddpolicy()) ssh.connect('localhost',username='****') 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() ftp = ssh.open_sftp() afile in filelist: (head, filename) = os.path.split(afile) print(filename) ftp.get(afile, './'+filename) ftp.close() ssh.close()
it dustyprogrammer proposed: on remote server apply shell commands acquire file list. postprocess list python.
to download have create new filepath each file - download directory proposed doesn't work (for me).
python paramiko
Comments
Post a Comment