Created
July 31, 2014 14:58
-
-
Save KrisSaxton/cec588113acde399ebcc to your computer and use it in GitHub Desktop.
read a config file dammit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys, getopt, string | |
sys.path.append("/opt/opsware/pylibs2") | |
from pytwist import * | |
from pytwist.com.opsware.server import * | |
from pytwist.com.opsware.search import Filter | |
longOptions = ["server=", "id=", "string=", "help"] | |
def usage(): | |
"Prints a usage statement for the program." | |
print """encrypt4target.py help: | |
encrypt4target.py ( --server <server> | --sid <server id> ) [--string <value>] | |
Encrypts a string such that it can only be read by the target server. | |
--server <server> Specify the server by name. | |
--id <server id> Specify the server by id. | |
--string <value> Specify the string to encrypt with the target's key | |
--help Prints this help | |
""" | |
PRIVATE_KEY_PATH='/etc/hosts' | |
argv=sys.argv[1:] | |
try: | |
opts, rest = getopt.getopt(argv,"",longOptions) | |
except getopt.error, errorStr: | |
print "Error: %s" % (errorStr) | |
usage() | |
sys.exit(1) | |
serverName=None | |
serverID=None | |
myString=None | |
for opt, arg in opts: | |
if opt == "--help": | |
usage() | |
sys.exit(0) | |
elif opt == "--server": | |
serverName=arg | |
elif opt == "--id": | |
serverID=arg | |
elif opt == "--string": | |
myString=arg | |
# Validate the options | |
if serverName==None and serverID==None: | |
print "Error: No server name (--server) or server ID (--id) specified." | |
usage() | |
sys.exit(1) | |
if serverName!=None and serverID!=None: | |
print "Error: Specify only on of server name (--server) or server ID (--id) specified." | |
usage() | |
sys.exit(1) | |
if myString==None: | |
print "Error: No string to encrypt given." | |
usage() | |
sys.exit(1) | |
ts=twistserver.TwistServer() | |
ss=ts.server.ServerService | |
# Lookup the server reference | |
if serverName: | |
filter=Filter() | |
filter.expression='{ServerVO.name EQUAL_TO "%s"}'%(serverName) | |
sRefs=ss.findServerRefs(filter) | |
if len(sRefs) == 0: | |
print "Could not find server" | |
sys.exit(1) | |
sRef=sRefs[0] | |
elif serverID: | |
sRef = ServerRef(serverID) | |
else: | |
print "Error: No server name (--server) or server ID (--id) specified." | |
usage() | |
sys.exit(1) | |
# Read the server agent's private key | |
ss.readConfigFile(sRef, PRIVATE_KEY_PATH) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment