如何搜索我返回的文件

0 投票
2 回答
1833 浏览
提问于 2025-04-17 00:07

在下面的代码中,我使用Paramiko这个库来远程登录到一个嵌入式网站服务器,然后获取所有的.log和.txt文件,并把它们放到我本地机器上的一个文件夹里,以便搜索可能明文显示的密码。第二段代码是一个脚本的一部分,可以解压.tgz文件,并在ascii、hex等格式中进行字符串搜索。我发现远程获取文件的成本太高,觉得在登录时直接在嵌入式设备上搜索所有.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()

2 个回答

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

根据我在这个页面上看到的,当你想要执行多个命令时,每个命令都必须在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或其他类型的账户来进行实际的测试。

.

我对你在命令定义中写的'find'感到困惑。
我在SSH2和SFTP命令的列表中没有看到这个命令。
'find'对应的是什么呢?

撰写回答