处理服务器身份验证Regex SSH Read

时间:2016-09-20 18:17:00

标签: php regex preg-match phpseclib

我正在尝试处理消息,以便在登录新的IP /服务器时自动连接,这是我到目前为止所做的:

    $read = $this->ssh->read('(Password:)|(Are you sure you want to continue connecting)', NET_SSH2_READ_REGEX);
            if (preg_match('/Are you sure you want to continue connecting/',$read)) {
                $this->ssh->write('yes');
                $this->ssh->write("\n");
                $this->ssh->read('Permanently added');

                $this->ssh->write('ssh '.$this->userid.'@'.$this->testIPAddress);
                $this->ssh->write("\n");                
                $read = $this->ssh->read('(Password:)|(Are you sure you want to continue connecting)', NET_SSH2_READ_REGEX);
            } else if ($this->ssh->isTimeout()) { 
                $testLineHideResult = "Connection failed";
            }

            if ($testLineHideResult == '') {
                $this->ssh->write($this->passwd);
                $this->ssh->write("\n");
                $this->ssh->setTimeout(25); 

但是我收到以下错误消息:

preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Unknown modifier '|'

你可以帮我解决这个问题或重写它以使其发挥作用吗?任何建议都有帮助:)

1 个答案:

答案 0 :(得分:1)

关键是SSH read()中的正则表达式与preg_ PHP函数中应该使用的正则表达式相同,因此需要正则表达式分隔符。您可以在phpseclib documentation网页上查看语法。

使用,比方说,

$this->ssh->read('/Password:|Are you sure you want to continue connecting/', NET_SSH2_READ_REGEX);
                  ^                                                      ^