Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import urllib
- import paramiko
- import json
- from optparse import OptionParser
- def main():
- parser = OptionParser(conflict_handler="resolve")
- parser.set_usage("ddsh [options] 'command'")
- parser.add_option("-p", dest="puppetclass", help="Use instances from the following puppet class")
- (options, args) = parser.parse_args()
- command = args[0]
- query = '[[Resource Type::instance]][[Puppet Class::%s]]|?FQDN|format=json' % (options.puppetclass)
- query = ask_encode(query)
- askaddress = 'http://nova-controller.tesla.usability.wikimedia.org/trunk.1/Special:Ask/'
- query = askaddress + query
- f = urllib.urlopen(query)
- results = f.read()
- results = json.loads(results)
- for result in results['items']:
- fqdn = result['fqdn']
- print 'Running "%s" on instance "%s"' % (command, fqdn)
- success = run_command(fqdn, command)
- def ask_encode(query):
- matches = {'[': '-5B', ']': '-5D', ' ': '-20', '|': '/', '=': '%3D', '?': '-3F'}
- for match,replace in matches.iteritems():
- query = query.replace(match, replace)
- return query
- def run_command(server, command):
- try:
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- try:
- ssh.connect(server)
- except (paramiko.SSHException, socket.error):
- print "Failed to connect to %s." % server
- return False
- stdin, stdout, stderr = ssh.exec_command(command)
- for line in stdout.readlines():
- line = line.strip()
- print line
- return True
- except Exception:
- print "Couldn't connect to %s" % (server)
- return False
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement