perl ping检测脚本

我的第一个用于生产环境的perl脚本,虽然不是很优秀,但也迈出了扎实的一步 :)
领导有任务,给一批IP列表,ping每一台机器,如果没有响应就发邮件通知,通知的邮件需要分开,不能通知一个列表,得一封一封的通知.
用到email::send模块,因为需要用到Gmail

  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Email::Send;
  5. use Email::Send::Gmail;
  6. use Email::Simple::Creator;
  7. my @list = qw/
  8. 1.1.1.1
  9. 2.2.2.2
  10. 3.3.3.3
  11. /;
  12. foreach my $re (@list){
  13.         my $p = `ping $re -c 3`;
  14.         if($p=~/100% packet loss/){
  15.                 my $email = Email::Simple->create(
  16.                 header => [
  17.                 From    => 'monitor@a.com',
  18.                 To      => 'monitor@b.com',
  19.                 Subject => "$re 100% packet loss",
  20. ],
  21. body => "$re the server is down!\n",
  22.  );
  23.                 my $sender = Email::Send->new(
  24.                 { mailer => 'Gmail',
  25.                 mailer_args => [
  26.                 username => 'monitor@a.com',
  27.                 password => 'xxx',
  28.                 ]
  29.                 }
  30. );
  31.         eval { $sender->send($email) };
  32.         die "Error sending email: $@" if $@;
  33.         }
  34. }

收藏_FTP参数解释

CWD – change working directory 更改目录
DELE – delete a remote file 删除文件
LIST – list remote files 列目录
MKD – make a remote directory 新建文件夹
NLST – name list of remote directory
PWD – print working directory 显示当前工作目录
RETR – retrieve a remote file 下载文件
RMD – remove a remote directory 删除目录
RNFR – rename from 重命名
RNTO – rename to 重命名
STOR – store a file on the remote host 上传文件
ABOR – abort a file transfer 取消文件传输
CWD – change working directory 更改目录
DELE – delete a remote file 删除文件
LIST – list remote files 列目录
MDTM – return the modification time of a file 返回文件的更新时间
MKD – make a remote directory 新建文件夹
NLST – name list of remote directory
PASS – send password
PASV – enter passive mode
PORT – open a data port 打开一个传输端口
PWD – print working directory 显示当前工作目录
QUIT – terminate the connection 退出
RETR – retrieve a remote file 下载文件
RMD – remove a remote directory
RNFR – rename from # RNTO – rename to
SITE – site-specific commands
SIZE – return the size of a file 返回文件大小
STOR – store a file on the remote host 上传文件
TYPE – set transfer type 设置传输类型
USER – send username 发送用户名

Directadmin到期 更换license的步骤

  1. cd /usr/local/directadmin/scripts
  2. ./getLicense.sh 你的client_ID 新的DA_LicenseIP
  3. service directadmin restart

for english people:
how to fix the expired issue for directadmin,
when you trying to log into your DA control panel and it showing:
License has expired, it means you need to start a new subscription.
log into your DA server by ssh,
cd in your DA path ( default is /usr/local/directadmin/scripts)
run the getLicense.sh with your client id and new license , new IP address.
Eg, ./getLicense.sh Your_Client_ID Your_new_DA_Licnese NewIP
./getLicense.sh 112233 123456 1.2.3.4
finally,just type command to restart the DA service,
service directadmin restart

perl模块_List::Util

1加2加3加4 … 加到100,试了一下还是perl最快
需要用到perl的List::Util模块.

  1. [root@centos perl]# more plus.pl
  2. #!/usr/bin/perl -w
  3. use strict;
  4. use List::Util qw[sum];
  5. my $t = sum(1..100);
  6. print "$t\n";
  7. [root@centos perl]# perl plus.pl
  8. 5050

shell取巧的方法也很简单,利用seq与sed和bc
seq生成数字,sed将空格替换为加号,通过管道传递给bc进行加法运算.

  1. [root@centos perl]# seq -s " " 100|sed 's/ /+/g'|bc
  2. 5050

centos配置vpn 基于pptpd

centos上配置vpn并指定出口IP

  1. 添加centos的源
  2. /etc/yum.repos.d/CentOS-Base.repo 
  3.  
  4. [doylenet]
  5. name=Doylenet custom repository for CentOS
  6. baseurl=http://files.doylenet.net/linux/yum/centos/5/i386/doylenet/
  7. gpgcheck=1
  8. gpgkey=http://files.doylenet.net/linux/yum/centos/RPM-GPG-KEY-rdoyle
  9. enabled=1
  10.  
  11. yum install ppp pptpd.i386 -y
  12.  
  13. echo 1 > /proc/sys/net/ipv4/ip_forward 
  14. sysctl -p
  15.  
  16. 修改 /etc/pptpd.conf
  17. 注释掉logwtmp
  18. 并添加IP范围
  19. localip 10.0.0.1
  20. remoteip 10.0.0.2-254
  21.  
  22. 修改 /etc/ppp/options.pptpd
  23. 添加DNS
  24. ms-dns 8.8.8.8
  25. ms-dns 8.8.4.4
  26.  
  27. 添加用户名密码
  28. /etc/ppp/chap-secrets 
  29.  
  30. iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -j SNAT --to-source YourIPs
  31. iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -j MASQUERADE #或者轮询