Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python -tt
- import mechanize
- import cookielib
- import paramiko
- from collections import defaultdict
- from BeautifulSoup import BeautifulSoup
- import argparse
- # Configuration
- jiveCloudUrl="XXX"
- jiveCloudLogin="XXX"
- jiveCloudPassword="XXXX"
- defaultRootPassword="XXX"
- def JcaLogin():
- br = mechanize.Browser()
- br.set_handle_refresh(False)
- cj = cookielib.LWPCookieJar()
- br.set_cookiejar(cj)
- br.open('%s/login.jspa' % jiveCloudUrl)
- br.select_form(nr=0)
- br.form["contactId"] = jiveCloudLogin
- br.form["contactPassword"] = jiveCloudPassword
- br.submit()
- return br
- def getDeployRequestLogData(deployRequestId):
- br = JcaLogin()
- response = br.open("%s/manage-deployrequestlog.jspa?deployRequestId=%s" % (jiveCloudUrl, deployRequestId))
- soup = BeautifulSoup(response.read())
- table = soup.findAll("table")[2]
- deployRequestLogData = []
- for row in table.findAll('tr')[1:]:
- col = row.findAll('td')
- timestamp = col[0].string.rstrip().lstrip()
- msg = col[1].string.rstrip().lstrip()
- deployRequestLogData.append({ 'timestamp': timestamp, 'msg': msg })
- return deployRequestLogData
- def getHostsFromDeployRequestLogData(deployRequestLogData):
- ips = defaultdict()
- for logline in deployRequestLogData:
- if 'Created new virtual machine record' in logline['msg']:
- ip = logline['msg'].split("with ip '")[1].split("'")[0].lstrip().rstrip()
- ips[ip] = ip
- listOfIps = []
- for ip in ips.iterkeys():
- listOfIps.append(ip)
- return listOfIps
- def runCommand(ips, command):
- for ip in ips:
- print "Running command on: " + ip
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh.connect(ip, username='root', password=defaultRootPassword)
- stdin, stdout, stderr = ssh.exec_command(command)
- print stdout.read()
- def parseOptions():
- parser = argparse.ArgumentParser(description='Run commands on hosts contained in a Create Installation DeployRequest')
- parser.add_argument("-deployRequest", "--deployRequestId", dest="deployRequestId",
- help="JiveCloud deployRequest Id to fix saasagent for")
- parser.add_argument("-command", "--remoteCommand", dest="remoteCommand",
- help="Command to run remotely on each host for a given deployRequest")
- args = parser.parse_args()
- return args
- args = parseOptions()
- ips = getHostsFromDeployRequestLogData(getDeployRequestLogData(args.deployRequestId))
- runCommand(ips, args.remoteCommand)
Add Comment
Please, Sign In to add comment