Im programming a ssh script to connect to a server.
ok grand fair enough,
but how do i pass commands to an already active connection, without opening and closing it every time?
if been browsing google for a while now, and maybe due to my vb.net programming experince i dont understand the concept of how this is done in php or other scripting languages..
just to clarify,
open connection,
script termminates ???
send comands,,,
script sends back result
script terminates
send comands,,,
script sends back result
script terminates
send quit
connection closed
all without having to keep reopening the connection (once the script is ran, the connection is closed), like keep-alive or something??
im not looking for code, just someone to explain in english on how to achieve this or something to help understand the concept
<?php
// Connection to SSH server
echo "Connexion SSH ";
if (!($resource=@ssh2_connect("localhost"))) {
echo "[FAILED]<br />";
exit(1);
}
echo "[OK]<br />";
// Authentification by login/passwd
echo "Authentification ";
if (!@ssh2_auth_password($resource,"Administrator","siemens")) {
echo "[FAILED]<br />";
exit(1);
}
echo "[OK]<br />";
// We need a shell
echo "Shell stdio ";
if (!($stdio = @ssh2_shell($resource,"xterm"))) {
echo "[FAILED]<br />";
exit(1);
}
echo "[OK]<br />";
// Execution of any command
// Be careful to add an '\n' at the end of the command
$command = "dir /p /w" .chr(13);
fwrite($stdio,$command);
// IMPORTANT
// For some obscur reasons, just after ur command, u need to make a sleep to be sure, that the command has reached the server and is running
sleep(4);
// Then u can fetch the stream to see what happens on stdio
while($line = fgets($stdio)) {
flush();
echo trim($line)."<br />";
}
// It's always cleaner to close all stream
fclose($stdio);
?>