Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php网页中的expect脚本?_Php_Linux_Exec_Rsync_Expect - Fatal编程技术网

如何使用php网页中的expect脚本?

如何使用php网页中的expect脚本?,php,linux,exec,rsync,expect,Php,Linux,Exec,Rsync,Expect,当我试图在debian上执行php5中的程序时,网页冻结,程序什么也不做。 当我从命令行调用它时,这个脚本可以工作。安全模式已禁用。Echo标准输出不工作(因为冻结)。 我在谷歌上看到了一些关于www权限的答案,但是如果这里有人有一个快速简单的回答 如何调试这个 php调用 脚本代码(我在这里找到的) 试一试 2>&1将stderr重定向到stdout,并使用passthru而不是exec将为您提供所有输出。另一个常见的问题是,当您运行命令时,您与apache运行命令时是不同的用户。通常,出于安

当我试图在debian上执行php5中的程序时,网页冻结,程序什么也不做。 当我从命令行调用它时,这个脚本可以工作。安全模式已禁用。Echo标准输出不工作(因为冻结)。 我在谷歌上看到了一些关于www权限的答案,但是如果这里有人有一个快速简单的回答

如何调试这个

php调用 脚本代码(我在这里找到的) 试一试


2>&1
stderr
重定向到
stdout
,并使用
passthru
而不是
exec
将为您提供所有输出。

另一个常见的问题是,当您运行命令时,您与apache运行命令时是不同的用户。通常,出于安全原因,用户apache运行时的设置非常有限

apache用户可能没有安装正确的路径。使用预期的绝对路径,而不是相对路径。您可以通过运行“whichexpect”来找到这一点

尝试“su”到与apache运行相同的用户(在apache conf文件中查找“user”命令或简单地浏览“ps aux”)并运行该命令,看看会出现什么错误。

PHP用于执行命令的“user”或“group”可能没有足够的权限在脚本中执行一个cmd(甚至不需要它本身)。您是否尝试过使用sudo以您测试的同一用户的身份运行脚本?

为什么要使用expect?
将rsync设置为使用公钥/私钥(请参阅),您将不必使用expect来输入密码。

您正在运行mod\uuuuuuuuuphp还是suPHP?
mod_php以Apache用户的身份运行脚本,因此尝试su到Apache用户,然后从shell“php scriptname.php”运行php脚本,看看它是否工作。

如果您使用suPHP,那么su将发送给安装了apache的用户,以便对这些脚本和相同的“php scriptname.php”进行tun,并检查输出。

请注意expect over php,在输入下一个命令之前,您需要等待足够长的时间,并且所有变量都是正确的。格雷格关于使用2>&1的指示为我省去了很多麻烦

试着跑步

