Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. queryOutput = []
  2. ssh = paramiko.SSHClient()
  3. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  4. try:
  5.     ssh.connect(theIP,username=theUser,password=thePass)
  6. except:
  7.     print "ERROR: Failed to connect QRadar Console..."
  8.     return None
  9.  
  10. chan = ssh.get_transport().open_session()
  11. chan.settimeout(300)
  12.  
  13. command = 'psql -U qradar -A -F \<\|\> -c "%s"' % query
  14. print "Executing command to get information from DB"
  15.  
  16. try:
  17.     chan.exec_command(command)
  18.     # To capture Data. Need to read the entire buffer to caputure output
  19.     contents = ""   #StringIO.StringIO()
  20.     error = ""      #StringIO.StringIO()
  21.     while not chan.exit_status_ready():
  22.         if chan.recv_ready():
  23.             data = chan.recv(1024)
  24.             while data:
  25.                 contents += data
  26.                 data = chan.recv(1024)
  27.         if chan.recv_stderr_ready():
  28.             error_buff = chan.recv_stderr(1024)
  29.             while error_buff:
  30.                 error += error_buff
  31.                 error_buff = chan.recv_stderr(1024)
  32.     exist_status = chan.recv_exit_status()
  33. except socket.timeout:
  34.     raise socket.timeout
  35. output = contents.split("\n")
  36. error_value = error.split("\n")
  37. if exist_status <> 0:
  38.     print "ERROR: Command execution failure:"
  39.     for error_line in error_value:
  40.         print "  > " + error_line
  41.     return
  42.  
  43. for i, line in enumerate(output):
  44.     line = line.strip()
  45.     columns = line.split('<|>')
  46.     # skip headers and underline
  47.     if i == 0:
  48.         continue
  49.     # skip bad entries
  50.     if len(columns) < 4:
  51.         continue
  52.     queryOutput.append(columns)
  53. ssh.close()
  54. print "Command executed."
  55. print "Found %s active offenses." % len(queryOutput)
  56. return queryOutput
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement