MySQL&Perl,便利之合数据库
1.MySQL&Perl,便利之合数据库 篇一
想写一些简化管理操作的脚本,下面是基础脚本之一,
对于一个从来没使用过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 3306192.168.0.34 root xiaojun 3306options:-h database server *-u account name *-pw password for account *-p port for mysqld *-c command to execute *-help print help};=podif(!defined($host)){ print “page flag set to $page ”}if(defined($user)){ print “user flag set to $user ”;}if(defined($password)){ print “onoff flag set to $password ”;}if(defined($command)){ print “help flag set to $command ”;}if(defined($help)){ print $printh}=cutsub 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(“ ”,@ary),“”;}$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 . $printh” }; open(IN,$groupfile); while($line= 使用方法 [root@centos511 ~]# ./6.perl -h 192.168.0.33 -u root -pw ‘xiaojun‘ -p 3306 -c ‘select user()‘ ;root@192.168.0.33[root@centos511 ~]# ./6.perl -g 2.txt -c ‘select user()‘ host:192.168.0.33command:select user()******BEGINroot@192.168.0.33******END *****************************************************host:192.168.0.33command:select user()******BEGINroot@192.168.0.33******END *****************************************************[root@centos511 ~]# cat 2.txt 192.168.0.33 root xiaojun 3306192.168.0.33 root xiaojun 3306[root@centos511 ~]# ./6.perl -helpusage : 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 3306192.168.0.34 root xiaojun 3306options:-h database server *-u account name *-pw password for account *-p port for mysqld *-c command to execute *-help print help ★ Shell脚本检查IP格式及mysql操作实例 ★ mysql不支持union select的盲注方法脚本安全 ★ 一个基于mysql的登陆验证程序数据库 ★ 动漫脚本 范文 ★ 动画脚本范文 ★ MySQL数据类型解析 ★ 情景党课脚本 ★ 常见的MySQL解决方案 ★ 状态性写作 ★ 人生状态用词 【MySQL&Perl,便利之合数据库】推荐阅读: 怎样掌握MySQL数据库中动态表特征11-14 谈谈数据从sql server数据库导入mysql数据库的体验07-07 酒便利分析08-19 便利店合作合同07-16 学校便利店分析10-06 便利店加盟协议03-13 便利店月工作总结10-08 便利店服务话术10-30