您的位置:首页 > 编程语言 > PHP开发

一般PHP命令行的编写

2011-06-19 13:41 281 查看
#!/usr/local/bin/php

<?php

error_reporting (0);

if (count($argv) <= 1 || $argv[1] == "-h") {

die ("No file or directory to sync./nUsage: synchronize file1[directory1] file2[directory2] .../n/n");

}

//$hosts = unserialize (file_get_contents("/bin/hosts"));

$hosts = file("/bin/hosts.line");

//$path = get_pwd();

$path = getcwd();

for ($i = 1; $i < count($argv); $i ++) {

if (strstr($argv[$i], "/") != $argv[$i]) $src = $path."/".$argv[$i];

else $src = $argv[$i];

if ($_SERVER["LOGNAME"] != 'root' || substr ($src, 0, 30) !== "/usr/local/apache2/htdocs/")

{

echo $src." not allowed./n";

continue;

}

$tar = dirname($src)."/";

if (is_dir($src)) {

foreach ($hosts as $h){

$h = trim ($h);

$cmd = "sudo /usr/bin/scp -r ".$src." test@".$h.":".$tar;

ex ($cmd);

}

} elseif (is_file($src)) {

foreach ($hosts as $h){

$h = trim ($h);

$cmd = "sudo /usr/bin/scp ".$src." test@".$h.":".$tar;

ex ($cmd);

}

} else {

echo "ignore ".$src." (does not exists?)/n";

}

}

function get_pwd () {

return $_SERVER['PWD'];

}

function ex ($cmd) {

echo substr($cmd, 14)."->executing, please wait...";

system ($cmd);

echo "ok!/n";

}

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: