如何从 php 脚本执行 shell
How to execute shell from php script
我想从 php 脚本执行此命令
scrapy crawl example -a siteid=100
我试过这个:
<?php
$id = 100;
exec('scrapy crawl example -a siteid= $id' $output, $ret_code);
?>
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
简而言之,有一个本机 PHP 命令可以执行您想要的操作。你也可以用 google for exec()
这是一个类似的函数。
根据您是否需要脚本的输出,有不同的方法。
exec
执行命令并return 输出给调用者。
passthru
当 Unix 命令的输出是二进制数据需要直接传回浏览器时,应该使用函数代替 exec。
system
执行一个外部程序并显示输出,但只显示最后一行。
popen
— 创建一个单向的新进程 read/writable
proc_open
— 创建一个支持双向的新进程 read/writable
对于你的 scrapy 脚本,我会使用 popen
和 pclose
的组合,因为我认为你不需要脚本输出。
pclose(popen("scrapy crawl example -a siteid=$id > /dev/null &", 'r'));
phpseclib - 从 http://phpseclib.sourceforge.net/ 下载并将其包含在您的项目中。
include('Net/SSH2.php');
$ssh = new Net_SSH2("Your IP Here");
if (!$ssh->login('Your User', 'Your Password')) {
exit('Login Failed');
}
$id = 100;
echo "<pre>";
print_r($ssh->exec('scrapy crawl example -a siteid= $id'));
希望这对您有所帮助。
试试这个:
<?php
$id = 100;
exec('scrapy crawl example -a siteid=$id 2>&1', $output);
return $output;
?>
您实际上需要重定向输出才能获得它。
如果你不需要输出,只是执行命令,你只需要第一部分,像这样:
exec('scrapy crawl example -a siteid=' . $id);
因为你没有把参数放在 ' ' 里面,你把它放在外面,阅读 PHP 中的文本连接。
我想从 php 脚本执行此命令
scrapy crawl example -a siteid=100
我试过这个:
<?php
$id = 100;
exec('scrapy crawl example -a siteid= $id' $output, $ret_code);
?>
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
简而言之,有一个本机 PHP 命令可以执行您想要的操作。你也可以用 google for exec()
这是一个类似的函数。
根据您是否需要脚本的输出,有不同的方法。
exec
执行命令并return 输出给调用者。passthru
当 Unix 命令的输出是二进制数据需要直接传回浏览器时,应该使用函数代替 exec。system
执行一个外部程序并显示输出,但只显示最后一行。popen
— 创建一个单向的新进程 read/writableproc_open
— 创建一个支持双向的新进程 read/writable
对于你的 scrapy 脚本,我会使用 popen
和 pclose
的组合,因为我认为你不需要脚本输出。
pclose(popen("scrapy crawl example -a siteid=$id > /dev/null &", 'r'));
phpseclib - 从 http://phpseclib.sourceforge.net/ 下载并将其包含在您的项目中。
include('Net/SSH2.php');
$ssh = new Net_SSH2("Your IP Here");
if (!$ssh->login('Your User', 'Your Password')) {
exit('Login Failed');
}
$id = 100;
echo "<pre>";
print_r($ssh->exec('scrapy crawl example -a siteid= $id'));
希望这对您有所帮助。
试试这个:
<?php
$id = 100;
exec('scrapy crawl example -a siteid=$id 2>&1', $output);
return $output;
?>
您实际上需要重定向输出才能获得它。
如果你不需要输出,只是执行命令,你只需要第一部分,像这样:
exec('scrapy crawl example -a siteid=' . $id);
因为你没有把参数放在 ' ' 里面,你把它放在外面,阅读 PHP 中的文本连接。