Advertisement
Guest User

Untitled

a guest
May 25th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 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.     log.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. log.debug(" - Executing command to get information from DB.")
  15.  
  16. def get_channel_content_error(chan, contents, error):
  17.     if chan.recv_ready():
  18.         data = chan.recv(1024)
  19.         while data:
  20.             contents += data
  21.             data = chan.recv(1024)
  22.     if chan.recv_stderr_ready():
  23.         error_buff = chan.recv_stderr(1024)
  24.         while error_buff:
  25.             error += error_buff
  26.             error_buff = chan.recv_stderr(1024)
  27.     return contents, error
  28.  
  29. try:
  30.     chan.exec_command(command)
  31.     # To capture Data. Need to read the entire buffer to capture output
  32.     contents = ''
  33.     error = ''
  34.  
  35.     # https: // github.com / paramiko / paramiko / issues / 621
  36.     # SSH can have exit status sent before stdout data. The typical approach in real-time streaming is to check
  37.     # if channel is recv_ready, recv_stderr_ready or exit_status_ready. There are no other documented
  38.     # ways to check the channel has actually finished.
  39.     #
  40.     # Undocumented 'closed' field can be used to check channel is really closed
  41.  
  42.     while not chan.exit_status_ready() or not chan.closed:
  43.         contents, error = get_channel_content_error(chan, contents, error)
  44.  
  45.     # In case we recieved exit_status_ready and channel is closed
  46.     # there still can be buffer data
  47.     contents, error = get_channel_content_error(chan, contents, error)
  48.  
  49.     exist_status = chan.recv_exit_status()
  50. except socket.timeout:
  51.     raise socket.timeout
  52.  
  53. output = contents.split("\n")
  54. error_value = error.split("\n")
  55. if exist_status != 0:
  56.     log.error("Command execution failure:")
  57.     for error_line in error_value:
  58.         log.info("  > " + error_line)
  59.     return None
  60.  
  61. for i, line in enumerate(output):
  62.     line = line.strip()
  63.     # skip last line "XXX rows"
  64.     if not '<|>' in line:
  65.         continue
  66.     columns = line.split('<|>')
  67.     # skip headers and underline
  68.     if i == 0:
  69.         continue
  70.     queryOutput.append(columns)
  71. ssh.close()
  72. log.debug(" - Command executed.")
  73. log.debug(" - Found %s active records." % len(queryOutput))
  74. return queryOutput
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement