日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

perl 远程 mysql_写的一个perl脚本,用于发送远程MySQL命令 -电脑资料

發(fā)布時間:2023/12/10 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 perl 远程 mysql_写的一个perl脚本,用于发送远程MySQL命令 -电脑资料 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

想寫一些簡化管理操作的腳本,下面是基礎(chǔ)腳本之一,

對于一個從來沒使用過perl腳本的我來說還是有些難度的,直接上代碼。

此腳本用于發(fā)送遠(yuǎn)程MySQL命令并且接收結(jié)果,功能比較簡單,后面會漸漸完善。

#!/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\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}=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("\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) ;}

使用方法

[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

總結(jié)

以上是生活随笔為你收集整理的perl 远程 mysql_写的一个perl脚本,用于发送远程MySQL命令 -电脑资料的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。