root / trunk / gdhostc

Revision 71, 5.9 kB (checked in by docwhat, 3 years ago)

r1125@dib: docwhat | 2005-09-27 20:44:38 -0400
Added better check on connection start

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to author date id revision
Line 
1#!/usr/bin/python
2## -*- coding: utf-8 -*-
3##
4## gdhostc -- Gerf Dynamic Host Client
5##
6## Copyright (C) 2001-2002,2005 Nick Borko & Christian H�e
7## This program is free software; you can redistribute it and/or modify
8## it under the terms of the GNU General Public License as published by
9## the Free Software Foundation; either version 2 of the License, or
10## (at your option) any later version.
11##
12## This program is distributed in the hope that it will be useful,
13## but WITHOUT ANY WARRANTY; without even the implied warranty of
14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15## GNU General Public License for more details.
16##
17## You should have received a copy of the GNU General Public License
18## along with this program; if not, write to the Free Software
19## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20##
21
22import sys
23import socket
24import gdhost.client as client
25from gdhost.version import version_string
26
27## Version Check
28if 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"
37elif sys.version_info >= (2,3):
38    import optparse
39else:
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
43parser = optparse.OptionParser(version=version_string,
44                               usage="%prog [options] command")
45parser.add_option( '-k', '--key', action="store",
46                        dest="key",
47                        help="Encryption Key (required)")
48parser.add_option( '-n', '--name', action="store",
49                   dest="name", default= socket.gethostname(),
50                   help="The dynamic host shortname" )
51parser.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." )
54parser.add_option( '-S', '--server', action="store",
55                   dest="server", default="gerf.net",
56                   help="The GDH server's dns name or ip address" )
57parser.add_option( '-P', '--port', action="store",
58                   dest="port", default=8888,
59                   help="The GDH server's port number" )
60parser.add_option( '-q', '--quiet', action="store_true",
61                   dest="quiet", default=False,
62                   help="Run quietly" )
63parser.add_option( '-d', '--debug', action="store_true",
64                   dest="debug", default=False,
65                   help="Turn on debugging" )
66
67if "__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    # Verify that ip is legit
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    # Helpful display
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    # Required values
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)
Note: See TracBrowser for help on using the browser.