CMDHelper.pm
薛泰
2023-12-01
use strict;
use warnings;
push @INC,substr($0,0,rindex($0,"\\"));
require "Log.pm";
package CMDHelper;
sub ExecuteCmdOrder
{
my $cmdOrder = shift;
Log::DEBUG("CMDORDER: $cmdOrder\n");
my @rr = `$cmdOrder`;
foreach(@rr)
{
Log::DEBUG("$_\n");
}
return \@rr;
}
#创建共享目录
sub CreateShareDirectory
{
my($shareName,$shareDir) = @_;
my $cmdOrder = "net share \"$shareName\"=\"$shareDir\" 2>&1";
my $rr = &ExecuteCmdOrder($cmdOrder);
foreach(@$rr)
{
if(/共享成功/)
{
Log::INFO("$_\n");
return 1;
}
if(/已经共享/)
{
Log::INFO("$_\n");
&DeleteShareName($shareName) and &CreateShareDirectory(@_);
return 1;
}
if(/拒绝访问/ or /发生系统错误 2/)
{
Log::ERROR("$_\n");
return 0;
}
}
return 0;
}
#删除共享
sub DeleteShareName
{
my $shareName = shift;
my $cmdOrder = "net share \"$shareName\" /delete 2>&1";
my $rr = &ExecuteCmdOrder($cmdOrder);
foreach(@$rr)
{
if(/已经删除/)
{
Log::INFO("$_\n");
return 1;
}
}
return 0;
}
#创建目录
sub CreateDirectory
{
my ($dir) = shift;
my $cmdOrder = "md \"$dir\" 2>&1";
my $rr = &ExecuteCmdOrder($cmdOrder);
foreach(@$rr)
{
if(length($_))
{
Log::ERROR("$_\n");
exit(0);
}
}
}
#获取本机的所有用户列表
sub GetLocalHostUserList
{
my @userList = ();
my $cmdOrder = "net user 2>&1";
my $rr = &ExecuteCmdOrder($cmdOrder);
foreach(@$rr)
{
my @tmpList = split/\s+/;
push @userList,@tmpList;
}
return \@userList;
}
#创建一个用户
sub CreateUser
{
my($user,$pwd) = @_;
my $cmdOrder = "net user \"$user\" \"$pwd\" /ADD 2>&1";
my $rr = &ExecuteCmdOrder($cmdOrder);
foreach(@$rr)
{
if(/拒绝访问/)
{
Log::ERROR("$_\n");
return 0;
}
}
return 1;
}
#获取所有的用户组
sub GetLocalGroupList
{
my @groupList = ();
my $cmdOrder = "net localgroup 2>&1";
my $rr = &ExecuteCmdOrder($cmdOrder);
foreach(@$rr)
{
chomp;
push @groupList,$_;
}
return \@groupList;
}
#添加用户到组中
sub AddUserToGroup
{
my ($userName,$groupName) = @_;
my $cmdOrder = "net localgroup \"$groupName\" \"$userName\" /ADD 2>&1";
my $rr = &ExecuteCmdOrder($cmdOrder);
foreach(@$rr)
{
if(/此命令的语法是/)
{
Log::ERROR("组$_不存在\n");
return 0;
}
if(/指定的本地组不存在/ or /拒绝访问/)
{
Log::ERROR("$_\n");
return 0;
}
if(/发生系统错误 1378/)
{
#用户已经是组用户
return 1;
}
if(/没有此全局用户或组/)
{
Log::ERROR("$_\n");
return 0;
}
}
return 1;
}
#修改用户密码
sub MotifyUserPasword
{
my($user,$pwd) = @_;
my $cmdOrder = "net user \"$user\" \"$pwd\" 2>&1";
my $rr = &ExecuteCmdOrder($cmdOrder);
foreach(@$rr)
{
if(/拒绝访问/ or /找不到用户名/)
{
Log::ERROR("$_\n");
return 0;
}
}
return 1;
}
#修改用户对文件夹的权限
sub MotifyDirAuthorization
{
my($dir,$user,$grant) = @_;
my $cmdOrder = "icacls \"$dir\" /grant $user:$grant 2>&1";
my $rr = &ExecuteCmdOrder($cmdOrder);
foreach(@$rr)
{
if(/帐户名与安全标识间无任何映射完成/)
{
Log::ERROR("$_\n");
return 0;
}
}
}
#停止服务
sub StopService
{
my $service=shift;
my $cmdOrder = "net stop \"$service\" 2>&1";
my $rr = &ExecuteCmdOrder($cmdOrder);
foreach(@$rr)
{
if(/服务名无效/ or /拒绝访问/)
{
Log::ERROR("$_\n");
return 0;
}
return 1 if (/服务已成功停止/ or /没有启动/);
}
return 1;
}
#启动服务
sub StartService
{
my $service=shift;
my $cmdOrder = "net start \"$service\" 2>&1";
my $rr = &ExecuteCmdOrder($cmdOrder);
foreach(@$rr)
{
if(/服务名无效/ or /拒绝访问/ or /无法启动服务/)
{
Log::ERROR("$_\n");
return 0;
}
return 1 if (/服务已经启动成功/ or /已经启动/);
}
return 1;
}
#设置服务的启动用户
sub SetServiceStartUser
{
my($service,$user,$pwd) = @_;
my $cmdOrder = "sc config \"$service\" obj= \".\\$user\" password= \"$pwd\" 2>&1";
my $rr = &ExecuteCmdOrder($cmdOrder);
foreach(@$rr)
{
if(/ChangeServiceConfig 失败/)
{
foreach(@$rr)
{
if(length($_)>0)
{
unless(/ChangeServiceConfig 失败/)
{
Log::ERROR("$_\n");
return 0;
}
}
}
}
if(/帐户名无效/ or /指定的服务未安装/ or /拒绝访问/)
{
Log::ERROR("$_\n");
return 0;
}
}
return 1;
}
#查找元素是否在列表中
sub IsExistItem
{
my ($rlist,$item,$ignoreCase) = @_;
foreach(@$rlist)
{
if($ignoreCase)
{
return 1 if /$item/i ;
}
else
{
return 1 if /$item/ ;
}
}
return 0;
}
1;