自分的に便利かもしれないツール

今さらこんなの作ってていいのかという話はナシの方向で。要IPy。ご利用はご自由に。

create table ipaddresstable (
  id integer primary key,
  address integer not null unique on conflict rollback,
  description text not null,
  state text not null,
  date timestamp not null
);
#!/usr/bin/python
# -*- coding:utf-8 -*-

import IPy
import sqlite3
import optparse
import time

def error() :
  print 'some error.'

def add_network(con, network, description, state) :
  if network == None or description == None or state == None :
    error()
    return
  t = time.strftime('%Y-%m-%d %H:%M:%S')
  net = IPy.IP(network)
  addresses = []
  for address in net :
    addresses.append(("%d" % address.int(), description.decode('utf-8'), state.decode('utf-8'), t))
  print 'add network ', network, description, state
  con.executemany("insert into ipaddresstable (address, description, state, date) values (?, ?, ?, datetime(?))", addresses)
  con.commit()

def modify_network(con, network, description, state) :
  if network == None or description == None or state == None :
    error()
    return
  t = time.strftime('%Y-%m-%d %H:%M:%S')
  net = IPy.IP(network)
  addresses = []
  for address in net :
    addresses.append((description.decode('utf-8'), state.decode('utf-8'), t, "%d" % address.int()))
  print 'modify network ', network, description, state
  con.executemany("update ipaddresstable set description = ?, state = ?, date = datetime(?) where address = ?", addresses)
  con.commit()

def remove_network(con, network) :
  if network == None :
    error()
    return
  net = IPy.IP(network)
  addresses = []
  for address in net :
    addresses.append(("%d" % address.int(), ))
  print 'remove network ', network
  con.executemany("delete from ipaddresstable where address = ?", addresses)
  con.commit()

def view_network(con, network) :
  net = IPy.IP(network)
  n_network = net.net().int()
  n_broadcast = net.broadcast().int()
  for row in con.execute("select address, description, state, date from ipaddresstable where address >= ? and address <= ?", (n_network, n_broadcast)) :
    print "%s, %s, %s, %s" % (IPy.IP(row[0]), row[1], row[2], row[3])

def main() :
  parser = optparse.OptionParser()
  parser.add_option('-t', '--database', dest='database', action='store', default='ipaddress.db', help='database file')
  
  parser.add_option('-a', '--add',    dest='command', action='store_const', const='add', help='add network')
  parser.add_option('-m', '--modify', dest='command', action='store_const', const='modify', help='modify description or state')
  parser.add_option('-r', '--remove', dest='command', action='store_const', const='remove', help='remove network')
  parser.add_option('-v', '--view',   dest='command', action='store_const', const='view', help='view network')
  
  parser.add_option('-n', '--network',     dest='network',     action='store', help='network (xxx.xxx.xxx.xxx/yy, xxxx:xxxx:xxxx::/yy, etc.)')
  parser.add_option('-d', '--description', dest='description', action='store', help='network description')
  parser.add_option('-s', '--state',       dest='state',       action='store', help='network state (used, unused, reserved, etc.)')
  
  (options, args) = parser.parse_args()

  con = sqlite3.connect(options.database)
    
  if options.command == 'add' :
    add_network(con = con, network = options.network, description = options.description, state = options.state)
  elif options.command == 'modify' :
    modify_network(con = con, network = options.network, description = options.description, state = options.state)
  elif options.command == 'remove' :
    remove_network(con = con, network = options.network)
  elif options.command == 'view' :
    view_network(con = con, network = options.network)
  else :
    con.close()
    parser.print_help()
  con.close()


if __name__ == '__main__' :
  main()

使ってるライブラリがIPv6に対応してるので、IPv6でも使えるけど、xxxx.xxxx.xxxx.xxxx::/64とか指定すると、きっとひどいめにあうので、使わないほうがよさげ。というか、Excelで用が足りることなんだけど。