Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How to connect to remote machine

by aalneyperl (Acolyte)
on Jan 22, 2008 at 12:14 UTC ( [id://663557]=perlquestion: print w/replies, xml ) Need Help??

aalneyperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have mysql health chek script on my local machine. I run the script which connects to a remote mysql databases and fetches mysql variables.However i want to fetch the OS parameters like free memory,total RAM of the remote machine.Both the local and host machine are linux based.I have read abt NET::SSH on CAPN however i was unable to find a simple script which shows me the commands or steps to do the connection.Please let me know which NET::?? module to use.Can anybody add the connection code to my script. I would really appreciate this.

# Health check the health of a mysql server.

use strict; use warnings; use Getopt::Long; use DBI; # -- # Print out the usage message # -- sub usage { print " MySQL Health Check Script\n"; print " Maintained By DataVail.com\n"; print " Important Usage Guidelines\n"; print " perl MysqlHC.pl -H <host> -u <user> -p <password> -role <s +erver_role>\n"; print " --role <OLTP,Read-slave,Reporting,Datawarehouse> \n" +; print " Optional parameters:\n"; print " --port <port> \n"; } $|=1; # -- # Parse arguments and read Configuration # -- my ($host, $user, $password, $port, $role, $help); GetOptions ( 'host=s' => \$host, 'H=s' => \$host, 'user=s' => \$user, 'u=s' => \$user, 'password=s' => \$password, 'p:s' => \$password, 'port=i' => \$port, 'role=s' => \$role, 'help' => \$help, ); if (!$host || !$user || !$role || !$help eq '') { usage(); exit(1); } if (!$port) { $port = 3306; } sub var { my $statement; if ($MySQLVersion =~ /^3.*|^4.*/ ) { $statement = 'show variables'; } elsif ($MySQLVersion =~ /^5\.0.*/ ) { $statement = 'show global variables'; } elsif ($MySQLVersion =~ /^5\.[1-9].*|^6.*/ ) { $statement = 'Select * from information_schema.global_variable +s order by 1'; }; # print $statement,"\n"; my $sth = $dbh->prepare($statement); $sth->execute(); while (my ($keyword, $value) = $sth->fetchrow_array()) { # print $keyword,"=",$value,"\n"; $variables{lc($keyword)}=lc($value); } $sth->finish(); } sub stat { my $statement; if ($MySQLVersion =~ /^3.*|^4.*/ ) { $statement = 'show status'; } elsif ($MySQLVersion =~ /^5\.0.*/ ) { $statement = 'show global status'; } elsif ($MySQLVersion =~ /^5\.[1-9].*|^6.*/ ) { $statement = 'Select * from information_schema.global_status o +rder by 1'; }; # print $statement,"\n"; my $sth = $dbh->prepare($statement); $sth->execute(); while (my ($keyword, $value) = $sth->fetchrow_array()) { # print $keyword,"=",$value,"\n"; $status{lc($keyword)}=lc($value); } $sth->finish(); } system_memory=`cat /proc/meminfo |grep -w MemTotal |awk '{print $2}'` total_system_memory=$(echo "$system_memory * 1024" | bc -l)

Update:

#!/usr/bin/perl -w use strict; use warnings; use Net::SSH qw(ssh); my $user = "abhishek"; my $hostname = "dpsharp"; my $command = "`cat /proc/meminfo |grep -w MemTotal |awk '{print $2} +'`";--->line 9 ssh('user@hostname', $command);
when i ran the script is gave me this error
Use of uninitialized value in concatenation (.) or string at ./ssh.pl +line 9. ssh: hostname: Name or service not known
please let me know my mistake When i ran the following code it gives me this error
#!/usr/bin/perl -w use strict; use warnings; use Net::SSH qw(ssh); my $user = "root"; my $hostname = "orarac1"; my $command = "cat /proc/meminfo |grep -w MemTotal |awk '{print \$2} +'"; ssh("$user\@$hostname", "$command"); Host key verification failed.
can anybody help me in this.

20080128 Janitored by Corion: Restored original post, update marked and appended as such

Replies are listed 'Best First'.
Re: How to connect to remote machine
by talexb (Chancellor) on Jan 22, 2008 at 13:52 UTC
      .. However i want to fetch the OS parameters like free memory,total RAM of the remote machine.Both the local and host machine are linux based ..

    This may be like using an eighteen-wheeler to deliver an envelope, but I've really enjoyed using Nagios to monitor my collection of servers and services. It uses plugins to collect important information about a server's health.

    Failing that, you may be able to build something using the Net::SSH and Expect modules.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      I second the Nagios recommendation. It may be an eighteen-wheeler, but to set it up with a small config like this is very quick.

      On the other hand, since ssh access is already set up, the nagios check plugins can be installed on the remote server without bothering to set up a full blown Nagios server. Then just use a script to ssh in and run one of the nagios checks and take action based on the exit code of the plugin.

      --
      naChoZ

      Therapy is expensive. Popping bubble wrap is cheap. You choose.

Re: How to connect to remote machine
by hipowls (Curate) on Jan 22, 2008 at 13:55 UTC

    The synopsis in Net::SSH2 gives an example of what you want to do.

    # Shamelessly cut and paste from said synopsis;) use Net::SSH2; my $ssh2 = Net::SSH2->new(); $ssh2->connect('example.com') or die; if ($ssh2->auth_keyboard('fizban')) { my $chan = $ssh2->channel(); $chan->exec('program'); my $sftp = $ssh2->sftp(); my $fh = $sftp->open('/etc/passwd') or die; print $_ while <$fh>; }

Re: How to connect to remote machine
by TOD (Friar) on Jan 22, 2008 at 13:34 UTC
    check the database user's permissions/ grants.
    --------------------------------
    masses are the opiate for religion.
      You're getting the uninitialized value error because $2 is not escaped. Try this:

      my $command = `cat /proc/meminfo |grep -w MemTotal |awk '{print \$2} + +'`;
Re: How to connect to remote machine
by jasonk (Parson) on Jan 22, 2008 at 18:47 UTC

    The error message you got is pretty clear, you told it ssh to a host named 'hostname', and it couldn't find a host with that name. Perhaps you wanted to ssh to "$user\@$hostname" instead of 'user@hostname'?


    We're not surrounded, we're in a target-rich environment!
      hi, I corrected that ssh('$user\@$hostname','$command'); and still it gives me the same error.:(

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://663557]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found