perl的expect和xml模塊簡單使用

 

常用模塊:

    (1)        Net::FTP 
(2) Net::Telnet 
(3) LWP::Simple, get() 
(4) Expect 
(5) XML::Simple, XMLin() 
(6) Data::Dumper, Dumper() 
(7) IO::Socket 
(8) Date::Manip, DateCalc(), UnixDate() 
(9) Date::Manip, Date_Cmp() 
(10) File::Find, find() 
(11) ExtUtils::Installed, new(), modules(), version() 
(12) DBI, connect(), prepare(), execute(), fetchrow_array() 
(13) Getopt::Std 
(14) Proc::ProcessTable 
(15) Shell 
(16) Time::HiRes, sleep(), time() 
(17) HTML::LinkExtor, links(), parse_file() 
(18) Net::Telnet, open(), print(), getline() 
(19) Compress::Zlib, gzopen(), gzreadline(), gzclose() 
(20) Net::POP3, login(), list(), get() 
(21) Term::ANSIColor 
(22) Date::Calc Calendar(), Today() 
(23) Term::Cap, Tgetend(), Tgoto, Tputs() 
(24) HTTPD::Log::Filter 
(25) Net::LDAP 
(26) Net::SMTP mail(), to(), data(), datasend(), auth() 
(27) MIME::Base64, encode_base64(), decode_base64() 
(28) Net::IMAP::Simple, login(), mailboxes(), select(), get()... 
(29) Bio::DB::GenBank, Bio::SeqIO 
(30) Spreadsheet::ParseExcel 
(31) Text::CSV_XS, parse(), fields(), error_input() 
(32) Benchmark

Expect.pm 模塊:主要用來和另外一個server進行交互的,比如ftp,telnet等等。在寫一些監控腳步上,也會經常用到。
我所用到的方法很簡單,
spawn($command,@params),Forks and execs $command. Returns an Expect object upon success or  undef   if the fork was unsuccessful or the command could not be found. spawn() passes its parameters unchanged to Perls exec(), so look there for detailed semantics。起了一個新進程用來執行$command命令

new expect()->expect($timeout,$content),Given $timeout in seconds Expect will wait for $object's handle to produce one of the match_patterns, which are matched exactly by default. If you want a regexp match, prefix the pattern with '-re'.在$timeout內,期待出現包含$content的內容,默認是精確匹配,也可以使用正則表達 式,expect($timeout,re=>'')

new expect()->send($string):Sends the given strings to the spawned command. 發送消息

new expect()->debug(0|1|2): 打印debug 信息,不同的數字表示不同的bug級別

new expect()->interact(),和用戶進行交互,把控制權轉交給用戶

new expect()->soft_close(), 軟關閉,直到$timeout時間到達,才關掉該進程

new expect()->hard_close(),硬關閉,立刻關閉該進程

new expect()->match(),returns the string matched by the last expect() call, undef if no string was matched.返回匹配的結果

new expect()->match_number(),exp_match_number() returns the number of the pattern matched by the last expect() call. Keep in mind that the first pattern in a list of patterns is 1, not 0. Returns undef if no pattern was matched.返回匹配的個數

例子:
#!/usr/bin/perl
use Expect;
use strict;

my $timeout = 20;
my $cmd = "telnet";
my $exp = Expect->spawn($cmd,"192.168.0.1") or die "Can't spawn $cmd!";
$exp->expect($timeout,-re=>'[Ll]ogin:');
$exp->send("test/r/n");
$exp->expect($timeout,-re=>'[Pp]assword:');
$exp->debug(1);
$exp->send("test/r/n");
$exp->expect($timeout,-re=>'Last login');
$exp->send("ps -ef |grep java/r/n");
$exp->expect($timeout,-re=>'java');
print $exp->match_number();
$exp->soft_close();


XML::Simple: xml與perl的接口,既可以從xml中讀取數據以hash或散列的形式存放,也可以通過perl將數據格式化寫到xml文件裏。這裏主要介紹從xml中讀取結構化數據
XML::Simple->new()->XMLin(),從xml中讀取數據
XML::Simple->new()->XMLout(),往xml中寫數據
例子:
#!/usr/bin/perl -w
use strict;
use XML::Simple;
use Data::Dumper;
my $simple = XML::Simple->new();
my $data   = $simple->XMLin('pets.xml');
# DEBUG
print Dumper($data) . "/n";
# END
其中Dumper()用來查看放到內存中的數據結構
本例子pets.xml爲
<?xml version='1.0'?>
<pets>
<cat>
<name>Madness</name>
<dob>1 February 2004</dob>
<price>150</price>
</cat>
<name>Maggie</name>
<dob>12 October 2005</dob>
<price>75</price>
<owner>Rosie</owner>
</dog>
<cat>
<name>Little</name>
<dob>23 June 2006</dob>
<price>25</price>
</cat>
</pets>
dumpe()後的結果
$VAR1 = {
'cat' => {
'Little' => {
'dob' => '23 June 2006',
'price' => '25'
},
'Madness' => {
'dob' => '1 February 2004',
'price' => '150'
}
},
'dog' => {
'owner' => 'Rosie',
'dob' => '12 October 2005',
'name' => 'Maggie',
'price' => '75'
}
};
我們可以通過@{$VAR1{cat}}[0]->{dob}形式來訪問hash內容,該值爲"23 June 2006"

http://blog.chinaunix.net/u3/98375/showart_1968735.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章