Noodle2732 Posted August 20, 2009 Share Posted August 20, 2009 Hi there, I am trying to use the SSH2 extension of PHP to run a command on a Draytek router. If i use putty or just ssh from the cli i can login browse the menu, run commands and exit. I am trying just to pull the status of the router off of it. the command is 'system status' and as i say running this in a normal ssh client works fine. In PHP however..... The first issue is that it takes forever to actually make the connection and run the command, i will be sitting there looking at the loading icon for 5 - 10 mins before i get any output. The second issue is that it sends the command too early i think. I'm not sure. Here is my code: if (!function_exists("ssh2_connect")) die("ERROR: The ssh2_connect function is not available.\n Is the SSH2 extension installed correctly?"); // log onto the SSH server if(!($con = ssh2_connect('10.0.1.254', 8022))){ die ("ERROR: Unable to connect to SSH server!"); } else { // try to authenticate the open SSH connection if(!ssh2_auth_password($con, "draytek", "Password")) { die ("ERROR: Unable to authenticate!"); } else { echo "Logged into SSH server!\n"; // create a shell if(!($shell = ssh2_shell($con, 'xterm'))){ die("ERROR: Failed requesting an interactive shell"); } else{ //Make sure that the stream receives data back before requesting it. stream_set_blocking( $shell, true ); // send a command fwrite($shell,'system status'); // Collect & display returning data echo "<pre>"; while( $buf = fgets($shell) ){ echo $buf; } echo "</pre>"; fclose($shell); } } } The output is: Logged into SSH server! system statusLogin by remote client DrayTek> in a normal ssh client this is the output: login as: draytek [email protected]'s password: Login by remote client DrayTek> system status ------------------- System Status ------------------- Model: Vigor3300V Firmware Version: 2.5.7.5 (EN) Hardware Version: 1.0 Build Date&Time: 2007-10-11 18:24:43 System Uptime: 16 days 4 hours 3 minutes 50 seconds CPU Usage: 1.1418% Memory Size: 64 MBytes Memory Usage: 47.7353% IP Address: 10.0.1.254 MAC Address: 00:50:7F:36:33:D3 DrayTek> btw if i connect to the webserver as an ssh server i.e. conenct to 127.0.0.1 i can run commands and get results back with no issues. Thanks in advance, Nathan Link to comment https://forums.phpfreaks.com/topic/171160-ssh2-run-commands-on-a-router/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.