ms133 发表于 2018-8-30 09:01:24

用 perl 把自己个shell 脚本连起来

  工作中经常要应为更新一个东西,要敲好几个脚本。 现在一句话搞定。
  现在用perl 把这几个shell 脚本连起来, 花了半天时间。哈哈,Perl时间长不用就是感觉生疏了。
  再次发挥perl 万能胶水的功能。废话少说,上码。
  $ cat test.pl
  #!/usr/local/bin/perl -w
  ###
  #auto update mrtg if this didnt work , please run
  #1.   Run   cfg txar1.lsi.comlsi2go      ( lsi2go is the snmp read string )
  #2.   Run   index txar1.lsi.comName:
  #At Name:   prompt, enter description like      Austin MPLS router
  #3.   Run   update txar1.lsi.com         ( repeat this 3 times until output messages stop )
  #4.   Run    add_image.pl txar1.lsi.com   (this add the picture for every link to the index.html)
  #Note: for the first time, when you create a new device, you have to /var/www/html/mrtg/txar1.lsi.com
  #And Run cp master.html index.html (then go to step 4)
  #
  #bo.zhang@lsi.com
  #2014/04/03
  $security_string = "lsi2go";
  $router_cat = $ARGV;
  #$_ = $argv;
  $mpls_router = "";
  $site_code = "";
  $site_name = "";
  $network_cat = "";
  $cfg = "/opt/mrtg/mrtg\-2.16.2/bin/cfg.snmp2 $ARGV $security_string";
  unless (system ($cfg)){
  print " we have already got the conf, $ARGV,$security_string\n";
  }
  #decide if this is MPLS netowrk or Internet netowrk
  #usually, r1 is MPLS network and r2/r5 is Internet network
  if ($router_cat =~ /r1/){
  $mpls_router = $&;
  $network_cat = "MPLS";
  print "network cat is $network_cat\n";
  }
  if ($router_cat =~/^/){
  $site_code = $&;
  print "site cose is $site_code\n";
  }
  print "$router_cat\n";
  # find the site name from site file
  if ( ! open SITE, "/opt/mrtg/mrtg\-2.16.2/bin/site.txt"){
  die" die error($!)";
  }
  while (){
  chomp;
  if ( $_ =~ /$site_code:/){
  $site_name = $';
  print " match is $&\n";
  print "$site_name\n";
  }
  }
  close SITE;
  print "$site_name $network_cat\n";
  print "$ARGV";
  #update index
  $index = "/opt/mrtg/mrtg\-2.16.2/bin/index1 $ARGV $site_name $network_cat";
  unless (system ($index)){
  print " we got here\n";
  }
  #update
  $update = "/opt/mrtg/mrtg\-2.16.2/bin/update $ARGV";
  unless (system ($update)){
  print " we got here udpate\n";
  }
  #add image to index.html
  $add = "/opt/mrtg/mrtg\-2.16.2/bin/add_image.pl $ARGV";
  unless (system ($add)){
  print " we got here add\n";
  }
  $ cat site.txt
  cip:Shanghai
  ciz:Shenzheng
  cib:Beijing

页: [1]
查看完整版本: 用 perl 把自己个shell 脚本连起来