Write a Perl script for sending the remote MySQL command

Source: Internet
Author: User
Tags dsn perl script

To write some scripts that simplify administrative operations, here is one of the underlying scripts.

For a person who has never used a Perl script, it is still a bit difficult for me to go directly to the code.

This script is used to send the remote MySQL command and receive the result, the function is relatively simple, the later will gradually improve.


#!/usr/bin/perl use Getopt::long;use DBI;                   Getopt::long::getoptions (' host|h=s ' + \ $host, ' user|u=s ' and ' = ' $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 A Ccount name *-PW password for account *-p port for mysqld *-c command to execute *-help print help};=podif (!de Fined ($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 =<in>) {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 USAG E:no account input \ n $printh "};unless (defined ($password)) {die" wrong usage:no password input. \ n $printh "};unle  SS (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);}


How to use


[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.33command:select user () ******begin[email prote Cted]******end *****************************************************host:192.168.0.33command:select User () ****** Begin[email protected]******end *****************************************************[[email protected] ~]# Cat 2.   TXT 192.168.0.33 root xiaojun 3306192.168.0.33 root xiaojun 3306[[email protected] ~]#./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-*-p port for mysqld *-c command-execute *-help print help



Write a Perl script for sending the remote MySQL command

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.