#!/usr/bin/perl
# Use mandatory external modules
use strict;
use warnings;
use DBI;
my $host = '192.168.90.X';
my $user = 'test';
my $pass = 'test';
my $port = '3306';
my $database = 'extmail';
my $table = 'alias';
my $filename = '/usr/local/src/user.txt';
open FH, "< $filename" or die "can't open $filename ..... ($!)";
# connect to servers (this)
our $this_dbh = &MysqlConnect( $host, $port, $user, $pass );
print "ERROR: Can't connect to MySQL (host = $host:$port, user = $user)!"
if ( !$this_dbh );
# insert user data
foreach () {
chomp $_;
my $mailbox = "$_\@cisco.cn";
my $alias_mailbox = "$_\@sales\.cisco.cn";
print "$mailbox\n";
print "$alias_mailbox\n";
my $sql = "INSERT INTO $database\.$table (address, goto) VALUES ('$mailbox', '$alias_mailbox')";
print "$sql\n";
my $quota_status = &MysqlChange( $this_dbh, $sql );
}
# Get user quota
my $sql = "select * from $database\.$table";
print "$sql\n";
my $user_list = &MysqlQuery( $this_dbh, $sql );
print "$user_list\n";
#-----------------------------------------------------------------
sub MysqlConnect($$$$) {
my ( $host, $port, $user, $pass ) = @_;
my $dsn = "DBI:mysql:host=$host;port=$port";
return DBI->connect( $dsn, $user, $pass, { PrintError => 0 } );
}
#-----------------------------------------------------------------
sub MysqlQuery($$) {
my ( $dbh, $query ) = @_;
my $sth = $dbh->prepare($query);
my $res = $sth->execute;
return undef unless ($res);
while ( my @row = $sth->fetchrow_array() ) {
print("@row \n");
}
$sth->finish;
}
#-----------------------------------------------------------------
sub MysqlChange($$) {
my ( $dbh, $change ) = @_;
my $sth = $dbh->do($change);
}
阅读(1223) | 评论(0) | 转发(1) |