作者 主題: Expect語法問題  (閱讀 5160 次)

0 會員 與 1 訪客 正在閱讀本文。

shane0711

  • 懷疑的國中生
  • **
  • 文章數: 32
    • 檢視個人資料
Expect語法問題
« 於: 2011-02-18 01:18 »
各位大大好,小弟最近想用expect搭配perl寫一支script來scan DELL server drac底下的網卡info ,但是目前只能一個一個IP查,希望可以將所有IP寫在一個host_ip.txt檔案
讓script讀取然後把值print出來,麻煩請各位大大提供一下意見,以下為我目前的語法

#!/usr/bin/perl

use Expect;
use Getopt::Long;

my $host;
my $user="root";
my $ssh="/usr/bin/ssh";
my $pwd="123456";
my $prompt=".*\/admin1.*";
my $search=".*iSCSI.*";
my $conn_timeout=60;
my $cmd_timeout=60;
my $send_cmd="racadm racdump";
my $log_stdout=0;
my $help;
my $result = GetOptions (
                "host=s"        =>      \$host,
                "user=s"        =>      \$user,
                "ssh=s"         =>      \$ssh,
                "pwd=s"         =>      \$pwd,
                "prompt=s"      =>      \$prompt,
                "regex=s"       =>      \$search,
                "sendcmd=s"     =>      \$send_cmd,
                "timeoutcon=i"  =>      \$conn_timeout,
                "timeoutcmd=i"  =>      \$cmd_timeout,
                "logstdout=i"   =>      \$log_stdout,
                "help"          =>      \$help
        );

die "Must specify -host" if !$host;
my @params;
push @params, "$user\@$host";
my $command=$ssh;
my $prmpt_regex=$prompt;
my $search_regex=$search;

my $e = new Expect();
$e->raw_pty(1);
$e->log_stdout($log_stdout);
$e->spawn($command,@params) or die "failed to spawn";

my $pat = $e->expect(10, "-re",".*password:.*");
$e->send("$pwd\n");

my $prm = $e->expect(10, "-re", $prompt);
$e->send("$send_cmd\n");

my @d  = $e->expect(15, "-re", $prompt);
$e->send("exit\n");
my @data = split("\n",$d[3]);

while (my $l = shift(@data)) {
        print "$l\n" if ( $l =~ $search);
}


另外,因為我還想同時search drac mac以及current ip ,如果要一次把這三個結果都print出來,該怎麼做呢?  麻煩請各位大大幫忙囉