File: portsentry-add-ip

package info (click to toggle)
portsentry 1.2-11.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 464 kB
  • ctags: 157
  • sloc: ansic: 2,283; makefile: 196; sh: 186; perl: 94; csh: 31
file content (38 lines) | stat: -rw-r--r-- 1,067 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/perl -w
# $Id: portsentry-add-ip,v 1.3 2001/07/01 22:51:00 agx Exp $
#
# add an IP to portsentry.ignore.static file
# by Guido Guenther <agx@debian.org>

$etcdir='/etc/portsentry';
$static_file='portsentry.ignore.static';
$ip_regexp='[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';

if( @ARGV == 0) {die "Adds an ip to portsentry.ignore.static.\
usage: portsentry-add-ip ip-address\n";}
else {
	$new_ip = $ARGV[0];
}

# do sanity check on given ip
if ( $new_ip !~ /^$ip_regexp$/) {
	die "$new_ip is not in aaa.bbb.ccc.ddd format!\n";
}
print "Debug: trying to add $new_ip\n" if defined($DEBUG);

# read ignore file
open( P, "$etcdir/$static_file") || die "Can't read $etcdir/$static_file: $!\n";
@ignored_ips = <P>;
close( P);

# reopen it for writing
$mode='>>';
open( P, "$mode$etcdir/$static_file") || die "Can't write to $etcdir/$static_file: $!\n";

# check if ip is already in the ignore file, if not append it
if( !grep( /^\s*$new_ip[\s#]/, @ignored_ips)) {
	print "Debug: adding $new_ip!\n" if defined($DEBUG);
	print P "$new_ip\n";
}
close( P);