diff options
Diffstat (limited to 'junos-bootstrap/httpd')
| -rw-r--r-- | junos-bootstrap/httpd/server_http.py | 56 | ||||
| -rw-r--r-- | junos-bootstrap/httpd/terminal.log | 48 | 
2 files changed, 69 insertions, 35 deletions
| diff --git a/junos-bootstrap/httpd/server_http.py b/junos-bootstrap/httpd/server_http.py index 635bd0e..a7c63d1 100644 --- a/junos-bootstrap/httpd/server_http.py +++ b/junos-bootstrap/httpd/server_http.py @@ -7,6 +7,7 @@ import time  import psycopg2  import psycopg2.extras  import sys +import os  def main():      # @@ -20,8 +21,9 @@ def main():  		    host = 'localhost'  	    ),  	    http = dict( -		    host = 'localhost', -		    port = 80 +		    # host = 'localhost', +		    host = '10.0.30.131', +		    port = 8080  	    )      ) @@ -48,13 +50,12 @@ def main():          sys.exit(1)      def template_get(model): -        return open(model + '.template').read() +        return open('junos-bootstrap/httpd/' + model + '.template').read()      def template_parse(template_src, hostname):          cur.execute("SELECT * FROM switches WHERE hostname = '%s'" % hostname)          if(cur.rowcount == 1):              row = cur.fetchall()[0] -            print(' --> DB response ok, populating template')              d={                  'hostname': row['hostname'],                  'distro_name': row['distro_name'], @@ -66,30 +67,61 @@ def main():              }              return Template(template_src).safe_substitute(d)          else: -            print(' --> No hits in DB for hostname "%s", cannot continue' % hostname)              return False      class httpd(BaseHTTPRequestHandler):          def do_GET(self): -            print('[%s] Incoming request: source:%s path:%s ' % (time.asctime(), self.client_address[0], self.path)) +            print('[%s] [%s] Incoming HTTP GET URI:%s ' % (self.client_address[0], time.asctime(), self.path)) +             +            # Client asks for the config file              if '/tg15-edge/' in self.path:                  hostname = self.path.split('/tg15-edge/')[1]                  if len(hostname) > 0: -                    print(' --> hostname "%s" accepted, fetching info from DB' % hostname) +                    print('[%s] --> Hostname "%s" accepted, fetching info from DB' % (self.client_address[0], hostname))                      template_parsed = template_parse(template_get('ex2200'), hostname)                      if template_parsed: -                        print(' --> sending response to client') +                        print('[%s] --> Template successfully populated' % self.client_address[0]) +                        print('[%s] --> Sending response to client' % self.client_address[0])                          self.send_response(200)                          self.send_header("Content-type", "text/plain")                          self.end_headers()                          self.wfile.write(bytes(template_parsed, "utf-8")) -                        print(' --> success - %s bytes sent to client' % len(template_parsed)) +                        print('[%s] --> Success - %s bytes sent to client' % (self.client_address[0], len(template_parsed)))                      else: -                        print(' --> error - template could not be populated') +                        print('[%s] --> Error - template could not be populated' % self.client_address[0]) +                else: +                    print('[%s] --> Rejected due to missing hostname' % self.client_address[0]) +                     +            # Client asks for a file download - most likely a JunOS file +            elif '/files/' in self.path: +                # It seems that "http.server" escapes nastiness from the URL - ("/files/../../../root_file" => "/files/root_file") +                requested_file = self.path.split('/files/')[1] +                files_dir = 'junos-bootstrap/httpd/files/' +                print('[%s] --> File request for "%s" in "%s"' % (self.client_address[0], requested_file, files_dir)) +                if os.path.isfile(files_dir + requested_file): +                    print('[%s] --> File found' % self.client_address[0]) +                    try: +                        f = open(files_dir + requested_file) +                        self.send_response(200) +                        self.send_header('Content-type', 'application/x-gzip') # correct content type for tar.gz +                        self.end_headers() +                        print('[%s]     --> File transfer started' % self.client_address[0]) +                        f = open(files_dir + requested_file, 'rb') +                        self.wfile.write(f.read()) +                        f.close() +                        print('[%s]     --> File transfer completed' % self.client_address[0]) +                        return +                    except IOError: +                        self.send_error(404,'File Not Found: %s' % self.path) +                        print('[%s] --> ERROR 404 - File not found' % self.client_address[0]) +                        pass +                    except: +                        print('[%s] --> Generic error during file reading' % self.client_address[0]) +                        pass                  else: -                    print(' --> rejected due to missing hostname') +                    print('[%s] --> File request rejected due to nonexisting file' % self.client_address[0])              else: -                print(' --> rejected due to bad path') +                print('[%s] --> rejected due to bad path' % self.client_address[0])          # silence stderr from BaseHTTPRequestHandler          # source: http://stackoverflow.com/questions/3389305/how-to-silent-quiet-httpserver-and-basichttprequesthandlers-stderr-output          def log_message(self, format, *args): diff --git a/junos-bootstrap/httpd/terminal.log b/junos-bootstrap/httpd/terminal.log index 1974079..bc29dc7 100644 --- a/junos-bootstrap/httpd/terminal.log +++ b/junos-bootstrap/httpd/terminal.log @@ -1,4 +1,4 @@ -j@lappie:~/dhcp-tech82$ python3 server_http.py +j@lappie:~/git/tgmanage$ sudo python3 junos-bootstrap/httpd/server_http.py  Switches in DB during server_http.py startup:   --> e-00-0-test, connected to distro-test port ge-0/0/0 @@ -6,29 +6,31 @@ Switches in DB during server_http.py startup:   --> e-00-2-test, connected to distro-test port ge-0/0/6   --> e-60-0-test, connected to distro-test port ge-0/0/9 -[Wed Jan 28 00:38:18 2015] Server Starts - localhost:9000 -[Wed Jan 28 00:38:42 2015] Incoming request: source:127.0.0.1 path:/tg15-edge/e-00-0-test  - --> hostname "e-00-0-test" accepted, fetching info from DB - --> DB response ok, populating template - --> sending response to client - --> success - 1442 bytes sent to client -[Wed Jan 28 00:38:50 2015] Incoming request: source:127.0.0.1 path:/tg15-edge/e-00-1-test  - --> hostname "e-00-1-test" accepted, fetching info from DB - --> DB response ok, populating template - --> sending response to client - --> success - 1442 bytes sent to client -[Wed Jan 28 00:38:59 2015] Incoming request: source:127.0.0.1 path:/tg15-edge/e-00-5-test  - --> hostname "e-00-5-test" accepted, fetching info from DB - --> No hits in DB for hostname "e-00-5-test", cannot continue - --> error - template could not be populated -[Wed Jan 28 00:39:27 2015] Incoming request: source:127.0.0.1 path:/tg15-edge/e-60-0-test  - --> hostname "e-60-0-test" accepted, fetching info from DB - --> DB response ok, populating template - --> sending response to client - --> success - 1442 bytes sent to client +[Wed Feb 11 03:02:15 2015] Server Starts - 10.0.30.131:8080 +[10.0.10.6] [Wed Feb 11 03:02:28 2015] Incoming HTTP GET request: source:10.0.10.6 path:/files/jinstall-ex-2200-12.3R6.6-domestic-signed.tgz  +[10.0.10.6] --> File request for "jinstall-ex-2200-12.3R6.6-domestic-signed.tgz" in "junos-bootstrap/httpd/files/" +[10.0.10.6] --> File found +[10.0.10.6]     --> File transfer started +[10.0.10.6]     --> File transfer completed +[10.0.10.6] [Wed Feb 11 03:03:32 2015] Incoming HTTP GET request: source:10.0.10.6 path:/files/nonexisting_filename  +[10.0.10.6] --> File request for "nonexisting_filename" in "junos-bootstrap/httpd/files/" +[10.0.10.6] --> File request rejected due to nonexisting file +[10.0.10.6] [Wed Feb 11 03:03:47 2015] Incoming HTTP GET request: source:10.0.10.6 path:/files/jinstall-ex-2200-14.1X53-D15.2-domestic-signed.tgz  +[10.0.10.6] --> File request for "jinstall-ex-2200-14.1X53-D15.2-domestic-signed.tgz" in "junos-bootstrap/httpd/files/" +[10.0.10.6] --> File found +[10.0.10.6]     --> File transfer started +[10.0.10.6]     --> File transfer completed +[10.0.10.6] [Wed Feb 11 03:08:18 2015] Incoming HTTP GET request: source:10.0.10.6 path:/tg15-edge/e-60-0-test  +[10.0.10.6] --> Hostname "e-60-0-test" accepted, fetching info from DB +[10.0.10.6] --> Template successfully populated +[10.0.10.6] --> Sending response to client +[10.0.10.6] --> Success - 1442 bytes sent to client +[10.0.10.6] [Wed Feb 11 03:08:18 2015] Incoming HTTP GET request: source:10.0.10.6 path:/favicon.ico  +[10.0.10.6] --> rejected due to bad path +[10.0.10.6] [Wed Feb 11 03:08:18 2015] Incoming HTTP GET request: source:10.0.10.6 path:/favicon.ico  +[10.0.10.6] --> rejected due to bad path  ^C -[Wed Jan 28 00:39:34 2015] HTTP Server stopped +[Wed Feb 11 03:09:06 2015] HTTP Server stopped -j@lappie:~/dhcp-tech82$  | 
