【问题标题】:PHP - Execute bash .sh file to remove file from different serversPHP - 执行 bash .sh 文件以从不同的服务器中删除文件
【发布时间】:2013-02-06 21:35:46
【问题描述】:

我想运行一个包含 IP 数组的 PHP,并从每个 IP 中删除一个特定文件。

类似这样的:

foreach($servers as $ip){           
   shell_exec("sh /my/dir/delete.sh ".$ip." ".$file);
}

在 delete.sh 文件中我会有这样的东西

ssh user@$1 'rm /my/dir/filespath/$2 '

所有服务器都有相同的路径和文件,还有用户名和密码 请问有什么建议吗?

编辑:

执行 sh 文件的 PHP 文件位于安全管理员页面中,IP 为本地 IP(192.168.1.25、26、27)

如果我想从路径中删除所有文件,我会这样做(就像我现在正在做的那样)

ssh user@192.168.1.25 '/usr/bin/find /my/dir/filespath/* -type d -exec /bin/rm -fr {} \;'
ssh user@192.168.1.26 '/usr/bin/find /my/dir/filespath/* -type d -exec /bin/rm -fr {} \;'
ssh user@192.168.1.27 '/usr/bin/find /my/dir/filespath/* -type d -exec /bin/rm -fr {} \;'

但我只想删除一个特定文件,例如:/my/dir/filespath/other/folder/file.txt

由于我将添加更多服务器或更改它们的 IP,我需要它们是可变的 [现在这不是强制性的]

【问题讨论】:

  • 目前的解决方案到底有什么问题?
  • 也许应该是 /bin/sh /my/dir/delete.sh ... 而不仅仅是 sh
  • 您确定没有更好的方法吗?在安全方面,您的处境并不好。
  • 好吧.. 我需要在安全页面中从 PHP 生成 URL 并将其传递给 bash 文件.. 还有哪些建议?

标签: php linux bash shell


【解决方案1】:

在您的远程服务器上,您可以托管一个文件,我们称之为 callme.php

callme.php 会是这样的

<?php
exec("/bin/sh /path/to/deletefiles.sh");
echo 'OK';
?>

deletefiles.sh 类似于

#!/bin/sh 
rm -rf /path/to/file/to/delete.txt
echo 'Ok'

最后在你的命令服务器上你可以有一个像这样的 bash 文件:

#!/bin/sh
servers+=("http://1.2.3.4")
servers+=("http://1.2.3.5")
servers+=("http://1.2.3.6")
servers+=("http://www.yoursite.com")
file='/callme.php'

for i in "${servers[@]}"
do
:
  echo $i$file
  curl -s $i$file
  sleep 5
done

或者如果您更愿意在 php 中调用远程文件

  <?php

 $servers[]="http://1.2.3.4";
 $servers[]="http://1.2.3.5";
 $servers[]="http://1.2.3.6";
 $servers[]="http://www.yoursite.com";

 $file = "/callme.php";

 foreach ($servers as $k => $v){
         $url = $v.$file;
         $results[] = curl_download($url);
 }
 var_dump($results);

 function curl_download($Url) {
        if (!function_exists('curl_init')) {
            die('Sorry cURL is not installed!');
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $Url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }
   ?>

这可能不是最好的方法,但它确实有效...上面的代码我只是快速写出来的,所以有些代码可能需要优化,你需要确保你的所有文件都有适当的权限.

【讨论】:

  • 我必须将路径/文件传递给 bash 文件,它是在 php 文件中生成的。这就是我遇到麻烦的地方,感谢您的建议
  • 如果将路径作为参数传递给php脚本?
  • 试过了,和例子一样
【解决方案2】:

** 已解决 **

我是管理员提出的这个要求

if($file){
    $res = file_get_contents("http://[current server IP]/delete.php?token=12345&p=".$file);
    echo $file;
}
echo $res;

delete.php 文件有这个

if($_GET['token']!='12345') exit();

$ips = array(192.168.1.25,192.168.1.26,192.168.1.27);

$file = $_GET['p'];
$file = str_replace(array('../','*','./'),'',$file);
if($file != ""){
    $command = '"/bin/rm -f /my/dir/filespath/'.$file.'"';
    foreach($ips as $ip){
        echo shell_exec('ssh user@'.$ip.' '.$command);
        sleep(1);// sleep 1 sec for letting the command time to delete the file (could be less)
    }
}
exit();

完美运行! 当然,delete.php 文件的安全性更高,这只是一个示例版本

谢谢大家!

【讨论】:

    猜你喜欢
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多