如何使用 php 网页中的 Expect 脚本?
当我尝试在我的 debian 上执行 php5 的程序时,网页冻结并且程序不执行任何操作。 当我从命令行调用该脚本时,该脚本可以工作。 安全模式已禁用。 Echo 标准输出不起作用(因为冻结)。 我在谷歌中读到了一些答案,其中讲述了 www 权限,但如果这里有人有一个快速而简单的响应......
如何调试这个?
php 调用
exec("expect scripts/sshtest.exp $module");
脚本代码(我在这里找到 http://bash. cyberciti.biz/security/expect-ssh-login-script/)
#!/usr/bin/expect -f
# set Variables
set module [lrange $argv 0 0]
set timeout -1
# rsync
spawn rsync -aCb --progress --delete --backup-dir=/var/www/blabla.com/rsyncBackups/BackupedFilesFromServer23_on_ /var/www/blabla/$module -e ssh [email protected]:/root/$module
match_max 1000000
# Look for passwod prompt
expect "*?assword:*"
# Send password
send -- "THEPASSWORD\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof
When i try to execute a program from php5 on my debian, the webpage freeze and the program do nothing.
This script works when i call it from the command line. Safe mode is disabled. Echo stdout doesnt work (because of the freeze).
I read some answers in google which tells of www permissions but if someone here have a quick and simple response...
How to debug this ?
The php call
exec("expect scripts/sshtest.exp $module");
The script code (which i found here http://bash.cyberciti.biz/security/expect-ssh-login-script/)
#!/usr/bin/expect -f
# set Variables
set module [lrange $argv 0 0]
set timeout -1
# rsync
spawn rsync -aCb --progress --delete --backup-dir=/var/www/blabla.com/rsyncBackups/BackupedFilesFromServer23_on_ /var/www/blabla/$module -e ssh [email protected]:/root/$module
match_max 1000000
# Look for passwod prompt
expect "*?assword:*"
# Send password
send -- "THEPASSWORD\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试
2>&1
将stderr
重定向到stdout
并使用passthru
而不是exec 将为您提供所有输出。
Try
The
2>&1
redirectsstderr
tostdout
and usingpassthru
instead ofexec
will give you all the output.为什么要使用expect?
设置 rsync 以使用公钥/私钥(请参阅 http://troy.jdmz.net/rsync /index.html),您将不必使用expect来输入密码。
Why use expect at all?
Set up rsync to use public/private keys (see http://troy.jdmz.net/rsync/index.html) and you will not have to use expect to enter passwords.
我也遇到了同样的问题,这让我抓狂。 我参加聚会有点晚了,但我想我应该发布解决我的问题的解决方案,以防其他人遇到此线程并遇到同样的问题。
我的症状和OP一样。 我会运行 PHP 脚本,它会通过 shell_execute 启动 Expect 脚本,然后它就会永远挂起。 事实证明,该问题是由于运行 Expect 脚本时向 apache 用户询问以下问题造成的:
当我从命令行运行脚本时,这个问题不会出现在我身上,因为我相信如果主机添加到主机列表中,这个问题只会被问一次。
为了解决这个问题,我在输入密码的位置之前添加了以下代码:
结果是这样的(我让我的 PHP 脚本返回 Expect 脚本的所有输出):
然而,紧接着就出现了密码提示,并且密码输入正确。 从那时起,Expect 脚本运行良好。
I had this exact same problem and it was making me nuts. I'm a little late to the party, but I figured I'd post the solution that solved my issue in case someone else comes across this thread and has the same problem.
My symptoms were the same as the OP. I would run the PHP script, it would kick off the Expect script via shell_execute, and then it would just hang forever. The problem turned out to be due to the following question that was being asked to the apache user when the Expect script ran:
This question wouldn't come up for me when I ran the script from the command line since I believe the question is only asked once if the host added to the host list.
To fix the problem, I added this code in before the spot where I entered in the password:
That resulted in this (I had my PHP script return all of the output from the Expect script):
However, directly after that the password prompt came up and the password was entered in correctly. From that point forward the Expect script ran fine.
另一个常见的问题是,当您运行该命令时,您与 apache 运行该命令时的用户是不同的用户。 通常,出于安全原因,用户 apache 的运行设置非常有限。
例如。 apache 用户可能没有安装正确的路径。 使用期望的绝对路径,而不是相对路径。 您可以通过运行“which Expect”找到它。
尝试“su”到 apache 运行的同一用户(在 apache conf 文件中查找“User”命令或简单地浏览“ps aux”)并运行该命令,看看会出现什么错误。
Another common catch is that when you run the command you are a different user from when apache runs the command. And often, the user apache runs as has very limited set up for security reasons.
eg. The apache user might not have the right paths installed. Use the absolute path to expect, not the relative. You can find this by running 'which expect'.
Try to 'su' to the same user apache runs as (look for 'User' command in apache conf file or simply browse 'ps aux') and run the command and see what errors you get.
PHP 用于执行命令的“用户”或“组”可能没有足够的权限来执行脚本中的一个 cmd(或者甚至没有足够的权限执行其本身)。 您是否尝试过使用 sudo 以与您测试的同一用户身份运行脚本?
The "user" or the "group" PHP is using to exec commands may have insufficient rights to execute one cmd in the script (or even expect itself). Have you tried using sudo to run the script as the same user you have been testing with?
您正在运行 mod___php 或 suPHP 吗?
mod_php 以 Apache 用户身份运行脚本,因此请尝试 su 到 apache 用户,然后从 shell“php scriptname.php”运行 php 脚本,看看它是否正常工作。
如果您使用 suPHP,则 su 到您已设置 apache 的用户以调整这些脚本以及相同的“php scriptname.php”并检查输出。
Are you running mod___php or suPHP?
mod_php runs scripts as the Apache user so try to su to the apache user and then run the php script from shell "php scriptname.php" and see if it's working.
If you use suPHP then su to the user under which you've setup apache to tun those scripts and the so the same "php scriptname.php" and check for the output.
请小心,在 php 中,您需要等待足够长的时间才能输入下一个命令,并且所有变量都是正确的。 格雷格关于使用 2>&1 的指导为我省去了很多麻烦。
尝试跑步
-d 会救你一命。
Be careful with expect over php that you wait long enough before putting in your next command, and also that all your variables are correct. Greg's pointer on using 2>&1 saved me a lot of hassle.
try running
-d will save your life in expect.
如果您使用 exec 而不是 passthru,请按以下方式操作:
exec("/bin/bash -c 'command' > logfile_to_read_or_include_next");
如果你想用生成的进程扰乱你的系统:
编写一个 perl/c 脚本,它将处理你的请求。
接下来,从代码中获取 127.0.0.1/addRequest-expect,瞧……
If you're using exec instead of passthru, do it in this way:
exec("/bin/bash -c 'command' > logfile_to_read_or_include_next");
If you want to mess your system with spawned processes:
write a perl/c script, that would process your requests.
Next, get 127.0.0.1/addRequest-expect from your code, and voila..