From cb65b2a1aed0a7d1f03a58434156e0aaac9abdce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20GR=C3=89A?= Date: Sat, 22 Feb 2020 16:44:15 +0100 Subject: [PATCH 1/6] Change the print syntax for newer PHP version + stderr --- phpipam-hosts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/phpipam-hosts b/phpipam-hosts index 5c2ef2c..fa01442 100755 --- a/phpipam-hosts +++ b/phpipam-hosts @@ -225,10 +225,10 @@ def listSections(): output = output + line output = output.rstrip('\n') - print output + print(output) except db.Error, e: - print "Error %d: %s" % (e.args[0],e.args[1]) + fwrite(STDERR, "Error %d: %s\n" % (e.args[0],e.args[1])) sys.exit(1) finally: @@ -262,10 +262,10 @@ def listSubnets(): output = output + line output = output.rstrip('\n') - print output + print(output) except db.Error, e: - print "Error %d: %s" % (e.args[0],e.args[1]) + fwrite(STDERR, "Error %d: %s\n" % (e.args[0],e.args[1])) sys.exit(1) finally: @@ -337,19 +337,19 @@ try: try: fqdn = row[0].strip() except: - print("Ignoring entry with FQDN missing: %s" % row_str) + fwrite(STDERR, "Ignoring entry with FQDN missing: %s\n" % row_str) continue hostname = fqdn.split('.')[0] try: ip = ipAddr(row[2]) except: - print("Ignoring entry with IP missing: %s" % row_str) + fwrite(STDERR, "Ignoring entry with IP missing: %s\n" % row_str) continue ip_real = ip try: state = row[3] except: - print("Ignoring entry with STATE missing: %s" % row_str) + fwrite(STDERR, "Ignoring entry with STATE missing: %s\n" % row_str) continue #entry = '' @@ -452,7 +452,7 @@ try: # If no output file is specified, or stdout is specified, print the output # to stdout. if args.output is None or args.output == '-': - print output + print(output) # Otherwise, write the output to a specified file. else: @@ -476,7 +476,7 @@ try: try: open(os.path.realpath(args.trigger),'w').close() except: - print "Error: cannot write to %s: access denied" % args.trigger + fwrite(STDERR, "Error: cannot write to %s: access denied\n" % args.trigger) sys.exit(1) # If --restart is enabled and host list has been changed, restart @@ -485,7 +485,7 @@ try: os.system(args.restart_command) except: - print "Error: cannot write to %s: access denied" % args.output + '.tmp' + fwrite(STDERR, "Error: cannot write to %s: access denied\n" % args.output + '.tmp') sys.exit(1) # There is no previous host list, so let's create a new one right away @@ -502,7 +502,7 @@ try: try: open(os.path.realpath(args.trigger),'w').close() except: - print "Error: cannot write to %s: access denied" % args.trigger + fwrite(STDERR, "Error: cannot write to %s: access denied\n" % args.trigger) sys.exit(1) # If --restart is enabled and host list has been changed, restart @@ -511,7 +511,7 @@ try: os.system(args.restart_command) except: - print "Error: cannot write to %s: access denied" % args.output + fwrite(STDERR, "Error: cannot write to %s: access denied\n" % args.output) sys.exit(1) # There is no output @@ -523,7 +523,7 @@ try: open(os.path.realpath(args.output),'w').close() except db.Error, e: - print "Error %d: %s" % (e.args[0],e.args[1]) + fwrite(STDERR, "Error %d: %s\n" % (e.args[0],e.args[1])) sys.exit(1) finally: From 1c627dd3c6fd41912b6460520627ea2290971805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20GR=C3=89A?= Date: Sat, 22 Feb 2020 19:18:53 +0100 Subject: [PATCH 2/6] =?UTF-8?q?My=20bad,=20it's=20python=20not=20PHP?= =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpipam-hosts | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/phpipam-hosts b/phpipam-hosts index fa01442..d75ee12 100755 --- a/phpipam-hosts +++ b/phpipam-hosts @@ -29,7 +29,7 @@ # http://www.gnu.org/copyleft/gpl.html -import os, sys, re, argparse, MySQLdb, string, socket, struct, ConfigParser, filecmp +import os, sys, re, argparse, MySQLdb, string, socket, struct, configparser, filecmp # Define script version script_version = 'v0.2.1' @@ -115,7 +115,7 @@ if args.states is None: # Load and parse specified configuration file -config = ConfigParser.SafeConfigParser() +config = configparser.SafeConfigParser() config.read(args.config.name) if config.has_section(args.group): @@ -227,8 +227,8 @@ def listSections(): output = output.rstrip('\n') print(output) - except db.Error, e: - fwrite(STDERR, "Error %d: %s\n" % (e.args[0],e.args[1])) + except db.Error as e: + print("Error %d: %s" % (e.args[0],e.args[1])) sys.exit(1) finally: @@ -264,8 +264,8 @@ def listSubnets(): output = output.rstrip('\n') print(output) - except db.Error, e: - fwrite(STDERR, "Error %d: %s\n" % (e.args[0],e.args[1])) + except db.Error as e: + print("Error %d: %s" % (e.args[0],e.args[1])) sys.exit(1) finally: @@ -337,19 +337,19 @@ try: try: fqdn = row[0].strip() except: - fwrite(STDERR, "Ignoring entry with FQDN missing: %s\n" % row_str) + print(("Ignoring entry with FQDN missing: %s" % row_str)) continue hostname = fqdn.split('.')[0] try: ip = ipAddr(row[2]) except: - fwrite(STDERR, "Ignoring entry with IP missing: %s\n" % row_str) + print(("Ignoring entry with IP missing: %s" % row_str)) continue ip_real = ip try: state = row[3] except: - fwrite(STDERR, "Ignoring entry with STATE missing: %s\n" % row_str) + print(("Ignoring entry with STATE missing: %s" % row_str)) continue #entry = '' @@ -476,7 +476,7 @@ try: try: open(os.path.realpath(args.trigger),'w').close() except: - fwrite(STDERR, "Error: cannot write to %s: access denied\n" % args.trigger) + print("Error: cannot write to %s: access denied" % args.trigger) sys.exit(1) # If --restart is enabled and host list has been changed, restart @@ -485,7 +485,7 @@ try: os.system(args.restart_command) except: - fwrite(STDERR, "Error: cannot write to %s: access denied\n" % args.output + '.tmp') + print("Error: cannot write to %s: access denied" % args.output + '.tmp') sys.exit(1) # There is no previous host list, so let's create a new one right away @@ -502,7 +502,7 @@ try: try: open(os.path.realpath(args.trigger),'w').close() except: - fwrite(STDERR, "Error: cannot write to %s: access denied\n" % args.trigger) + print("Error: cannot write to %s: access denied" % args.trigger) sys.exit(1) # If --restart is enabled and host list has been changed, restart @@ -511,7 +511,7 @@ try: os.system(args.restart_command) except: - fwrite(STDERR, "Error: cannot write to %s: access denied\n" % args.output) + print("Error: cannot write to %s: access denied" % args.output) sys.exit(1) # There is no output @@ -522,8 +522,8 @@ try: if not os.path.isfile(os.path.realpath(args.output)): open(os.path.realpath(args.output),'w').close() -except db.Error, e: - fwrite(STDERR, "Error %d: %s\n" % (e.args[0],e.args[1])) +except db.Error as e: + print("Error %d: %s" % (e.args[0],e.args[1])) sys.exit(1) finally: @@ -532,4 +532,3 @@ finally: db.close() except NameError: sys.exit(0) - From 7343f2b54bb359244863536055e33bc119cf6a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20GR=C3=89A?= Date: Sat, 22 Feb 2020 20:49:27 +0100 Subject: [PATCH 3/6] Removing deprecated file type --- phpipam-hosts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/phpipam-hosts b/phpipam-hosts index d75ee12..0aef4a4 100755 --- a/phpipam-hosts +++ b/phpipam-hosts @@ -41,20 +41,20 @@ arg_parser = argparse.ArgumentParser( epilog='%(prog)s Copyright (C) 2014 Maciej Delmanowski \nLicense: GPLv3. Homepage: https://github.com/ginas/phpipam-scripts/') # Default confguration file -arg_parser.add_argument('-c','--config', type=file, default='/etc/dhcp/phpipam.conf', metavar='CONFIG', help='use alternative configuration file') +arg_parser.add_argument('-c','--config', default='/etc/dhcp/phpipam.conf', metavar='CONFIG', help='use alternative configuration file') # Use DNS hostnames instead of IP addresses in generated host lists arg_parser.add_argument('-d','--dns', default=False, action='store_true', help='write host names instead of IP addresses') # Output format -arg_parser.add_argument('-f','--format', type=str, default='dhcpd', choices=['dhcpd','dnsmasq','hosts','ethers'], help='output format (default: dhcpd)') +arg_parser.add_argument('-f','--format', default='dhcpd', choices=['dhcpd','dnsmasq','hosts','ethers'], help='output format (default: dhcpd)') # Default configuration section to use by default -arg_parser.add_argument('-g','--group', type=str, default='hosts', help='configuration section to use (default: hosts)') +arg_parser.add_argument('-g','--group', default='hosts', help='configuration section to use (default: hosts)') # Generated hostname prefixes for dhcp and dynamic hosts -arg_parser.add_argument('-i','--prefix-dhcp', type=str, default='dhcp', metavar='PREFIX', help='prefix for hosts without hostname') -arg_parser.add_argument('-j','--prefix-dynamic', type=str, default='dynamic', metavar='PREFIX', help='prefix for hosts without static IP address') +arg_parser.add_argument('-i','--prefix-dhcp', default='dhcp', metavar='PREFIX', help='prefix for hosts without hostname') +arg_parser.add_argument('-j','--prefix-dynamic', default='dynamic', metavar='PREFIX', help='prefix for hosts without static IP address') # By default script will create empty files to avoid problems with missing # includes in dhcpd and dnsmasq @@ -66,14 +66,14 @@ arg_parser.add_argument('-n','--no-mac', default=False, action='store_true', hel # Optional output file. If one is configured in the configuration options, you # can set '-o -' to output to stdout -arg_parser.add_argument('-o','--output', type=str, metavar='FILE', help='output host list to a file') +arg_parser.add_argument('-o','--output', metavar='FILE', help='output host list to a file') # Default shell command to execute to restart dhcpd daemon -arg_parser.add_argument('-r','--restart-command', type=str, default='/etc/init.d/isc-dhcp-server restart', metavar='COMMAND', help='use alternative shell command to restart dhcpd') +arg_parser.add_argument('-r','--restart-command', default='/etc/init.d/isc-dhcp-server restart', metavar='COMMAND', help='use alternative shell command to restart dhcpd') # Optional trigger file which can be used to indicate that the generated host # file has changed -arg_parser.add_argument('-t','--trigger', type=str, metavar='FILE', help='create trigger file if host file has changed') +arg_parser.add_argument('-t','--trigger', metavar='FILE', help='create trigger file if host file has changed') # If this option is enabled, script will restart dhcpd daemon using specified # shell command @@ -116,7 +116,7 @@ if args.states is None: # Load and parse specified configuration file config = configparser.SafeConfigParser() -config.read(args.config.name) +config.read(args.config) if config.has_section(args.group): @@ -126,7 +126,7 @@ if config.has_section(args.group): if args.ddns is False and config.has_option(args.group,'ddns'): args.ddns = config.getboolean(args.group,'ddns') - if args.format is 'dhcpd' and config.has_option(args.group,'format'): + if args.format == 'dhcpd' and config.has_option(args.group,'format'): args.format = config.get(args.group,'format') if args.output is None and config.has_option(args.group,'output'): @@ -135,7 +135,7 @@ if config.has_section(args.group): if args.restart is False and config.has_option(args.group,'restart'): args.restart = config.getboolean(args.group,'restart') - if args.restart_command is '/etc/init.d/isc-dhcp-server restart' and config.has_option(args.group,'restart-command'): + if args.restart_command == '/etc/init.d/isc-dhcp-server restart' and config.has_option(args.group,'restart-command'): args.restart_command = config.get(args.group,'restart-command') if args.restart_trigger is False and config.has_option(args.group,'restart-trigger'): @@ -212,7 +212,7 @@ def listSections(): query = 'SELECT id,name,description FROM sections ORDER BY id ASC' try: - db = MySQLdb.connect(read_default_file = args.config.name, read_default_group = 'mysql') + db = MySQLdb.connect(read_default_file = args.config, read_default_group = 'mysql') cursor = db.cursor() cursor.execute(query) @@ -248,7 +248,7 @@ def listSubnets(): query = query + 'ORDER BY subnet ASC' try: - db = MySQLdb.connect(read_default_file = args.config.name, read_default_group = 'mysql') + db = MySQLdb.connect(read_default_file = args.config, read_default_group = 'mysql') cursor = db.cursor() cursor.execute(query) @@ -317,7 +317,7 @@ if args.subnets is not None and args.subnets: query = query + "state IN (" + ','.join(map(str, args.states)) + ") ORDER BY ip_addr ASC" try: - db = MySQLdb.connect(read_default_file = args.config.name, read_default_group = 'mysql') + db = MySQLdb.connect(read_default_file = args.config, read_default_group = 'mysql') cursor = db.cursor() cursor.execute(query) From c8e5c9abf2f24ce4e9b1f41aeca78d3bc523432b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20GR=C3=89A?= Date: Sun, 23 Feb 2020 03:51:19 +0100 Subject: [PATCH 4/6] Renamed column dns_name to hostname --- phpipam-hosts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpipam-hosts b/phpipam-hosts index 0aef4a4..3a59643 100755 --- a/phpipam-hosts +++ b/phpipam-hosts @@ -299,7 +299,7 @@ if args.subnets is not None and not args.subnets: # ---- Host list / host file generation ---- -query = "SELECT dns_name,mac,ip_addr,state FROM ipaddresses WHERE " +query = "SELECT hostname,mac,ip_addr,state FROM ipaddresses WHERE " # Don't include hosts without specified MAC addresses by default. if args.no_mac is False: From fa7ab044a56b7ff10beb243438c58aa16e31a0a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20GR=C3=89A?= Date: Sun, 23 Feb 2020 03:59:31 +0100 Subject: [PATCH 5/6] Using sys.exit for errors and displaying warnings in stderr --- phpipam-hosts | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/phpipam-hosts b/phpipam-hosts index 3a59643..91aabe6 100755 --- a/phpipam-hosts +++ b/phpipam-hosts @@ -228,8 +228,7 @@ def listSections(): print(output) except db.Error as e: - print("Error %d: %s" % (e.args[0],e.args[1])) - sys.exit(1) + sys.exit("Error %d: %s" % (e.args[0],e.args[1])) finally: if db: @@ -265,8 +264,7 @@ def listSubnets(): print(output) except db.Error as e: - print("Error %d: %s" % (e.args[0],e.args[1])) - sys.exit(1) + sys.exit("Error %d: %s" % (e.args[0],e.args[1])) finally: if db: @@ -337,19 +335,19 @@ try: try: fqdn = row[0].strip() except: - print(("Ignoring entry with FQDN missing: %s" % row_str)) + print("Warning: Ignoring entry with FQDN missing: %s" % row_str, file=sys.stderr) continue hostname = fqdn.split('.')[0] try: ip = ipAddr(row[2]) except: - print(("Ignoring entry with IP missing: %s" % row_str)) + print("Warning: Ignoring entry with IP missing: %s" % row_str, file=sys.stderr) continue ip_real = ip try: state = row[3] except: - print(("Ignoring entry with STATE missing: %s" % row_str)) + print("Warning: Ignoring entry with STATE missing: %s" % row_str, file=sys.stderr) continue #entry = '' @@ -476,8 +474,7 @@ try: try: open(os.path.realpath(args.trigger),'w').close() except: - print("Error: cannot write to %s: access denied" % args.trigger) - sys.exit(1) + sys.exit("Error: cannot write to %s: access denied" % args.trigger) # If --restart is enabled and host list has been changed, restart # dhcpd daemon. @@ -485,8 +482,7 @@ try: os.system(args.restart_command) except: - print("Error: cannot write to %s: access denied" % args.output + '.tmp') - sys.exit(1) + sys.exit("Error: cannot write to %s: access denied" % args.output + '.tmp') # There is no previous host list, so let's create a new one right away # without a temporary file. @@ -502,8 +498,7 @@ try: try: open(os.path.realpath(args.trigger),'w').close() except: - print("Error: cannot write to %s: access denied" % args.trigger) - sys.exit(1) + sys.exit("Error: cannot write to %s: access denied" % args.trigger) # If --restart is enabled and host list has been changed, restart # dhcpd daemon. @@ -511,8 +506,7 @@ try: os.system(args.restart_command) except: - print("Error: cannot write to %s: access denied" % args.output) - sys.exit(1) + sys.exit("Error: cannot write to %s: access denied" % args.output) # There is no output else: @@ -523,8 +517,7 @@ try: open(os.path.realpath(args.output),'w').close() except db.Error as e: - print("Error %d: %s" % (e.args[0],e.args[1])) - sys.exit(1) + sys.exit("Error %d: %s" % (e.args[0],e.args[1])) finally: try: From 310927bfab34cf6f4ebf95725d476df9c684bdd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20GR=C3=89A?= Date: Sun, 23 Feb 2020 10:27:55 +0100 Subject: [PATCH 6/6] Deprecated function name --- phpipam-hosts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpipam-hosts b/phpipam-hosts index 91aabe6..a3e88d1 100755 --- a/phpipam-hosts +++ b/phpipam-hosts @@ -115,7 +115,7 @@ if args.states is None: # Load and parse specified configuration file -config = configparser.SafeConfigParser() +config = configparser.ConfigParser() config.read(args.config) if config.has_section(args.group):