Guest User

Untitled

a guest
Apr 15th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. #!/usr/bin/python -tt
  2.  
  3. import mechanize
  4. import cookielib
  5. import paramiko
  6. from collections import defaultdict
  7. from BeautifulSoup import BeautifulSoup
  8. import argparse
  9.  
  10.  
  11. # Configuration
  12. jiveCloudUrl="XXX"
  13. jiveCloudLogin="XXX"
  14. jiveCloudPassword="XXXX"
  15. defaultRootPassword="XXX"
  16.  
  17. def JcaLogin():
  18.     br = mechanize.Browser()
  19.     br.set_handle_refresh(False)
  20.     cj = cookielib.LWPCookieJar()
  21.     br.set_cookiejar(cj)
  22.     br.open('%s/login.jspa' % jiveCloudUrl)
  23.     br.select_form(nr=0)
  24.     br.form["contactId"] = jiveCloudLogin
  25.     br.form["contactPassword"] = jiveCloudPassword
  26.     br.submit()
  27.     return br
  28.  
  29. def getDeployRequestLogData(deployRequestId):
  30.     br = JcaLogin()
  31.     response = br.open("%s/manage-deployrequestlog.jspa?deployRequestId=%s" % (jiveCloudUrl, deployRequestId))
  32.     soup = BeautifulSoup(response.read())
  33.     table = soup.findAll("table")[2]
  34.     deployRequestLogData = []
  35.     for row in table.findAll('tr')[1:]:
  36.         col = row.findAll('td')
  37.         timestamp = col[0].string.rstrip().lstrip()
  38.         msg = col[1].string.rstrip().lstrip()
  39.         deployRequestLogData.append({ 'timestamp': timestamp, 'msg': msg })
  40.     return deployRequestLogData
  41.  
  42. def getHostsFromDeployRequestLogData(deployRequestLogData):
  43.     ips = defaultdict()
  44.     for logline in deployRequestLogData:
  45.         if 'Created new  virtual machine record' in logline['msg']:
  46.             ip = logline['msg'].split("with ip '")[1].split("'")[0].lstrip().rstrip()
  47.             ips[ip] = ip
  48.     listOfIps = []
  49.     for ip in ips.iterkeys():
  50.         listOfIps.append(ip)
  51.     return listOfIps
  52.  
  53. def runCommand(ips, command):
  54.     for ip in ips:
  55.         print "Running command on: " + ip
  56.         ssh = paramiko.SSHClient()
  57.         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  58.         ssh.connect(ip, username='root', password=defaultRootPassword)
  59.         stdin, stdout, stderr = ssh.exec_command(command)
  60.         print stdout.read()
  61.  
  62. def parseOptions():
  63.     parser = argparse.ArgumentParser(description='Run commands on hosts contained in a Create Installation DeployRequest')
  64.     parser.add_argument("-deployRequest", "--deployRequestId", dest="deployRequestId",
  65.                       help="JiveCloud deployRequest Id to fix saasagent for")
  66.     parser.add_argument("-command", "--remoteCommand", dest="remoteCommand",
  67.                       help="Command to run remotely on each host for a given deployRequest")
  68.     args = parser.parse_args()
  69.     return args
  70.  
  71. args = parseOptions()
  72.  
  73. ips = getHostsFromDeployRequestLogData(getDeployRequestLogData(args.deployRequestId))
  74.  
  75. runCommand(ips, args.remoteCommand)
Add Comment
Please, Sign In to add comment