用 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.com  lsi2go        ( 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)      
#
#[email protected]  
#2014/04/03


$security_string = "lsi2go";
$router_cat = $ARGV[0];
#$_ = $argv[0];
$mpls_router = "";
$site_code = "";
$site_name = "";
$network_cat = "";
$cfg = "/opt/mrtg/mrtg\-2.16.2/bin/cfg.snmp2 $ARGV[0] $security_string";
unless (system ($cfg)){
print " we have already got the conf, $ARGV[0],$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 =~/^[a-z][a-z][a-z]/){
   $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 (<SITE>){
chomp;
if ( $_ =~ /$site_code:/){
 $site_name = $';
 print " match is $&\n";
 print "$site_name\n";
}
}
close SITE;
print "$site_name $network_cat\n";
print "$ARGV[0]";

#update index
$index = "/opt/mrtg/mrtg\-2.16.2/bin/index1 $ARGV[0] $site_name $network_cat";
unless (system ($index)){

  print " we got here\n";
}

#update
$update = "/opt/mrtg/mrtg\-2.16.2/bin/update $ARGV[0]";
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[0]";
unless (system ($add)){
 print " we got here add\n";
}
$ cat site.txt
cip:Shanghai
ciz:Shenzheng
cib:Beijing

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