source: branches/stable/cgi-bin/allocate.pl@ 276

Last change on this file since 276 was 188, checked in by Kris Deugau, 20 years ago

/branches/stable

Added allocate.pl to make allocation of arbitrary IP blocks easier.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 2.6 KB
RevLine 
[188]1#!/usr/bin/perl
2# Shell-based script to allocate arbitrary block
3###
4# SVN revision info
5# $Date: 2005-03-07 22:51:25 +0000 (Mon, 07 Mar 2005) $
6# SVN revision $Rev: 188 $
7# Last update by $Author: kdeugau $
8###
9
10use strict;
11use warnings;
12#use CGI::Carp qw(fatalsToBrowser);
13use Carp;
14use DBI;
15use CommonWeb qw(:ALL);
16use MyIPDB;
17use IBLink;
18use POSIX qw(ceil);
19use NetAddr::IP;
20
21use Sys::Syslog;
22
23openlog "IPDBshell","pid","local2";
24
25# Collect the username from the environment. If undefined, something
26# is Officially Hosed.
27my $authuser;
28if (!defined($ENV{'USER'})) {
29 die "Bad environment! USER not defined.\n";
30} else {
31 $authuser = $ENV{'USER'};
32}
33
34# Why not a global DB handle? (And a global statement handle, as well...)
35# Use the connectDB function, otherwise we end up confusing ourselves
36my $ip_dbh;
37my $sth;
38my $errstr;
39($ip_dbh,$errstr) = connectDB_My;
40if (!$ip_dbh) {
41 printAndExit("Failed to connect to database: $errstr\n");
42}
43checkDBSanity($ip_dbh);
44initIPDBGlobals($ip_dbh);
45
46# Hokay, now we can start to handle the allocation.
47
48my ($cidr, $type, $custid, $city, $desc, $alloc_from);
49# Check ARGV. We need some information to determine what to allocate.
50if (!$ARGV[1]) {
51 # Usage message
52 print "Usage: allocate.pl [IP/subnet] [Type] [CustID] [City] [\"Description\"]\n".
53 " Further information can be entered via the web interface\n";
54 exit;
55} else {
56 $cidr = new NetAddr::IP "$ARGV[0]";
57 $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr'");
58 $sth->execute;
59 my @data = $sth->fetchrow_array;
60# User deserves errors if user can't be bothered to find the free block first.
61 die "Can't allocate from outside a free block!!\n"
62 if !$data[0];
63 $alloc_from = new NetAddr::IP $data[0];
64 $sth->finish;
65 $type = $ARGV[1];
66 if (!$ARGV[4]) {
67 # Default desc
68 $desc = "DEFAULT: $disp_alloctypes{$type}";
69 } else {
70 $desc = $ARGV[4];
71 }
72 if (!$ARGV[3]) {
73 # Default city
74 $sth = $ip_dbh->prepare("select city from routed where cidr >>='$cidr'");
75 $sth->execute;
76 my @data = $sth->fetchrow_array;
77 $city = $data[0];
78 $sth->finish;
79 } else {
80 $city = $ARGV[3];
81 }
82 if (!$ARGV[2]) {
83 # Default custid - make it REAL obvious.
84 $custid = "FIXME";
85 } else {
86 $custid = $ARGV[2];
87 }
88}
89
90print "Allocating $cidr as $type to $custid in $city: '$desc'\n";
91
92my ($code,$msg) = allocateBlock($ip_dbh, $cidr, $alloc_from, $custid, $type, $city,
93 $desc, '', '');
94
95if ($code eq 'OK') {
96 print "Allocation OK!\n";
97 syslog "notice", "($authuser) Allocated '$cidr' to '$custid' as '$type'";
98} else {
99 print "Allocation failed! IPDB::allocateBlock said:\n$msg\n";
100 syslog "err", "($authuser) Allocation of '$cidr' to '$custid' as '$type' failed: '$msg'";
101}
102
103# Close it down.
104finish($ip_dbh);
Note: See TracBrowser for help on using the repository browser.