passthru("expect -d scripts/sshtest.exp $module");
-d将在预期中拯救你的生命

  • 如果使用exec而不是PASSTRU,请按以下方式执行: exec(“/bin/bash-c'command'>logfile_to_read_或_include_next”)

  • 如果您想用衍生进程扰乱系统,请执行以下操作:

  • 编写一个perl/c脚本,用于处理您的请求

    #!/usr/bin/perl -w
    use HTTP::Daemon;
    use HTTP::Status;
    use strict;
      my $d = HTTP::Daemon->new(LocalPort => 10050) || die;
      print "Please contact me at: <URL:", $d->url, ">\n";
      while (my $c = $d->accept) {
          while (my $r = $c->get_request) {
              if ($r->method eq 'GET' and $r->uri->path =~/addRequest-(.*)$/) {
                    # variable $1 now has your request.
                     my $rq = $1; # wash me!
                    # assign it a #ID, 
                     my $id = "id".time().rand(100);
                     &run_in_another_thread($rq,$id);
                  $c->send_responce($id);
              } elsif ($r->method eq 'GET' and $r->uri->path =~/seeRequest-(.*)$/) {
                 $c->send_responce( &get_result_for_id($1) );
              }
              else {
                  $c->send_error(RC_FORBIDDEN)
              }
          }
          $c->close;
          undef($c);
      }
    }
    
    sub run_in_another_thread {
     my ($rq,$id) = @_;
     my $evil = threads->create( sub { qx"/bin/bash -c '$rq' > logfile_$id.log" # start process
      }->detach();
    }
    
    sub get_result_for_id {
     my ($id) = @_;
     return qx"cat logfile_$id.log";
    }
    
    #/usr/bin/perl-w
    使用HTTP::Daemon;
    使用HTTP::Status;
    严格使用;
    我的$d=HTTP::Daemon->new(LocalPort=>10050)| | die;
    打印“请通过以下地址与我联系:\n”;
    而(我的$c=$d->接受){
    while(my$r=$c->get\u请求){
    如果($r->method eq'GET'和$r->uri->path=~/addRequest-(.*)$/){
    #变量$1现在有您的请求。
    我的$rq=$1;#给我洗个澡!
    #给它分配一个#ID,
    我的$id=“id”.time().rand(100);
    &在另一个线程中运行线程($rq,$id);
    $c->send_response($id);
    }elsif($r->method eq'GET'和$r->uri->path=~/seeRequest-(.*)$/){
    $c->send_responce(&get_result_for_id($1));
    }
    否则{
    $c->发送错误(禁止使用RC)
    }
    }
    $c->close;
    基金(c美元);
    }
    }
    在另一个线程中运行{
    我的($rq,$id)=@;
    my$evil=threads->create(sub{qx”/bin/bash-c'$rq'>logfile_$id.log“#启动进程
    }->分离();
    }
    子获取\u id的\u结果\u{
    我的($id)=@;
    返回qx“cat logfile_$id.log”;
    }
    

    接下来,从您的代码中获取127.0.0.1/addRequest-expect,瞧..

    我遇到了完全相同的问题,这让我发疯。我来晚了一点,但我想我应该发布解决问题的解决方案,以防其他人遇到此线程并遇到同样的问题

    我的症状与OP相同。我将运行PHP脚本,它将通过shell_execute启动Expect脚本,然后它将永远挂起。问题最终是由于Expect脚本运行时向apache用户询问了以下问题:

    主机“xx.xx.xx.xx”的真实性 (xx.xx.xx.xx)“”无法建立。 RSA密钥指纹是xxxxx。你呢 确实要继续连接吗 (是/否)

    当我从命令行运行脚本时,这个问题不会出现,因为我相信如果主机添加到主机列表中,这个问题只会被问一次

    为了解决这个问题,我在输入密码的位置之前添加了以下代码:

    expect "*you sure you want to continue*"
    send -- "yes\r"
    
    结果是(我让我的PHP脚本返回Expect脚本的所有输出):

    主机“xx.xx.xx.xx”的真实性 (xx.xx.xx.xx)“”无法建立。 RSA密钥指纹是xxxxx。你呢 确实要继续连接吗 (是/否)? 是无法将主机添加到已知主机列表(/var/www/.ssh/known_hosts)


    但是,紧接着出现密码提示,密码输入正确。从那时起,Expect脚本运行良好。

    freeze不让我看到输出。你说的“freeze”是什么意思你是说网页不会加载到你的浏览器中,也就是说,它会永远加载,不会返回网站?还是说,它意味着浏览器本身被冻结,它“没有响应?”或者完全是其他什么?这对我来说也很有效,但我仍然在寻找解决方法,因为我不希望网页中出现这种输出。
    passthru("expect -d scripts/sshtest.exp $module");
    
    #!/usr/bin/perl -w
    use HTTP::Daemon;
    use HTTP::Status;
    use strict;
      my $d = HTTP::Daemon->new(LocalPort => 10050) || die;
      print "Please contact me at: <URL:", $d->url, ">\n";
      while (my $c = $d->accept) {
          while (my $r = $c->get_request) {
              if ($r->method eq 'GET' and $r->uri->path =~/addRequest-(.*)$/) {
                    # variable $1 now has your request.
                     my $rq = $1; # wash me!
                    # assign it a #ID, 
                     my $id = "id".time().rand(100);
                     &run_in_another_thread($rq,$id);
                  $c->send_responce($id);
              } elsif ($r->method eq 'GET' and $r->uri->path =~/seeRequest-(.*)$/) {
                 $c->send_responce( &get_result_for_id($1) );
              }
              else {
                  $c->send_error(RC_FORBIDDEN)
              }
          }
          $c->close;
          undef($c);
      }
    }
    
    sub run_in_another_thread {
     my ($rq,$id) = @_;
     my $evil = threads->create( sub { qx"/bin/bash -c '$rq' > logfile_$id.log" # start process
      }->detach();
    }
    
    sub get_result_for_id {
     my ($id) = @_;
     return qx"cat logfile_$id.log";
    }
    
    expect "*you sure you want to continue*"
    send -- "yes\r"