Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2147786
  • 博文数量: 227
  • 博客积分: 10521
  • 博客等级: 上将
  • 技术积分: 3452
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-20 14:59
个人简介

低调做人,高调做事!

文章分类

全部博文(227)

文章存档

2013年(4)

2012年(8)

2011年(16)

2010年(24)

2009年(92)

2008年(83)

分类: LINUX

2008-10-09 17:20:37

#!/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) |
给主人留下些什么吧!~~