数据库是bdb的,每次都要用telnet ip port 然后上去输入指令,麻烦,写了一个perl脚本来查询用户。
-d domain 是查询域
-u user 是查询用户
调用IO::Socket模块来实现tcp通信,相当于就是telnet了。传递字符串给socket,相当于输入查询的命令。
程序写的很简陋,嘿嘿
#!/usr/bin/perl
sub queryuser{
my $server="192.168.132.213";
my $port=9002;
my $user=$_[0];
use IO::Socket;
my $sk = IO::Socket::INET->new(PeerAddr => $server,
PeerPort => $port,
Proto => 'tcp',
timeout =>30);
sub queryuser{
my $server="192.168.132.213";
my $port=9002;
my $user=$_[0];
use IO::Socket;
my $sk = IO::Socket::INET->new(PeerAddr => $server,
PeerPort => $port,
Proto => 'tcp',
timeout =>30);
if (!($sk)) {
print STDERR "fail to connect bdb slave:".$server."\n";
exit;
}
# print $sk "mget(\"test100\@sohu.net\")\n";
print $sk "mget(\"$user\")\n";
$echo=<$sk>;
chomp($echo);
@items=split/\ /,$echo;
$status=$items[0];
$data=$items[1];
if ( $status eq "-1" ) {
print "not exist this user:".$user."\n";
exit 0;
}
@newitems=split/\:/,$data;
@quota=$newitems[0];
$tongid=$newitems[1];
$maildir=$newitems[2];
if ( $quota eq "2" ) {
print "quota of user is full"."\n";
}
print "tongid is:".$tongid."\n";
print "maildir is:".$maildir."\n";
print STDERR "fail to connect bdb slave:".$server."\n";
exit;
}
# print $sk "mget(\"test100\@sohu.net\")\n";
print $sk "mget(\"$user\")\n";
$echo=<$sk>;
chomp($echo);
@items=split/\ /,$echo;
$status=$items[0];
$data=$items[1];
if ( $status eq "-1" ) {
print "not exist this user:".$user."\n";
exit 0;
}
@newitems=split/\:/,$data;
@quota=$newitems[0];
$tongid=$newitems[1];
$maildir=$newitems[2];
if ( $quota eq "2" ) {
print "quota of user is full"."\n";
}
print "tongid is:".$tongid."\n";
print "maildir is:".$maildir."\n";
}
sub querydomain{
my $server="192.168.41.194";
my $port=2000;
my $domain=$_[0];
use IO::Socket;
my $sk = IO::Socket::INET->new(PeerAddr => $server,
PeerPort => $port,
Proto => 'tcp',
timeout =>30);
sub querydomain{
my $server="192.168.41.194";
my $port=2000;
my $domain=$_[0];
use IO::Socket;
my $sk = IO::Socket::INET->new(PeerAddr => $server,
PeerPort => $port,
Proto => 'tcp',
timeout =>30);
if (!($sk)) {
print STDERR "fail to connect bdb master:".$server."\n";
exit;
}
# print $sk "mget(\"test100\@sohu.net\")\n";
print $sk "mdomget(\"$domain\"\,\"enabled\")\n";
$echo=<$sk>;
chomp($echo);
@items=split/\ /,$echo;
$status=$items[0];
$data=$items[1];
if ( $status ne "0" ) {
print "not exist this domain:".$domain."\n";
exit;
}
if ( $data eq "1" ) {
print "domain is ok"."\n";
}
elsif ( $data eq "0"){
print "domain is disabled"."\n";
}
}
print STDERR "fail to connect bdb master:".$server."\n";
exit;
}
# print $sk "mget(\"test100\@sohu.net\")\n";
print $sk "mdomget(\"$domain\"\,\"enabled\")\n";
$echo=<$sk>;
chomp($echo);
@items=split/\ /,$echo;
$status=$items[0];
$data=$items[1];
if ( $status ne "0" ) {
print "not exist this domain:".$domain."\n";
exit;
}
if ( $data eq "1" ) {
print "domain is ok"."\n";
}
elsif ( $data eq "0"){
print "domain is disabled"."\n";
}
}
sub usage {
printf "usage: [option] [arguments] \n";
printf "-d domain : query domain\n";
printf "-u user : query user\n";
}
printf "usage: [option] [arguments] \n";
printf "-d domain : query domain\n";
printf "-u user : query user\n";
}
if ( $#ARGV ne 1) {
&usage();
exit 0;
}
$option=$ARGV[0];
if ( $option eq "-d" ) {
$domain=$ARGV[1];
&querydomain($domain);
$domain=$ARGV[1];
&querydomain($domain);
}
elsif ( $option eq "-u" ) {
$user=$ARGV[1];
&queryuser($user);
}
else {
&usage();
exit 0;
}
$user=$ARGV[1];
&queryuser($user);
}
else {
&usage();
exit 0;
}
转载于:https://blog.51cto.com/coolerfeng/90993