PHP使用phpseclib - exec(ls ...)命令找不到刚刚创建并存在于磁盘

时间:2017-08-09 20:06:52

标签: php linux exec phpseclib

使用我的PHP应用程序中的phpseclib,我正在运行一个perl .pl脚本,该脚本在其中一个exec()命令中的远程服务器驱动器/文件夹上创建一个文件。这工作正常,文件存在。下一个exec()做了一个' ls'用于检索完整文件名的命令,该文件名包含未知文件名末尾的日期/时间戳。当它运行时,它在那里找不到文件,我可以在做一本手册时看到它' ls'在Linux系统上。我认为这可能是一个时间问题,所以我还在创作和做“&”之间进行了一次睡眠。命令,但这并没有解决问题。如果我再次运行程序,创建第二个文件,那么' ls'返回我生成的第一个文件的文件名。我没有在文档中看到fclose,但确实在其他帖子中看到它可能会让我相信这也可能是问题。有没有其他人遇到过这个问题,你是如何解决的?感谢。

更新:已添加代码段....

            $ssh2->login(LINUX_USER,LINUX_PASS);
            $connected = $ssh2->isAuthenticated();
            if ($connected) {
                //$_SESSION['feedback'] .= "Connection and authentication successful to " . LINUX_SVR . ".<br>";
                $cmd_string = "/usr/bin/perl-report.pl " . LINUX_ENV . " " . $parm1 . " " . $parm2;
                $error = $ssh2->exec($cmd_string);
                if ($error) {
                    $_SESSION['feedback'] .= "Unable to execute command to create the Report - contact Operations with this error.<br>";
                    $_SESSION['feedback'] .= $ssh2->getLog();
                } else {
                    $_SESSION['feedback'] .= "Report successfully created for ID " . $parm2 . ".<br>";
                    //get the full name of the file created
                    $cmd_string = "sleep 15";
                    $ssh2->exec($cmd_string);
                    $cmd_string = "ls /root/Report-" . $parm1 . "-" . $parm2 . "-" . $date . "*";
                    $error = $ssh2->exec($cmd_string);
                    if (empty($error)) {
                        $_SESSION['feedback'] .= "Unable to execute command to obtain report name - contact Operations with this error.<br>";
                        $_SESSION['feedback'] .= $ssh2->getLog();
                    } else {
                        $filename = $ssh2->exec($cmd_string);
                        $filename = substr($filename, 6, strlen($filename));
                        $_SESSION['feedback'].= "The file name created is " . $filename . "<br>";
                        //verify that the filename is valid
                        if (substr($filename,0,7)=="Report-") {
                            //mail the attachment to the emailaddr
                            $cmd_string = "echo 'Requested Report attached.' | mail -s '" . $filename . "' -a '/root/" . $filename . "' " . $emailaddr . "\n";
                            $error = $ssh2->exec($cmd_string);
                            if ($error) {
                                $_SESSION['feedback'] .= "Unable to successfully email the requested report - contact Operations with this error.<br>";
                                $_SESSION['feedback'] .= $ssh2->getLog();
                            } else {
                                $_SESSION['feedback'] .= "Report successfully emailed to " . $emailaddr . ".  Process complete.<br>";
                                $_SESSION['feedback'] .= $ssh2->getLog();
                            }
                        } else {
                            $_SESSION['feedback'] .= "Unable to email attachment as filename is invalid - contact Operations with this error.<br>";
                            $_SESSION['feedback'] .= $ssh2->getLog();
                        }
                    }
                }
            } else {
                $_SESSION['feedback'] .= "Connection and authentication failed to " . LINUX_SVR . ".<br>";
            }
            //disconnect when done
            $ssh2->reset();
            $ssh2->disconnect();
        }

0 个答案:

没有答案