Search code examples
phpregexpreg-matchphpseclib

Handling server authentication Regex SSH Read


I am trying to handle the message to automatically connect when a new ip/server is being logged into, this is what I have so far:

    $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); 

But I get the following error message:

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

Can you help me fix this or rewrite it to get it to work? Any suggestions are helpful :)


Solution

  • The point is the the regex inside SSH read() is the same regex as should be used in the preg_ PHP functions, and thus requires regex delimiters. You may check the syntax at the phpseclib documentation Web page.

    Use, say,

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