- 论坛徽章:
- 1
|
CPU status script
提供一個過去我寫的Expect程序...
這個程序主要是要跨兩個網段,
先連至ip1,再連到ip2的機器去下指令,
將結果放在log檔中..
我這個程序只有一半..事實上$exp這個handle並未完結..
要下$exp->;hard_close();才算結束..
或者$exp->;soft_close();
有興趣的請自行研究...
#!/usr/bin/perl
#----------------------------------------------------------
# 目的:連上某台主機
# 更改日期: 2002/11/13
# 開發者:apile
#----------------------------------------------------------
use Expect;
#----------------------------
#$_ip1 = "10.xxx.xx.xx";
#$_ip2 = "172.xxx.xxx.xxx";
#$log = "/tmp/1.log";
#&P_Telnet($_ip1,$_ip2,$log);
#$exp->;hard_close();
#----------------------------
sub PP_Telnet{
($_IP1,$_IP2,$LOG_FILE) = @_;
#----------------------------------------------------------
$_ID1 = "xxx";
$_PWD1= "xxxx12";
$_ID2 = "xxxx";
$_PWD2 = "xxxx12";
#----------------------------------------------------------
# Time out 30秒
$timeout = 30;
$exp = new Expect;
## 設定terminal type
#$exp->;stty(qw(-raw -echo));
## 重設定buffer 大小
#$exp->;match_max(1024);
## 重設定timeout時間(有收到東西就重設定)
$exp->;restart_timeout_upon_receive(1);
## 看詳細內容
#$exp->;exp_internal(1);
## 開啟Debug模式
#$exp->;debug(2);
#$exp->;send_slow(0);
## 顯示標準輸出
$exp->;log_stdout(0);
$exp->;spawn("telnet $_IP1" || die "cannot spawn telnet: $!\n";
$error_value =0;
$spawn_ok =0;
$exp->;expect( $timeout,
[ qr'login: $',
sub {
$spawn_ok = 1;
my $fh = shift;
$fh->;send("$_ID1\r"
$fh->;clear_accum();
exp_continue; } ],
[ qr'sword: $' ,
sub {
$spawn_ok = 2;
my $fh = shift;
$fh->;send("$_PWD1\r"
$fh->;clear_accum();
exp_continue; } ],
[ qr'Login incorrect',\&_error1],
[ eof =>; \&_error1 ],
[ timeout =>; \&_error1 ],
'-re', qr'\012\$ $',
sub {
my $fh = shift;
$fh->;send("telnet $_IP2\r"
}
);
$spawn_ok = 0;
$exp->;expect( $timeout,
[ qr'Invalid name.',\&_error2],
[ qr'Invalid password.',\&_error2],
[ qr'Login:\s+$',
sub {
$spawn_ok = 1;
my $fh = shift;
$fh->;send("$_ID2\r"
$fh->;clear_accum();
exp_continue; } ],
[ qr'sword: $' ,
sub {
$spawn_ok = 2;
my $fh = shift;
$fh->;send("$_PWD2\r"
$fh->;clear_accum();
exp_continue; } ],
[ eof =>;\&_error2 ],
[ timeout =>; \&_error2 ],
'-re', qr'\d+>; $',
sub {
my $fh = shift;
$fh->;send("要下的指令\r"
}
);
$exp->;expect( $timeout,
[ eof =>;\&run_error ],
[ timeout =>; \&run_error ],
'-re', qr'>; $', ##要匹配的prompt
);
}
sub run_error{
$exp->;log_file($LOG_FILE);
$exp->;log_file->;print("\r\n 從 $_IP2的無法下指令##\r\n"
$error_value = 999;
}
sub _error1 {
$exp->;log_file($LOG_FILE);
if($spawn_ok == 1) {
$exp->;log_file->;print("\r\n 無法登錄主機 $_IP1,可能帳號密碼錯誤\r\n"
$exp->;log_file->;print(" 請洽人員處理\n"
}elsif($spawn_ok == 2) {
$exp->;log_file->;print("\r\n 無法登錄主機 $_IP1,可能帳號密碼錯誤\r\n");
$exp->;log_file->;print(" 請洽人員處理\r\n");
}else{
$exp->;log_file->;print("\r\n 無法連線至$_IP1 主機。\r\n");
$exp->;log_file->;print(" 請洽人員處理\r\n");
}
$error_value = 10;
$exp->;log_file(undef);
# $exp->;hard_close;
die "10\n";
}
sub _error2 {
$exp->;log_file($LOG_FILE);
if($spawn_ok == 1) {
$exp->;log_file->;print("\r\n 無法登錄$_IP2 ,可能帳號密碼錯誤\r\n");
$exp->;log_file->;print(" 請洽人員處理\r\n");
}elsif($spawn_ok == 2) {
$exp->;log_file->;print("\r\n無法登錄$_IP2,可能帳號密碼錯誤\r\n");
$exp->;log_file->;print(" 請洽人員處理\r\n");
}else{
$exp->;log_file->;print("\r\n 無法連線至$_IP2 Passport\r\n");
$exp->;log_file->;print(" 請洽人員處理\r\n");
}
$error_value = 12;
$exp->;log_file(undef);
# $exp->;hard_close;
die "12\n";
}
1; |
|