host = $host; if($port!=NULL) $this->port = $port; $this->connection = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); if (!$this->connection) { $this->msg="fail: unable to establish telnet connection to ".$this->host.":".$this->port." - $errstr ($errno)"; return false; } return true; } //--------------------------------------------------------------------------------------------- function exec($cmd, $timeout=NULL, $stopstring=NULL) { if($timeout==NULL) $timeout = $this->timeout; $this->write($cmd . $this->eol); $data = $this->read($timeout, $stopstring); return $data; } //--------------------------------------------------------------------------------------------- function write($cmd) { // execute a command if(!( @fputs($this->connection, $cmd )) ){ $this->msg="fail: unable to execute telnet command"; return false; } return true; } //--------------------------------------------------------------------------------------------- function exec2($cmd, $timeout=NULL, $stopstring=NULL) { if($timeout==NULL) $timeout = $this->timeout; // execute a command if(!( @fputs($this->connection, $cmd )) ){ $this->msg="fail: unable to execute telnet command"; return false; } /* if($this->stream_blocking == false) { // collect returning data from command stream_set_blocking( $this->_con, false ); $time_start = time(); while( true ){ $data .= fread($this->_con, 4096); // if( preg_match('(__COMMAND_FINISHED__\s*$)', $data) ) { // $this->msg="okay: command finished"; // break; // } if( (time()-$time_start) > $this->timeout ){ $this->msg="fail: timeout of ".$this->timeout." seconds has been reached"; return false; } } } else { // stream_blocking==true stream_set_blocking( $this->_con, true ); */ stream_set_timeout( $this->connection, $timeout); // Read from stdout stream while($buf = @fgets($this->connection, 4096)) { $meta = stream_get_meta_data($this->connection); if ($meta["timed_out"]) break; $data .= $buf; if (!empty($stopstring) && stripos($buf, $stopstring) !== false) { // echo "stopstring found!!!"; break; } } /* }*/ // $data = preg_replace('(__COMMAND_FINISHED__\s*$)', '', $data); $this->result = $data; return $data; } //--------------------------------------------------------------------------------------------- function read($timeout=NULL, $stopstring=NULL) { if($timeout==NULL) $timeout = $this->timeout; stream_set_timeout( $this->connection, $timeout); // Read from stdout stream while($buf = @fgets($this->connection, 4096)) { $meta = stream_get_meta_data($this->connection); if ($meta["timed_out"]) break; $data .= $buf; if (!empty($stopstring)) { // ak je zadany nejaky stopstring if(is_array($stopstring)) { // ak stopstring je pole foreach($stopstring as $ss) { // pre kazdy prvok pola stopstring if(stripos($buf, $ss) !== false) { // overit ci sa nachadza vo vystupe (buf) // echo "stopstring found!!!"; break 2; } } } elseif(stripos($buf, $stopstring) !== false) { // sk stopstring nie je pole a zaroven sa nachadza vo vystupe // echo "stopstring found!!!"; break; } } } $this->result = $data; return $data; } //--------------------------------------------------------------------------------------------- function close() { if($this->connection == NULL) return; @fclose($this->connection); $this->connection = NULL; } //--------------------------------------------------------------------------------------------- } ?>