in reply to Re: Perl script in cron
in thread Perl script in cron

Thanx for a quick answer. Im sorry Im not acustom with how this site works. Will make sure to be on my best behavior next time. You may already have answered my question though. I do rely on $ENV{'HOME'} when you menssioned it. Gonna check that out. Thanx again.

/Roger

Replies are listed 'Best First'.
Re^3: Perl script in cron
by marto (Cardinal) on Dec 19, 2006 at 09:47 UTC
    bashi,

    don't worry about your behaviour, you have not upset anyone here. Welcome to the Monastery, I am sure you will get used to this place soon :).

    Cheers,

    Martin
      OK, Im back, this time with the small newbie script Ive put together. Ive removed the $ENV{} for roots homedir and added the full path instead but I still get error att line 43(marked with comment and <---). Ive search on the site but cant seem to get any clues on whats going wrong here.

      #!/usr/bin/perl # # getsysinfo.pl - via ssh, get systeminformation about servers. # Note: This script depends on DSA/RSA key authentication # as we dont want passwords in the actual script so # you need to make sure you got this set up correctly # before this script will work. use DBI; use Net::SSH::Perl; $username = 'root'; %db = ( database => 'sysinfo', hostname => 'localhost', username => 'boogo', password => 'xxxxx'); # Hostnames of servers to check @servers = ('nw04','nw05','gw01'); # Commands for the checks to be executed. %checks = ( "disk" => 'df -h', "mem" => 'free -mot', "pci" => 'lspci -v', "net" => 'ifconfig' ); %results = {}; # Set up mysql connection $dsn = "DBI:mysql:database=$db{'database'}:host=$db{'hostname'}"; $db = DBI->connect($dsn, $db{'username'}, $db{'password'}) or die ("$!\n"); # lets walk the @servers array and do the tests and save results. foreach $server (@servers) { $ssh = Net::SSH::Perl->new($server, debug=>0, identity_files=>["/root/.ssh/id_rsa.pub"], protocol=>'2,1'); $ssh->login($username); # <--- ERROR returned +from cron to root's mailbox while (($key, $value) = each(%checks)) { if (&db_host_exists($server) > 0) { # host already exists in database, lets update it. ($stdout, $stderr, $exit) = $ssh->cmd($checks{$key}); $sth = $db->prepare(qq{ UPDATE servers SET $key='<pre>$stdout</pre>', updated=NOW() }); $sth->execute(); $sth->finish; } else { # host does not exists in database, lets insert it. ($stdout, $stderr, $exit) = $ssh->cmd($checks{$key}); $sth = $db->prepare(qq{ INSERT INTO servers (id, hostname, updated, $key) VALUES ('','$server',NOW(),'<pre>$stdout</pre>') }); $sth->execute(); $sth->finish; } } } # db_host_exists($) - subroutine to check if the host allready # exists in the database. Then we use update, # otherwise insert. # Arguments: $ - hostname to look for sub db_host_exists($) { $result = 0; $sth = $db->prepare(qq{ SELECT * FROM servers WHERE hostname LIKE '%$server%' }); $sth->execute(); if ($sth->rows > 0) { $result = 1; } $sth->finish; return $result; }

      Guess you monks will see my error right away.

      My cron line looks like this:
      0,30 * * * * /root/bin/getsysinfo.pl


      /Roger

        It has to do with the fact you are using the root account with Net::SSH::Perl. You have to specify an additional switch to get it to work.

        I don't remember specifically what I had to do to get to work, but looking in the documentation for Net::SSH::Perl you have to set a switch to work with a privileged port if you are root.

        If you don't get an answer here, you can always go to this mailing list. That was where I got my answer dealing with this module.