optparse_example.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #! /usr/bin/env python
  2. """
  3. Example of using optparse.
  4. ipAddr - command line wrapper to ipAddr
  5. By default, take a IPv4 addresses or CIDR blocks on command line.
  6. Print out various selected fields using ipAddr.
  7. Usage:
  8. ipAddr.py [--all] [--help] [options] cidr [cidr...]
  9. """
  10. import ipaddr
  11. import optparse
  12. import sys
  13. __author__ = 'George Jones'
  14. __maintainer__ = 'George Jones'
  15. __email__ = 'gmj@pobox.com'
  16. __version__ = '0.0.1'
  17. #
  18. # ipAddr calculator
  19. #
  20. #
  21. #
  22. # or simply
  23. #
  24. # sort -u FOO
  25. #
  26. # in python
  27. def p_error(msg=None):
  28. optp.print_help()
  29. if msg:
  30. optp.error(msg)
  31. sys.exit(1)
  32. def parse_args(argv):
  33. global optp
  34. usage = """
  35. %prog [--all] [options] cidr [cidr...]
  36. """
  37. # Parse arguments.
  38. optp = optparse.OptionParser(description=__doc__.strip(), version=__version__,
  39. usage=usage)
  40. # Flag for all known netblock info
  41. optp.add_option('-A', '--all', help='Print all fields',
  42. action='store_true')
  43. # Flag for each
  44. optp.add_option('-H', '--hostmask', help='print hostask',
  45. action='store_true', default=True)
  46. optp.add_option('-N', '--netmask', help='print netask',
  47. action='store_true', default=True)
  48. optp.add_option('-I','--ip', help='print ip', action='store_true', default=True)
  49. optp.add_option('-K','--is_link_local', help='print is_link_local', action='store_true')
  50. optp.add_option('-L','--is_loopback', help='print is_loopback', action='store_true')
  51. optp.add_option('-M','--is_multicast', help='print is_multicast', action='store_true')
  52. optp.add_option('-P','--is_private', help='print is_private', action='store_true')
  53. optp.add_option('-R','--is_reserved', help='print is_reserved', action='store_true')
  54. optp.add_option('-U','--is_unspecified', help='print is_unspecified', action='store_true')
  55. optp.add_option('-S','--masked', help='print masked', action='store_true')
  56. optp.add_option('-X','--max_prefixlen', help='print max_prefixlen', action='store_true')
  57. optp.add_option('-W','--network', help='print network', action='store_true', default=True)
  58. optp.add_option('-O','--numhosts', help='print numhosts', action='store_true', default=True)
  59. optp.add_option('-F','--prefixlen', help='print prefixlen', action='store_true', default=True)
  60. optp.add_option('-B','--subnet', help='print subnet', action='store_true')
  61. optp.add_option('-T','--supernet', help='print supernet', action='store_true')
  62. optp.add_option('-V','--ipversion', help='print version', action='store_true', default=True)
  63. optp.add_option('--with_hostmask', help='print with_hostmask', action='store_true')
  64. optp.add_option('--with_netmask', help='print with_netmask', action='store_true')
  65. optp.add_option('--with_prefixlen', help='print with_prefixlen', action='store_true')
  66. # Parse arguments
  67. (opts, args) = optp.parse_args()
  68. return opts, args
  69. def main():
  70. global opts
  71. opts, args = parse_args(sys.argv)
  72. # Check for conflicting options here
  73. if len(args) == 0:
  74. p_error('Must supply at least one IP address')
  75. for arg in args:
  76. try:
  77. net = ipaddr.IPv4Network(arg)
  78. except (IPv4IpValidationError, IPv4NetmaskValidationError):
  79. pass
  80. print(("network:", net))
  81. if opts.all or opts.hostmask:
  82. print(" hostmask: ", net.hostmask)
  83. if opts.all or opts.netmask:
  84. print(" netmask: ", net.netmask)
  85. if opts.all or opts.ip:
  86. print(" ip:", net.ip)
  87. if opts.all or opts.is_link_local:
  88. print(" is_link_local: ", net.is_link_local)
  89. if opts.all or opts.is_loopback:
  90. print(" is_loopback: ", net.is_loopback)
  91. if opts.all or opts.is_multicast:
  92. print(" is_multicast: ", net.is_multicast)
  93. if opts.all or opts.is_private:
  94. print(" is_private: ", net.is_private)
  95. if opts.all or opts.is_reserved:
  96. print(" is_reserved: ", net.is_reserved)
  97. if opts.all or opts.is_unspecified:
  98. print(" is_unspecified: ", net.is_unspecified)
  99. if opts.all or opts.max_prefixlen:
  100. print(" max_prefixlen: ", net.max_prefixlen)
  101. if opts.all or opts.network:
  102. print(" network: ", net.network)
  103. if opts.all or opts.numhosts:
  104. print(" numhosts: ", net.numhosts)
  105. if opts.all or opts.prefixlen:
  106. print(" prefixlen: ", net.prefixlen)
  107. if opts.all or opts.ipversion:
  108. print(" ipversion: ", net.version)
  109. if opts.all or opts.with_prefixlen:
  110. print(" with_prefixlen: ", net.with_prefixlen)
  111. if __name__ == '__main__':
  112. main()