Question

I'm attempting to login to an Ubuntu server and search logs at several different paths with a function that already works locally (Python 2.7 - win7 machine). Below is the function of how I login and select the logs (also, the basis of my program is Python's cmd module):

def do_siteserver(self, line):
       import paramiko
       paramiko.util.log_to_file('c:\Python27\paramiko-wininst.log')
       host = '10.5.48.65'
       portNum = raw_input("\nEnter a port number: ")
       while True:
           try:
              port = int(portNum)
              break
           except:
              print "\nPort is not valid!!!"
              break                
       transport = paramiko.Transport((host,port))
       while True:
            try:
               passW = raw_input("\nEnter the SiteServer weekly password: ") 
               password = passW
               username = 'gilbert'
               nport = str(port)
               print '\nEstablishing SFTP connection to:  {}:{} ...'.format(host,port)
               transport.connect(username = username, password = password)
               sftp = paramiko.SFTPClient.from_transport(transport)
               print 'Authorization Successful!!!'
               log_names = ("/var/log/apache2/access.log",
                            "/var/log/apache2/error.log",
                            "/var/opt/smartmerch/log/merch_error.log",
                            "/var/opt/smartmerch/log/merch_event.log",
                            "/var/opt/smartmerch/log/server_sync.log")

               #call search function here?

               #for log_file, local_name in log_names.iteritems():
               #     sftp.get(log_file, local_name)
               #sftp.close()
               #transport.close()
               break 
            except:
               print "\nAuthorization Failed!!!"

Here is the function (in the same class) that I want to call:

def do_search(self, line):
    print '\nCurrent dir: '+ os.getcwd()  
    userpath = raw_input("\nPlease enter a path to search (only enter folder   name,   eg. SQA\log): ")  
    directory = os.path.join("c:\\",userpath)
    os.chdir(directory)
    print "\n                               SEARCHES ARE CASE SENSITIVE"
    print " "
    line = "[1]Single File [2]Multiple Files [3]STATIC HEX"
    col1 = line[0:14]
    col2 = line[15:32]
    col3 = line[33:46]
    print "                   " + col1 + "     " + col2 + "     " + col3
    print "\nCurrent Dir: " + os.getcwd()
    searchType = raw_input("\nSelect type of search: ")
       if searchType == '1':  
          logfile = raw_input("\nEnter filename to search (eg. name.log): ")   
          fiLe = open(logfile, "r")
          userString = raw_input("\nEnter a string name to search: ")
          for i,line in enumerate(fiLe.readlines()):
              if userString in line:
                 print "String: " + userString
                 print "File: " + os.join(directory,logfile)
                 print "Line: " + str(i)
                 break
          else:
              print "%s NOT in %s" % (userString, logfile)

          fiLe.close()
       elif searchType =='2':
          print "\nDirectory to be searched: " + directory
          #directory = os.path.join("c:\\","SQA_log")
          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 )))
          choice = raw_input("1: search with respect to whitespace. 2: search ignoring whitespace: ")
          if choice == '1':
               for root,dirname, files in os.walk(directory):
                  for file in files:
                      if file.endswith(".log") or file.endswith(".txt"):
                         f=open(os.path.join(root, file))
                         for i,line in enumerate(f.readlines()):
                             result = regex.search(line)
                             if result:
                                print "\nLine: " + str(i)
                                print "File: " + os.path.join(root,file)
                                print "String Type: " + result.group() + '\n'



                         f.close()
          re.purge()              
          if choice == '2':
             regex2  = re.compile(r'\s+')
             for root,dirname, files in os.walk(directory):
                 for file in files:
                     if file.endswith(".log") or file.endswith(".txt"):
                        f=open(os.path.join(root, file))
                        for i,line in enumerate(f.readlines()):
                            result2 = regex.search(re.sub(regex2, '',line))
                            if result2:
                               print "\nLine: " + str(i)
                               print "File: " + os.path.join(root,file)
                               print "String Type: " + result2.group() + '\n'


                        f.close()  


                        re.purge()

       elif searchType =='3':
          print "\nDirectory to be searched: " + directory
          print " "
          #directory = os.path.join("c:\\","SQA_log")
          regex = re.compile(r'(?:3\d){6}')
          for root,dirname, files in os.walk(directory):
             for file in files:
               if file.endswith(".log") or file.endswith(".txt"):
                  f=open(os.path.join(root,file))
                  for i, line in enumerate(f.readlines()):
                      searchedstr = regex.findall(line)
                      ln = str(i)
                      for word in searchedstr:
                         print "\nString found: " + word
                         print "Line: " + ln
                         print "File: " + os.path.join(root,file)
                         print " "
                         logfile = open('result3.log', 'w')

                  f.close()

          re.purge()
Was it helpful?

Solution

self.do_search(linenumber)

That is all.

OTHER TIPS

Methods are invoked via the object that has them.

self.do_search(...)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top