|
@@ -15,6 +15,11 @@ class WscriptShell
|
|
logError('failed to creeate WScript.Shell',$e->getCode(),$e->getMessage());
|
|
logError('failed to creeate WScript.Shell',$e->getCode(),$e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ public static function pathJoin($base, $path) {
|
|
|
|
+ $base = str_replace('\\','/',$base);
|
|
|
|
+ $path = str_replace('\\','/',$path);
|
|
|
|
+ return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
|
|
|
|
+ }
|
|
/* style
|
|
/* style
|
|
0 SW_HIDE
|
|
0 SW_HIDE
|
|
1 SW_SHOWNORMAL
|
|
1 SW_SHOWNORMAL
|
|
@@ -35,12 +40,42 @@ class WscriptShell
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
try{
|
|
try{
|
|
- return $shell->Run($cmd,$style,$wait);
|
|
|
|
|
|
+ $code = $shell->Run($cmd,$style,$wait);
|
|
|
|
+ if($code != 0){
|
|
|
|
+ logError('failed to Run',$cmd, 'code=',$code);
|
|
|
|
+ }
|
|
|
|
+ return $code;
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
logError('failed to Run',$cmd, $e->getCode(),$e->getMessage());
|
|
logError('failed to Run',$cmd, $e->getCode(),$e->getMessage());
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ public static function RunOutput($cmd,$out_file) //运行并获取输出文本数组
|
|
|
|
+ {
|
|
|
|
+ $shell = self::$shell;
|
|
|
|
+ if(! $shell){
|
|
|
|
+ logError('failed to Run. shell is null.');
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ if( file_exists($out_file) ){
|
|
|
|
+ unlink($out_file);
|
|
|
|
+ }
|
|
|
|
+ $cmd = "cmd.exe /C {$cmd} > {$out_file}";
|
|
|
|
+ $ret = self::Run($cmd,0,1);
|
|
|
|
+ if( $ret === false){
|
|
|
|
+ logError('failed to RunOutput',$cmd);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ if( $ret !== 0){
|
|
|
|
+ logError('failed to RunOutput',$cmd, 'code',$ret);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ if( !file_exists($out_file) ){
|
|
|
|
+ logError('failed to find out file',$out_file);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ return $ret;
|
|
|
|
+ }
|
|
public static function RegRead($key)
|
|
public static function RegRead($key)
|
|
{
|
|
{
|
|
$shell = self::$shell;
|
|
$shell = self::$shell;
|
|
@@ -136,27 +171,19 @@ class WinProcess extends WscriptShell
|
|
WscriptShell::__construct();
|
|
WscriptShell::__construct();
|
|
self::$tmp_dir = $tmp_dir;
|
|
self::$tmp_dir = $tmp_dir;
|
|
}
|
|
}
|
|
- private static function pathJoin($base, $path) {
|
|
|
|
- $base = str_replace('\\','/',$base);
|
|
|
|
- $path = str_replace('\\','/',$path);
|
|
|
|
- return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
|
|
|
|
- }
|
|
|
|
public static function find($name){
|
|
public static function find($name){
|
|
$out_file = pathJoin(self::$tmp_dir,$name . '.txt');
|
|
$out_file = pathJoin(self::$tmp_dir,$name . '.txt');
|
|
- if( file_exists($out_file) ){
|
|
|
|
- unlink($out_file);
|
|
|
|
- }
|
|
|
|
- $cmd = "cmd.exe /C tasklist /FI \"IMAGENAME eq {$name}\" /FO list > {$out_file}";
|
|
|
|
- if( self::Run($cmd,0,1) != 0){
|
|
|
|
- logError('failed to Run',$cmd);
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- if( !file_exists($out_file) ){
|
|
|
|
- logError('failed to find out file',$out_file);
|
|
|
|
|
|
+ $cmd = "tasklist /FI \"IMAGENAME eq {$name}\" /FO list";
|
|
|
|
+ if( self::RunOutput($cmd,$out_file) !== 0){
|
|
|
|
+ logError('failed to RunOutput',$cmd);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
$pids = array();
|
|
$pids = array();
|
|
$fp = fopen($out_file,"r");
|
|
$fp = fopen($out_file,"r");
|
|
|
|
+ if(!$fp){
|
|
|
|
+ logError('failed to open',$out_file);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
while($line = fgets($fp) ){
|
|
while($line = fgets($fp) ){
|
|
if( substr($line,0,4) == 'PID:'){
|
|
if( substr($line,0,4) == 'PID:'){
|
|
$pid = trim(substr($line,4));
|
|
$pid = trim(substr($line,4));
|
|
@@ -171,7 +198,7 @@ class WinProcess extends WscriptShell
|
|
{
|
|
{
|
|
//关闭进程
|
|
//关闭进程
|
|
$cmd = "cmd.exe /C taskkill /F /IM {name}";
|
|
$cmd = "cmd.exe /C taskkill /F /IM {name}";
|
|
- if( self::Run($cmd,0,1) != 0){
|
|
|
|
|
|
+ if( self::Run($cmd,0,1) !== 0){
|
|
logError('failed to Run',$cmd);
|
|
logError('failed to Run',$cmd);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -270,7 +297,7 @@ class WinAutorun extends WscriptShell
|
|
}
|
|
}
|
|
public static function remove($name)
|
|
public static function remove($name)
|
|
{
|
|
{
|
|
- if( strlen($name) == 0){
|
|
|
|
|
|
+ if( strlen($name) == 0){
|
|
logError('failed to removeAutorun. name is empty.');
|
|
logError('failed to removeAutorun. name is empty.');
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -332,10 +359,9 @@ class WinFile extends WscriptShell
|
|
public static function setHidden($path)
|
|
public static function setHidden($path)
|
|
{
|
|
{
|
|
$cmd = "cmd.exe /C attrib \"{$path}\" +s +h";
|
|
$cmd = "cmd.exe /C attrib \"{$path}\" +s +h";
|
|
- return self::Run($cmd,0,1);
|
|
|
|
|
|
+ return self::Run($cmd,0,1) === 0;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
-
|
|
|
|
function setHiddenAttrib($path){
|
|
function setHiddenAttrib($path){
|
|
$os = getOsName();
|
|
$os = getOsName();
|
|
if( $os == 'win'){
|
|
if( $os == 'win'){
|
|
@@ -345,4 +371,137 @@ function setHiddenAttrib($path){
|
|
else{
|
|
else{
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+}
|
|
|
|
+//windows服务操作
|
|
|
|
+class WinService extends WscriptShell
|
|
|
|
+{
|
|
|
|
+ private static $tmp_dir;
|
|
|
|
+ public function __construct($tmp_dir)
|
|
|
|
+ {
|
|
|
|
+ WscriptShell::__construct();
|
|
|
|
+ self::$tmp_dir = $tmp_dir;
|
|
|
|
+ }
|
|
|
|
+ public static function register($name,$path)
|
|
|
|
+ {
|
|
|
|
+ if(stripos($name,'mysql') !== false){
|
|
|
|
+ return self::mysqldInstall($name,$path);
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
|
|
+ logError('failed to register service. not support name.',$name,$path);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ public static function remove($name)
|
|
|
|
+ {
|
|
|
|
+ //先停止服务
|
|
|
|
+ if(! self::scstop($name) ){
|
|
|
|
+ logError('failed to stop service.',$name);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ //再移除服务
|
|
|
|
+ if(! self::scdelete($name)){
|
|
|
|
+ logError('failed to delete service.',$name);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // false: failed
|
|
|
|
+ // 0: exists
|
|
|
|
+ // 1060: not exists
|
|
|
|
+ // other int: other error
|
|
|
|
+ public static function scexists($name)
|
|
|
|
+ {
|
|
|
|
+ $cmd = "sc query {$name}";
|
|
|
|
+ return self::Run($cmd,0,1);
|
|
|
|
+ }
|
|
|
|
+ // false: failed
|
|
|
|
+ // 1060: not exists
|
|
|
|
+ // other int: other error
|
|
|
|
+ // array: service info
|
|
|
|
+ // *** not implemented ***
|
|
|
|
+ public static function scquery($name)
|
|
|
|
+ {
|
|
|
|
+ $cmd = "sc query {$name}";
|
|
|
|
+ return self::Run($cmd,0,1) === 0;
|
|
|
|
+ }
|
|
|
|
+ public static function scstart($name)
|
|
|
|
+ {
|
|
|
|
+ $cmd = "sc start {$name}";
|
|
|
|
+ return self::Run($cmd,0,1) === 0;
|
|
|
|
+ }
|
|
|
|
+ public static function scdelete($name)
|
|
|
|
+ {
|
|
|
|
+ $cmd = "sc delete {$name}";
|
|
|
|
+ $code = self::Run($cmd,0,1);
|
|
|
|
+ if($code === false){
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ else if($code === 0){
|
|
|
|
+ logInfo('service deleted successfully.',$name);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ else if($code === 1060){
|
|
|
|
+ logInfo('service not exists. no need to delete.',$name);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
|
|
+ logError('unknow error during delete service.',$name,$code);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // false: execute failed
|
|
|
|
+ // true: stop succeeded ( service not exists, service already stopped )
|
|
|
|
+ public static function scstop($name)
|
|
|
|
+ {
|
|
|
|
+ $cmd = "sc stop {$name}";
|
|
|
|
+ $code = self::Run($cmd,0,1);
|
|
|
|
+ if($code === false){
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ else if($code === 0){
|
|
|
|
+ logInfo('service stopped successfully.',$name);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ else if($code === 1060){
|
|
|
|
+ logInfo('service not exists. no need to stop.',$name);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ else if($code === 1062){
|
|
|
|
+ logInfo('service not running. no need to stop.',$name);
|
|
|
|
+ return true;
|
|
|
|
+ }else{
|
|
|
|
+ logError('unknow error during stopping service.',$name,$code);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ private static function mysqldInstall($name,$path)
|
|
|
|
+ {
|
|
|
|
+ $cmd = "{$path} --install {$name}";
|
|
|
|
+ $code = self::Run($cmd,0,1);
|
|
|
|
+ if($code === 0){
|
|
|
|
+ logError('failed to install mysql service.',$name,$path);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ private static function mysqldRemove($path)
|
|
|
|
+ {
|
|
|
|
+ $cmd = "{$path} --remove";
|
|
|
|
+ $code = self::Run($cmd,0,1);
|
|
|
|
+ if($code === 0){
|
|
|
|
+ logError('failed to remove mysql service.',$name,$path);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+$a = new WscriptShell();
|
|
|
|
+$cmd = 'cmd.exe /C sc query phpStudySrv > 1.txt';
|
|
|
|
+$cmd = 'sc stop phpStudySrv';
|
|
|
|
+print($a::Run($cmd,0,1));
|