开发者

How can search the files that I'm returning below

In this code below, I'm using Paramiko to login remotely to an embedded site server and retrieve all .log and .txt files and place them in a folder on my local machine to search for potential pin # that may be in the clear. In the second piece of code, this is part of a script that can unzip .tgz files and perform searches of strings in ascii, hex, etc.... I find that getting the files remotely is not cost effective, and think it's better to just search for all .log and .txt on the embedded device while logged in. However, I'm still a Python novice and i开发者_运维知识库t took me a long time to come up with the code that I use now. I'm asking for assistance for the sake of time. Can someone tell me how I would use the code below to implement more exec_commands (I already have the code to search - below the 1st code)? I'm just unsure of where and how to implement it. Thanks!

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

Here is the code that I currently use to search log and text files:

 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()


paramiko has inbuilt classes that do exactly what you're trying to do in your first code section.

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)

Can be boiled down to

ftp = ssh.open_sftp()

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

Read their API documentation: http://www.lag.net/paramiko/docs/


According to what I read in this page, when one wants several commands to be executed, each command must be written in exec_command() on a distinct line.
But I don't know if this would work:

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

or

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

I've just discovered Paramiko thanks to your question.
What SSH2 site could I use to run your code ? I have no SFTP or other type account to perform real essays.

.

I am puzzled by the fact you write 'find' in the definition of a command
I didn't see this command among lists of SSH2 and SFTP commands
What 'find' correspond to ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