#!/usr/bin/gawk -f # # # ipnumber [] / # # print all IP-number in the given network omiting the network # and broadcast address unless the -a option is given. # # If -c is set ipnumber prints configuration information (ipnum, # network address, broadcast and network mask) for the given network. # # # # 24JUN05wzk # # Added -c option. While working on the script I found that the # compl() function doesn't work proper. I assume that this is my # mistake since I run my self complied gawk. I replaced the compl() # calls with simple binary arithmetic. # # I found also some strange behaviour with 32bit integer computations, # was also solved by a workaround. # function ipval(str, i, n, x, val) { if ((n = split(str, x, ".")) != 4) return (0); val = x[1]; for (i=2; i<=4; i++) val = lshift(val, 8) + x[i]; return (val); } function ipmask(str, val) { if (index(str, ".") == 0) { str = str + 0; if (str < 0 || str > 32) str = 32; val = (str == 0)? 0: lshift(0xFFFFFFFF, 32 - str); } else { val = ipval(str); if (val == 0) val = 0xFFFFFFFF; } return (val); } function ipstr(val, i, str) { str = sprintf ("%d", and(val, 255)); for (i=2; i<=4; i++) { val = rshift(val, 8); str = sprintf ("%d", and(val, 255)) "." str; } return (str); } function nextarg(par, arg) { if (argi >= ARGC) { printf ("%s: missing argument: %s\n", program, par) >STDERR; exit (1); } arg = ARGV[argi]; ARGV[argi++] = ""; return (arg); } BEGIN { program = "ipnumbers"; STDERR = "/dev/stderr"; allnumbers = printconfig = 0; argi = 1; while (argi < ARGC && substr(ARGV[argi], 1, 1) == "-") { options = nextarg("option"); if (options == "--") break; for (i = 2; i<=length(options); i++) { c = substr(options, i, 1); if (c == "a") allnumbers = 1; else if (c == "c") printconfig = 1; else { printf ("%s: unkown option: -%s\n", program, c) >>STDERR; exit (1); } } } iparg = ARGV[argi]; ARGV[argi] = ""; if (index(iparg, "/") == 0) iparg = iparg "/" 32; k = index(iparg, "/"); ip = ipval(substr(iparg, 1, k-1)); mask = ipmask(substr(iparg, k+1)); if (printconfig != 0) { first = and(ip, mask); last = or(ip, xor(0xFFFFFFFF, mask)); printf ("%s\n%s\n%s\n%s\n", ipstr(ip), ipstr(first), ipstr(last), ipstr(mask)); } else { first = and(ip, mask); last = or(ip, xor(0xFFFFFFFF, mask)); if (last - first > 3 && allnumbers == 0) { first++; last--; } last = and(last, 0xFFFFFFFF); for (adr=first; adr <= last; adr++) printf ("%s\n", ipstr(adr)); } exit (0); }