perl 远程 mysql_写的一个perl脚本,用于发送远程MySQL命令

这是一个使用Perl编写的脚本,用于发送远程MySQL命令并接收结果。脚本支持通过命令行参数指定数据库服务器、用户名、密码、端口和执行的SQL命令。此外,还可以从文件中读取多组服务器信息批量执行命令。提供了帮助信息以指导用户如何正确使用该脚本。
摘要由CSDN通过智能技术生成

想写一些简化管理操作的脚本,下面是基础脚本之一。

对于一个从来没使用过perl脚本的我来说还是有些难度的,直接上代码。

此脚本用于发送远程MySQL命令并且接收结果,功能比较简单,后面会渐渐完善。

#!/usr/bin/perl

use Getopt::Long;

use DBI;

Getopt::Long::GetOptions(

‘host|h=s‘ => \$host,

‘user|u=s‘ => \$user,

‘password|pw=s‘ => \$password,

‘port|p=s‘ => \$port,

‘command|c=s‘ => \$command,

‘groupfile|f=s‘ => \$groupfile,

‘help‘ => \$help

);

#print help info

my $printh=q{

usage :

mysqlcon -h 192.168.0.33 -u root -pw ‘xiaojun‘ -p 3306 -c ‘show global status‘

or

mysqlcon -g 2.txt -c ‘select user();‘

cat 2.txt:

192.168.0.33 root xiaojun 3306

192.168.0.34 root xiaojun 3306

options:

-h database server *

-u account name *

-pw password for account *

-p port for mysqld *

-c command to execute *

-help print help

};

=pod

if(!defined($host)){

print "page flag set to $page "

}

if(defined($user)){

print "user flag set to $user\n ";

}

if(defined($password)){

print "onoff flag set to $password \n";

}

if(defined($command)){

print "help flag set to $command \n";

}

if(defined($help)){

print $printh

}

=cut

sub execute_sql{

my $dsn = "DBI:mysql:database=mysql;host=$_[0]:$_[1]";

my ($dbh,$sth,@ary);

$dbh = DBI->connect($dsn,$_[2],$_[3],{‘RaiseError‘ => 1});

$sth = $dbh->prepare("$_[4]");

$sth->execute();

while(@ary = $sth->fetchrow_array()){

print join("\t",@ary),"\n";

}

$sth->finish;

$dbh->disconnect;

}

#&execute_sql($host,$port,$user,$password,$command) ;

unless (!defined($help)) { die "$printh" };

if(defined($groupfile)){

unless (defined($command)) { die "Wrong usage : No command input .\n $printh" };

open(IN,$groupfile);

while($line=){

my @args=split /\s/,$line;

print "host:@args[0]\ncommand:$command\n******BEGIN\n";

&execute_sql(@args[0],@args[3],@args[1],@args[2],$command) ;

print "******END \n\n\n*****************************************************\n\n\n";

}

}else{

unless (defined($host)) { die "Wrong usage : No host input .\n $printh" };

unless (defined($user)) { die "Wrong usage : No account input .\n $printh" };

unless (defined($password)) { die "Wrong usage : No password input .\n $printh" };

unless (defined($port)) { die "Wrong usage : No port number input .\n $printh" };

unless (defined($command)) { die "Wrong usage : No command input .\n $printh" };

&execute_sql($host,$port,$user,$password,$command) ;

}

使用方法

[[email protected] ~]# ./6.perl -h 192.168.0.33 -u root -pw ‘xiaojun‘ -p 3306 -c ‘select user()‘ ;

[email protected]

[[email protected] ~]# ./6.perl -g 2.txt -c ‘select user()‘

host:192.168.0.33

command:select user()

******BEGIN

[email protected]

******END

*****************************************************

host:192.168.0.33

command:select user()

******BEGIN

[email protected]

******END

*****************************************************

[[email protected] ~]# cat 2.txt

192.168.0.33 root xiaojun 3306

192.168.0.33 root xiaojun 3306

[[email protected] ~]# ./6.perl -help

usage :

mysqlcon -h 192.168.0.33 -u root -pw ‘xiaojun‘ -p 3306 -c ‘show global status‘

or

mysqlcon -g 2.txt -c ‘select user();‘

cat 2.txt:

192.168.0.33 root xiaojun 3306

192.168.0.34 root xiaojun 3306

options:

-h database server *

-u account name *

-pw password for account *

-p port for mysqld *

-c command to execute *

-help print help

原文:http://blog.csdn.net/renfengjun/article/details/41684865

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值