- 論壇徽章:
- 0
|
本帖最后由 leavesdi 于 2010-03-25 17:09 編輯
大家好,我剛剛開始學習perl,打算使用SSH模塊監(jiān)控其他機器cpu及內存情況,其中使用了兩個循環(huán),但是發(fā)現(xiàn)結果有問題,好像是進行了多次循環(huán),但是我又找不出問題,麻煩大家?guī)兔匆幌拢x謝。
perl腳本:#!/usr/bin/perl -w
use Net::SSH::Expect;
our $ADDIP="/root/hostip";
our %ADDLISTHASH; #define a hash
open ADDLIST,"$ADDIP"; #open the file ADDIP by using the file handle ADDLIST
while (<ADDLIST>){
chomp;
our @ADDLIST=split(/ /,$_); #define a private array,and using a blank to split the ADDLIST's content
#$ADDLISTHASH{ADDLIST} = $ADDLIST[1];
our $ADD = shift(@ADDLIST);
$ADDLISTHASH{$ADD} = \@ADDLIST;
#print "$ADD\n";
#print "@ADDLIST\n";
#print "@{$ADDLISTHASH{$ADD}}\n";
foreach $ADD (keys %ADDLISTHASH) {
my $PASS = $ADDLISTHASH{$ADD}[0];
my $ssh = Net::SSH::Expect->new (
host => "$ADD",
password=> "$PASS",
user => 'root',
raw_pty => 1
);
my $cpuanddisk = 'vmstat';
my $memstat = 'free';
$ssh->login();
$ssh->exec("stty raw -echo");
my $sysstate = $ssh->exec("$cpuanddisk");
my $memstate = $ssh->exec("$memstat");
print "$sysstate\n";
print "$memstate\n";
}
}
close ADDLIST; #close the file handle
運行結果如下:[color=Grayprocs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 0 384436 64580 476596 0 0 2 4 64 39 0 0 100 0
[root@xxjh ~]#
total used free shared buffers cached
Mem: 1035864 651492 384372 0 64580 476596
-/+ buffers/cache: 110316 925548
Swap: 1044216 0 1044216
[root@xxjh ~]#
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 0 384292 64580 476600 0 0 2 4 64 39 0 0 100 0
[root@xxjh ~]#
total used free shared buffers cached
Mem: 1035864 651572 384292 0 64580 476600
-/+ buffers/cache: 110392 925472
Swap: 1044216 0 1044216
[root@xxjh ~]#
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 85220 4072 12672 16292 0 0 1 1 2 1 0 1 99 0
[root@lvwdtest5 ~]#
total used free shared buffers cached
Mem: 126148 122076 4072 0 12672 16292
-/+ buffers/cache: 93112 33036
Swap: 265064 85220 179844
[root@lvwdtest5 ~]#
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 0 384220 64580 476600 0 0 2 4 64 39 0 0 100 0
[root@xxjh ~]#
total used free shared buffers cached
Mem: 1035864 651644 384220 0 64580 476600
-/+ buffers/cache: 110464 925400
Swap: 1044216 0 1044216
[root@xxjh ~]#
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 85220 4044 12696 16292 0 0 1 1 2 1 0 1 99 0
[root@lvwdtest5 ~]#
total used free shared buffers cached
Mem: 126148 122104 4044 0 12696 16292
-/+ buffers/cache: 93116 33032
Swap: 265064 85220 179844
[root@lvwdtest5 ~]#
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 119396 177040 2377424 0 0 0 2 1 2 0 0 100 0 0
[root@syslog ~]#
total used free shared buffers cached
Mem: 3107300 2987904 119396 0 177040 2377424
-/+ buffers/cache: 433440 2673860
Swap: 955856 0 955856
[root@syslog ~]# |
|