从PHP调用shell脚本无法按预期工作

时间:2013-05-09 14:09:34

标签: php shell

我正在尝试从我的机​​器上打开远程机器浏览器。我设置密码少连接(考虑到两个系统在相同的n / w并且是可信的事实)。

我可以在脚本下面运行以启动浏览器,该浏览器需要3个参数

A)。用户名
B)。远程机器的IP地址
C)。 url在firefox中打开

如果我在我的bash shell中运行此脚本,我可以打开浏览器而不会出现任何问题。但是,如果我在PHP中调用它,我无法在远程计算机上启动浏览器...它显示所有调试打印但无法打开浏览器。任何指针都非常有用。

下面是我在远程机器上打开浏览器的shell脚本。

#!/bin/bash
if test $# != 3;
then
     echo "
         $0
         This command line script is used to launch, browser remotely. You need to pass 3 arguments to this script.
         YOU NEED TO PASS 3 ARGUMEMTS TO THIS SCRIPT,
         For example , \$> sh $0 \"<userNameofRemoteSystem>\"  \"<remoteSystemIP>\"  \"<webPageAddress>\"

         \$> sh $0 \"Uname\" \"172.17.64.94\"  \"mail.google.com\" \n";
         exit 1
fi

echo "<br><br>\nRECEIVED PARAMETERS,\n\tuserName\t-> |$1|\n\tipAddress\t-> |$2|\n\twebURL\t\t-> |$3|\n\n<br>";
echo "<br>Checking if Connection to |$2| WITH USERNAME |$1| is password less???<br><br>";
sleep 2

echo "<br>grep -wc $1 /home/user/.ssh/authorized_keys<br>";
count=`grep -wc $1 /home/user/.ssh/authorized_keys`;
echo "<br>got process count=|$count|<br>"

sleep 2

if [ "$count" != 1 ];
then
    echo "
       Looks like password less connection to |$2| is not configured with this server for username |$1|..
       Please congfigure the same and run this script again...\n";
fi

echo "SUCCESSFULLY CONNECTED, LAUNCHING BROWSER ON |$2| WITH |$3|\n";
ssh $1@$2 "nohup sh openBrowser.sh $3" &
PID=$$;
echo "PID IS |$PID|\n";
sleep 2;
echo "<br>PROCESS OUTPUT LOOKS LIKE THIS";
ps aux | grep $PID | grep -v grep
echo "<br>";
echo "after sleep";
kill -9 $PID && echo "after kill pid=$PID, passed ssh $1@$2 \"nohup sh openBrowser.sh $3\" &";

下面是我用来调用shell脚本的php脚本。

<?php
    $c_url=$_POST['url'];
    $c_name=$_POST['uname'];
    $c_ip=$_POST['ipaddress'];

    $output = system("/bin/sh /home/user/launchBrowser_remote.sh $uName $ipADDR $c_url");
    echo "$output";
?>

看起来我在shell脚本中得到错误的PID并且它杀死了调用者脚本而不是将ssh发送到远程..... BTW我已经尝试了execshell_exec但是没有运气

任何指针都非常有帮助

感谢

1 个答案:

答案 0 :(得分:0)

确保运行php文件的用户(如果在该上下文中调用Web服务器用户)也设置为无密码身份验证。