2011-08-22 24 views
0

在下面的代码中,我使用Paramiko远程登录到嵌入式站点服务器,并检索所有.log和.txt文件,并将它们放置在本地文件夹中机器来搜索潜在的引脚#可能是清晰的。在第二段代码中,这是脚本的一部分,它可以解压缩.tgz文件并以ascii,十六进制等方式执行字符串搜索......我发现远程获取文件不具有成本效益,并且认为它更好只需在登录时搜索嵌入式设备上的所有.log和.txt。但是,我仍然是一名Python新手,并且花了很长时间才想出我现在使用的代码。为了时间的缘故,我寻求帮助。有人能告诉我如何使用下面的代码来实现更多的exec_commands(我已经有代码搜索 - 在第一个代码下面)?我只是不确定在哪里以及如何实施它。谢谢!如何搜索下面我要返回的文件

import paramiko 
import sys 
import os 
import re 



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!!!" 

这里是我目前使用的搜索日志和文本文件中的代码:

print "\nDirectory to be searched: " + directory 
     print "\nFile result2.log will be created in: c:\Temp_log_files." 
     paths = "c:\\Temp_log_files\\result2.log" 
     temp = file(paths, "w") 
     userstring = raw_input("Enter a string name to search: ") 
     userStrHEX = userstring.encode('hex') 
     userStrASCII = ''.join(str(ord(char)) for char in userstring) 
     regex = re.compile(r"(%s|%s|%s)" % (re.escape(userstring), re.escape(userStrHEX), re.escape(userStrASCII))) 
     goby = raw_input("Press Enter to begin search (search ignores whitespace)!\n") 


     for root,dirname, files in os.walk(directory): 
      for file1 in files: 
       if file1.endswith(".log") or file1.endswith(".txt"): 
       f=open(os.path.join(root, file1)) 
       for i,line in enumerate(f.readlines()): 
        result = regex.search(line) 
        if result: 
         ln = str(i) 
         pathnm = os.path.join(root,file1) 

         template = "\nLine: {0}\nFile: {1}\nString Type: {2}\n\n" 
         output = template.format(ln, pathnm, result.group()) 
         print output 
         temp.write(output) 
         break  
       else: 
        print "String Not Found in: " + os.path.join(root,file1) 
        temp.write("\nString Not Found: " + os.path.join(root,file1) + "\n") 



       f.close() 
     re.purge() 
+0

你的问题是你想在远程服务器上执行一个Python脚本吗? –

+0

@Mikko Ohtamaa - 我想你可以这么说。 – suffa

+0

我弄明白了,很快就会发布代码。 – suffa

回答

0

的paramiko具有内置类,做你想在你的第一个代码段,该怎么做。

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 = ssh.open_sftp() 

for files in ftp.listdir(): 
    if os.path.splitext(files) in ('.log','.txt'): 
     print os.path.split(files)[1] 

阅读他们的API文档:http://www.lag.net/paramiko/docs/

+0

我认为你应该阅读API并阅读我正在努力实现的目标......你会发现你在“......被归结为”的限制。 – suffa

0

据我在this page看,当一个人想执行多个命令,每个命令必须写在exec_command()在不同的线上。
但我不知道这是否会工作:

command1 = ............ 
command2 = .......... 
command3 = ............... 
stdin, stdout, stderr = ssh.exec_command('\n'.join((command1,command2,command3))) 

command1 = ............ 
command2 = .......... 
command3 = ............... 
allcomands = '''%s 
%s 
%s''' 
stdin, stdout, stderr = ssh.exec_command(allcommands % (command1,command2,command3))) 

我刚刚发现的paramiko感谢你的问题。
我可以用什么SSH2站点来运行你的代码?我没有SFTP或其他类型的帐户来执行真正的散文。

我被你写在命令
我没有看到SSH2和SFTP的列表中该命令的定义“查找”的事实感到困惑命令
什么“找到”对应?

+0

我正在使用远程服务器(Ubuntu).... – suffa

+0

@ user706808我明白了这个过程是连接到远程PC或服务器。接着 ? – eyquem

+0

这是我通过Python(Paramiko)执行的bash命令,要在Ubuntu服务器上执行。 – suffa