Chinaunix首页 | 论坛 | 博客
  • 博客访问: 69380
  • 博文数量: 23
  • 博客积分: 906
  • 博客等级: 准尉
  • 技术积分: 250
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-24 23:57
文章分类

全部博文(23)

文章存档

2011年(3)

2010年(20)

我的朋友

分类:

2010-05-03 14:17:04

目标:利用php编写一个shell界面,实现所有shell所支持的命令行,运行VI等程序支持;
所需要考虑:安全性,php访问超时;
所能使用的函数参考popen,shell_exec,exec,system,passthru;
1.popen
resource  popen( string command, string mode )
打开一个指向进程的管道,该进程由派生给定的 command命令执行而产生。
返回一个和 fopen()所返回的相同的文件指针,只不过它是单向的(只能用于读或写)并且必须
用 pclose()来关闭。此指针可以用于 fgets(),fgetss()和 fwrite()。
如果出错返回 FALSE
2.shell_exec
string shell_exec( string cmd )
执行cmd命令,命令的结果存在返回结果中;本函数在安全模式下被禁用。
3.exec
string exec( string command [, array &output [, int &return_var]] )
参数:
command将要执行的命令
output如果指定了output参数,则对应的array数组将会填满该命令输出的每一行。
return_var如果指定了return_var参数,注意,return_var参数只有output参数被设定后才能指定,
那么该命令的执行状态将会以结果参数的形式返回给该变量。
返回值:
返回改名了的最后一行结果。
范例
例子 1. An exec() example
[root@south4 demo]# vi test.php
#!/usr/bin/php  -q
exec('ls /etc',$results,$ret);
echo  $results[5] . "\n";
echo  $results[12]. "\n";
echo  $results[3]. "\n";
echo  $ret;
?>
[root@south4 demo]# ./test.php
aliases
audit
adjtime
0
4.system
string system( string command [, int &return_var] )
system是开一个bash进程的,执行完毕之后,就会释放,如下就会达不到目的:
system("cd /home/y/share/NBHadoop/scripts");
system("sudo -u yahoo ./nb_halfhourly.sh $yes_halfHour");
可以写成:
system("cd /home/y/share/NBHadoop/scripts;sudo -u yahoo ./nb_halfhourly.sh $yes_halfHour");
5.fpassthru
passthru ( string $command [, int &$return_var] )
与exec和system类似,返回的值是raw data,如二进制数据,比如图片。
escapeshellarg ( string $arg )
可以用到php的安全中,会过滤掉arg中存在的一些特殊字符。在输入的参数中如果包含中文传递给
escapeshellarg,会被过滤掉。
escapeshellcmd ( string $command )
escapeshellcmd()函数会转义命令中的所有shell元字符来完成工作。这些元字符包括:# & ; ` , | * ? ~ < >
^ ( ) [ ] { } $ \\。
 
阅读(960) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~