| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | import sys |
|---|
| 23 | import socket |
|---|
| 24 | import gdhost.client as client |
|---|
| 25 | from gdhost.version import version_string |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | if sys.version_info >= (2, 2) and \ |
|---|
| 29 | sys.version_info < (2,3): |
|---|
| 30 | try: |
|---|
| 31 | import optik as optparse |
|---|
| 32 | except ImportError: |
|---|
| 33 | try: |
|---|
| 34 | import optparse |
|---|
| 35 | except ImportError: |
|---|
| 36 | print >> sys.stderr, "You must either install optik (http://optik.sf.net) or upgrade to python 2.3" |
|---|
| 37 | elif sys.version_info >= (2,3): |
|---|
| 38 | import optparse |
|---|
| 39 | else: |
|---|
| 40 | print >> sys.stderr, "This program requires python 2.2 with the optik module (http://optik.sf.net) or python 2.3 or greater" |
|---|
| 41 | sys.exit(1) |
|---|
| 42 | |
|---|
| 43 | parser = optparse.OptionParser(version=version_string, |
|---|
| 44 | usage="%prog [options] command") |
|---|
| 45 | parser.add_option( '-k', '--key', action="store", |
|---|
| 46 | dest="key", |
|---|
| 47 | help="Encryption Key (required)") |
|---|
| 48 | parser.add_option( '-n', '--name', action="store", |
|---|
| 49 | dest="name", default= socket.gethostname(), |
|---|
| 50 | help="The dynamic host shortname" ) |
|---|
| 51 | parser.add_option( '-i', '--ip', action="store", |
|---|
| 52 | dest='ip', default='auto', |
|---|
| 53 | help="Your IP address, 'auto' or 'server'. By default, this is auto. 'auto' means that your ip is discovered locally before talking to the server. 'server' means that the server will discover your ip. If you are behind a NAT firewall, 'server' will discover the firewall's ip address." ) |
|---|
| 54 | parser.add_option( '-S', '--server', action="store", |
|---|
| 55 | dest="server", default="gerf.net", |
|---|
| 56 | help="The GDH server's dns name or ip address" ) |
|---|
| 57 | parser.add_option( '-P', '--port', action="store", |
|---|
| 58 | dest="port", default=8888, |
|---|
| 59 | help="The GDH server's port number" ) |
|---|
| 60 | parser.add_option( '-q', '--quiet', action="store_true", |
|---|
| 61 | dest="quiet", default=False, |
|---|
| 62 | help="Run quietly" ) |
|---|
| 63 | parser.add_option( '-d', '--debug', action="store_true", |
|---|
| 64 | dest="debug", default=False, |
|---|
| 65 | help="Turn on debugging" ) |
|---|
| 66 | |
|---|
| 67 | if "__main__" == __name__: |
|---|
| 68 | options, command = parser.parse_args() |
|---|
| 69 | if command: |
|---|
| 70 | command = command[0].strip() |
|---|
| 71 | if command not in ('update','remove') : |
|---|
| 72 | parser.error('Command must be one of update or remove; not %s' % \ |
|---|
| 73 | repr(command)) |
|---|
| 74 | sys.exit(1) |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | if options.ip not in ('auto','server') and \ |
|---|
| 78 | not options.ip.replace('.','').isdigit(): |
|---|
| 79 | |
|---|
| 80 | parser.error("IP address must be an ip number, 'auto', or 'server'; not '%s'" % options.ip) |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | if not command: |
|---|
| 84 | parser.print_help() |
|---|
| 85 | if options.ip == 'auto': |
|---|
| 86 | ip = "discover locally" |
|---|
| 87 | elif options.ip == 'server': |
|---|
| 88 | ip = "server will discover the ip" |
|---|
| 89 | else: |
|---|
| 90 | ip = repr(options.ip) |
|---|
| 91 | print |
|---|
| 92 | print "current values:" |
|---|
| 93 | print " KEY = '%s'" % (options.key or '-required-') |
|---|
| 94 | print " NAME = '%s'" % options.name |
|---|
| 95 | print " IP = %s" % ip |
|---|
| 96 | print " SERVER = '%s'" % options.server |
|---|
| 97 | print " PORT = '%s'" % options.port |
|---|
| 98 | sys.exit(1) |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | if not options.key: |
|---|
| 102 | parser.error('You must set the encryption key (-k)') |
|---|
| 103 | sys.exit(1) |
|---|
| 104 | |
|---|
| 105 | try: |
|---|
| 106 | connection = client.Connection( server=options.server, |
|---|
| 107 | port=options.port ) |
|---|
| 108 | if command == 'update': |
|---|
| 109 | if options.ip == 'auto': |
|---|
| 110 | ip = client.LOCAL_IP_DISCOVER |
|---|
| 111 | elif options.ip == 'server': |
|---|
| 112 | ip = client.REMOTE_IP_DISCOVER |
|---|
| 113 | else: |
|---|
| 114 | ip = options.ip |
|---|
| 115 | connection.update( key=options.key, |
|---|
| 116 | name=options.name, |
|---|
| 117 | ip=ip ) |
|---|
| 118 | else: |
|---|
| 119 | connection.delete( key=options.key, |
|---|
| 120 | name=options.name ) |
|---|
| 121 | except client.ErrorConnection,err: |
|---|
| 122 | if options.debug: |
|---|
| 123 | raise |
|---|
| 124 | else: |
|---|
| 125 | print >> sys.stderr, "Unable to communicate with %s:%s:\n\t%s" % ( |
|---|
| 126 | options.server, options.port, str(err)) |
|---|
| 127 | sys.exit(10) |
|---|
| 128 | except client.ErrorDenied: |
|---|
| 129 | if options.debug: |
|---|
| 130 | raise |
|---|
| 131 | else: |
|---|
| 132 | print >> sys.stderr, "The server rejected your request" |
|---|
| 133 | sys.exit(10) |
|---|
| 134 | except client.ErrorNoName: |
|---|
| 135 | if options.debug: |
|---|
| 136 | raise |
|---|
| 137 | else: |
|---|
| 138 | print >> sys.stderr, "The server didn't recognize the name %s" % options.name |
|---|
| 139 | sys.exit(10) |
|---|
| 140 | except: |
|---|
| 141 | if options.debug: |
|---|
| 142 | raise |
|---|
| 143 | print >> sys.stderr, "Unexpected Error!" |
|---|
| 144 | if options.debug: |
|---|
| 145 | print >> sys.stderr, "Plase file a bug at http://trac.gerf.org/gdhost with the above output" |
|---|
| 146 | else: |
|---|
| 147 | print >> sys.stderr, "Please rerun this command with --debug and file a bug at http://trac.gerf.org/gdhost" |
|---|
| 148 | |
|---|
| 149 | if not options.quiet: |
|---|
| 150 | print "Successfully did a %s for %s!" % (command, options.name) |
|---|
| 151 | sys.exit(0) |
|---|