perl脚本之------向/etc/aliases中添加/删除邮件组
来源:互联网 发布:免费php分类信息系统 编辑:程序博客网 时间:2024/06/05 23:07
两个月没来上空间了,好多东西都没写上,现在补上吧,留作备忘,也让自己有一些成就感,最近在用mimedefang做一个邮件过滤系统,然后又做了过滤系统的数据库和数据库的前端web管理界面,先上几个用perl写的小功能脚本吧,其它的慢慢再写上
1,向/etc/aliases中添加邮件组,并添加此邮件组说明。格式如下:
###comment
group_name:
CODE:
2,删除/etc/aliases中的邮件组和组的说明
CODE:
1,向/etc/aliases中添加邮件组,并添加此邮件组说明。格式如下:
###comment
group_name:
脚本实现的功能:先备份/etc/aliases,把原来的文件备份成“文件名.日期"的格式,如果今天运行这个脚本,刚备份文件名为aliases.20080903,然后把改动写入文件,最后运行newaliases命令全改动生效
CODE:
#!/usr/bin/perl -w
#name:aliases-add-group.pl
#
use strict;
my $comment=shift;
my $group_name=shift;
my $dir="/etc";
my $file="aliases";
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$mon=$mon+1;
$mon='0'.$mon if($mon<10);
$year=$year+1900;
$mday='0'.$mday if($mday<10);
my $ext=$year.$mon.$mday;
if(-f "$dir//$file/.$ext"){
system("rm -f $dir//$file/.$ext");
system("mv $dir//$file $dir//$file/.$ext");
}else{
system("mv $dir//$file $dir//$file/.$ext");
}
my $bak_file="$file/.$ext";
open(FILE,"$dir//$bak_file") or die "connot open $!";
$comment="/#/#/#$group_name: ".$comment."/n";
$group_name=$group_name.": "."/n";
my @aliases=<FILE>;
push (@aliases,$comment);
push (@aliases,$group_name);
foreach my $str(@aliases){
open FH,">>$dir//$file" or die $!."/n";
print FH $str;
close(FH);
}
close(FILE);
system("/usr/bin/newaliases");
#name:aliases-add-group.pl
#
use strict;
my $comment=shift;
my $group_name=shift;
my $dir="/etc";
my $file="aliases";
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$mon=$mon+1;
$mon='0'.$mon if($mon<10);
$year=$year+1900;
$mday='0'.$mday if($mday<10);
my $ext=$year.$mon.$mday;
if(-f "$dir//$file/.$ext"){
system("rm -f $dir//$file/.$ext");
system("mv $dir//$file $dir//$file/.$ext");
}else{
system("mv $dir//$file $dir//$file/.$ext");
}
my $bak_file="$file/.$ext";
open(FILE,"$dir//$bak_file") or die "connot open $!";
$comment="/#/#/#$group_name: ".$comment."/n";
$group_name=$group_name.": "."/n";
my @aliases=<FILE>;
push (@aliases,$comment);
push (@aliases,$group_name);
foreach my $str(@aliases){
open FH,">>$dir//$file" or die $!."/n";
print FH $str;
close(FH);
}
close(FILE);
system("/usr/bin/newaliases");
2,删除/etc/aliases中的邮件组和组的说明
CODE:
#!/usr/bin/perl -w
#name:aliases-del-group.pl
use strict;
my $group_name=shift;
my $dir="/etc";
my $file="aliases";
exit if($group_name eq "all");
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$mon=$mon+1;
$mon='0'.$mon if($mon<10);
$year=$year+1900;
$mday='0'.$mday if($mday<10);
my $ext=$year.$mon.$mday;
if(-f "$dir//$file/.$ext"){
system("rm -f $dir//$file/.$ext");
system("mv $dir//$file $dir//$file/.$ext");
}else{
system("mv $dir//$file $dir//$file/.$ext");
}
my $bak_file="$file/.$ext";
open(FILE,"$dir//$bak_file") or die "$!/n";
my @aliases=<FILE>;
foreach my $line(@aliases){
if(($line=~/^/#/#/#$group_name:/) || ($line=~/^$group_name:/)){
$line=~ s/^.*$//g;
$line=~ s/^/s$//g;
}
}
foreach my $str(@aliases){
open FH,">>$dir//$file" or die $!."/n";
print FH $str;
close(FH);
}
close(FILE);
#name:aliases-del-group.pl
use strict;
my $group_name=shift;
my $dir="/etc";
my $file="aliases";
exit if($group_name eq "all");
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$mon=$mon+1;
$mon='0'.$mon if($mon<10);
$year=$year+1900;
$mday='0'.$mday if($mday<10);
my $ext=$year.$mon.$mday;
if(-f "$dir//$file/.$ext"){
system("rm -f $dir//$file/.$ext");
system("mv $dir//$file $dir//$file/.$ext");
}else{
system("mv $dir//$file $dir//$file/.$ext");
}
my $bak_file="$file/.$ext";
open(FILE,"$dir//$bak_file") or die "$!/n";
my @aliases=<FILE>;
foreach my $line(@aliases){
if(($line=~/^/#/#/#$group_name:/) || ($line=~/^$group_name:/)){
$line=~ s/^.*$//g;
$line=~ s/^/s$//g;
}
}
foreach my $str(@aliases){
open FH,">>$dir//$file" or die $!."/n";
print FH $str;
close(FH);
}
close(FILE);
- perl脚本之------向/etc/aliases中添加/删除邮件组
- perl脚本之------向/etc/aliases邮件组添加删除用户
- perl发邮件脚本
- perl发送邮件脚本
- FreeBSD邮件服务Postfix问题:“fatal: open database /etc/aliases.db”
- Shell向Perl脚本中传递变量的方法
- Shell向Perl脚本中传递变量的方法
- Ubuntu中向菜单栏添加程序(脚本)
- Ubuntu中向菜单栏添加程序(脚本)
- 向div中动态添加删除节点
- 菜鸟的Shell脚本之路---向linux系统中添加dos2unix和rz,sz命令
- Perl 脚本中单元测试
- ASP.NET 向网页中动态添加客户端脚本(C#)
- 发邮件的PERL/Tk脚本
- 向网页中动态添加或删除控件
- 向SDE中添加图层(删除图层)
- 向表中添加一个字段或删除一个字段
- JavaScript向select下拉框中添加和删除元素
- 良好的程序设计习惯
- ubuntu linux shell下删除目录命令
- 工作流系统全面介绍
- 收集面试的笔试,机试题目
- 32位系统上使用64位变量需要注意
- perl脚本之------向/etc/aliases中添加/删除邮件组
- perl脚本之------向/etc/aliases邮件组添加删除用户
- 引用变量注意事项——第八章(P226)
- 推荐一篇好文,林锐写的《软件工程思想》
- 升级gentoo失败,genkernel all通不过,revdep-rebuild反复编辑gcc
- 操作TreeView(咏南工作室)
- 防止程序重复执行的单元
- JavaScript的一些实用技巧收藏
- 使用ASP.NET上传图片汇总(二)