设为首页 收藏本站
查看: 995|回复: 0

[经验分享] perl的expect和xml模块简单使用

[复制链接]

尚未签到

发表于 2015-12-27 07:31:51 | 显示全部楼层 |阅读模式
  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"

关于更多的xml和perl接口的文章,可以参考此链接

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156771-1-1.html 上篇帖子: perl处理命令行参数 下篇帖子: perl 关于Url Encode 和 Url Decode
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表