亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2169 | 回復: 6
打印 上一主題 下一主題

求助perl中循環(huán)問題 [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2010-03-25 16:49 |只看該作者 |倒序瀏覽
本帖最后由 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 ~]#

論壇徽章:
0
2 [報告]
發(fā)表于 2010-03-25 17:09 |只看該作者
foreach $ADD (keys %ADDLISTHASH)
這個 $ADD 是不是要local下
或者my下
新手

論壇徽章:
0
3 [報告]
發(fā)表于 2010-03-25 17:10 |只看該作者
回復 2# nuclearxin


    調整了一下,但是又出現(xiàn)了新問題,循環(huán)了很多次,本應該出來三組結果,但是現(xiàn)在出來了6組,三臺主機信息分別出現(xiàn)了3、2、1次,這是為什么啊?

論壇徽章:
0
4 [報告]
發(fā)表于 2010-03-25 17:14 |只看該作者
感覺還是 lexicaly scoped 定義的變量 比較混亂,想到啥就寫啥可以整理下
有必要用our嗎 (我不知道 覺得 my 就ok了。

論壇徽章:
0
5 [報告]
發(fā)表于 2010-03-25 17:19 |只看該作者
看住來啦
你while是負責賦值的
之后
在用 foreach
結果你。。。。。。。
一邊賦值 一邊 foreach

論壇徽章:
0
6 [報告]
發(fā)表于 2010-03-25 17:32 |只看該作者
回復 5# nuclearxin


    那我我是應該把while的括號括在foreache之前嗎?之前好像那樣做過,可是結果是foreach的時候只取到了最后一次的值,該怎么辦呢?

論壇徽章:
0
7 [報告]
發(fā)表于 2010-03-25 17:35 |只看該作者
回復 6# leavesdi


    ok啦,是我那個while循環(huán)鬧的,讓while循環(huán)結束在foreach之前,之前犯錯誤可能是因為我的變量沒弄明白,更改后代碼如下,僅供參考。

#!/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;
my @ADDLIST=split(/ /,$_); #define a private array,and using a blank to split the ADDLIST's content
#$ADDLISTHASH{ADDLIST} = $ADDLIST[1];
my $ADD = shift(@ADDLIST);
$ADDLISTHASH{$ADD} = \@ADDLIST;

#print "$ADD\n";
#print "@ADDLIST\n";
#print "@{$ADDLISTHASH{$ADD}}\n";
}

close ADDLIST; #close the file handle

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";
}
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP