function testTelnetClientBSD() { $TelnetClient = new TelnetClient("65.38.4.218"); // Try to connect $conn = $TelnetClient->Connect(); $this->assertTrue($conn, "PHP Telnet client successfully connected to host"); // Try to login with valid password $login = $TelnetClient->Login("telnettest", "password", "login:"******"Password:"******"[telnettest@bsd2 ~]$"); $this->assertTrue($login, "PHP Telnet client successfully log on"); if ($login) { // Send command and wait for response $TelnetClient->Send("whereis php"); $data = $TelnetClient->WaitForString("/usr/local/bin/php"); $this->assertTrue($data, "PHP Telnet client successfully received waitfor string"); // Get all response $TelnetClient->Send("whereis php"); $data = $TelnetClient->ReadAll(); $this->assertTrue($data, "PHP Telnet client successfully received some data ({$data})"); $TelnetClient->Send("ls -al"); $data = $TelnetClient->WaitForString("dfasdfasdfas"); $this->assertFalse($data, "PHP Telnet client successfully not received waitfor string thast not found"); } // Disconnect $this->assertTrue($TelnetClient->Disconnect(), "PHP Telnet client successfully disconnected from server"); }
/** * @dataProvider connectionParametersProvider */ public function testConnect($host, $port = NULL, $user = NULL, $pass = NULL) { $client = new TelnetClient($host, $port); $client->connect(); if ($user && $pass) { $client->login($user, $pass); } $client->disconnect(); }
/** * Execute script * * @param string $script * @param array $params * @return bool */ public function Execute($script, $params) { $lastresponse = ""; $state = "NORMAL"; $this->ScriptExecutionLog = ""; $script_lines = $this->ParseScript($script); foreach ($script_lines as $linenum=>$line) { if (preg_match("/^\[EXPECT '(.*)'\]$/si", $line, $matches)) { $waitforstring = preg_quote($this->PrepareScriptLine($matches[1], $params), '/'); $res = preg_match("/{$waitforstring}/si", $lastresponse); if ($res) $state = "NORMAL"; else $state = "SKIP"; } elseif (preg_match("/^\[ELSE\]$/si", $line)) { if ($state == "NORMAL") $state = "SKIP"; else $state = "NORMAL"; } elseif (preg_match("/^\[ENDEXPECT\]$/si", $line)) { $state = "NORMAL"; } else { if ($state == "NORMAL") { if (preg_match("/^\[TERMINATE[\s]*([0-9]+)?\]$/si", $line, $matches)) { $this->ScriptExecutionLog .= "[TERMINATED]\n"; return ($matches[1] > 0) ? false : true; } else { $line = $this->PrepareScriptLine($line, $params); $this->ScriptExecutionLog .= "SEND: {$line}\n"; $lastresponse = $this->SSH->Exec($line, $this->Options["consolePromt"]); $this->ScriptExecutionLog .= "RECV: ".$lastresponse."\n"; } } } } return true; }
/** * Execute script * * @param string $script * @param array $params * @return bool */ public function Execute($script, $params) { $script = $this->PrepareScriptLine($script, $params); $lines = explode(";", $script); $this->DB->BeginTrans(); try { foreach ($lines as $line) { if (strlen($line) > 0) $this->DB->Execute(trim($line)); } $this->DB->CommitTrans(); } catch(Exception $e) { $this->DB->RollbackTrans(); return false; } return true; }
/** * Wait for string * * @param string $string * @return bool */ private function WaitForString($string, $buf = false) { return $this->TelnetClient->WaitForString($string, false, $buf); }
/** * Execute script * * @param string $script * @param array $params * @return bool */ public function Execute($script, $params) { $lastresponse = ""; $state = "NORMAL"; $script_lines = $this->ParseScript($script); foreach ($script_lines as $linenum=>$line) { if (preg_match("/^\[EXPECT '(.*)'\]$/si", $line, $matches)) { $waitforstring = preg_quote($this->PrepareScriptLine($matches[1], $params), '/'); $res = preg_match("/{$waitforstring}/si", $lastresponse); if ($res) $state = "NORMAL"; else $state = "SKIP"; } elseif (preg_match("/^\[ELSE\]$/si", $line)) { if ($state == "NORMAL") $state = "SKIP"; else $state = "NORMAL"; } elseif (preg_match("/^\[ENDEXPECT\]$/si", $line)) { $state = "NORMAL"; } else { if ($state == "NORMAL") { if (preg_match("/^\[TERMINATE[\s]*([0-9]+)?\]$/si", $line, $matches)) { $this->ScriptExecutionLog .= "[TERMINATED]\n"; return ($matches[1] > 0) ? false : true; } else { $line = $this->PrepareScriptLine($line, $params); $this->ScriptExecutionLog .= "SEND: {$line}\n"; list($method, $url, $post_fields) = explode(" ", $line); $post = ($method == "POST") ? true : false; $parsed_url = parse_url($url); if ($post) parse_str($post_fields, $params); else $params = array(); $lastresponse = $this->HTTPClient->Fetch($url, $params, $post); $this->ScriptExecutionLog .= "RECV: {$lastresponse}\n"; } } } } return true; }