From: Radek Krejca

> I am trying send command to remote host over ssh with sockets. But
> I need to set up username/password. I am trying to modify this script
> (from - function fsockopen), but I dont know, where set
> username/password because I got this message:
> Bad protocol version identification 'password' from ip
> Library ssh2 is not currentu userfull for me, because I am not
> admin of server.

> <?php
> /************************************************************
> * Author: Richard Lajaunie
> * Mail :
> *
> * subject : this script retreive all mac-addresses on all ports
> * of a Cisco 3548 Switch by a telnet connection
> *
> * base on the script by: xbensemhoun at t-systems dot fr on the same
> **************************************************************/
> if ( array_key_exists(1, $argv) ){
>    $cfgServer = $argv[1];
> }else{
>    echo "ex: 'php test.php' \n";
>    exit;
> }
> $cfgPort    = 23;                //port, 22 if SSH
> $cfgTimeOut = 10;
> $usenet = fsockopen($cfgServer, $cfgPort, $errno, $errstr),
> if(!$usenet){
>        echo "Connexion failed\n";
>        exit();
> }else{
>        echo "Connected\n";
>        fputs ($usenet, "password\r\n");
>        fputs ($usenet, "en\r\n");
>        fputs ($usenet, "password\r\n");
>        fputs ($usenet, "sh mac-address-table\r\n");
>        fputs ($usenet, " "); // this space bar is this for long output

Well, in the first place, you don't simply send a series of words to the
other end after opening the connection. Most protocols define a
conversation that happens between the two ends of a new connection. So
you have to wait for the server to send you a welcome message with a
prompt and reply to that. Then wait for the next prompt and reply to it.
So instead of just blasting out these strings, you need a receive loop
and parser to interpret what the server is saying to you. Once you know
what it says at each step you can decide how to respond.

SSH adds another layer in front of this to select key exchanges,
ciphers, hashes, etc. You don't want to write an SSH client. It can take
days just to read and understand the protocol definition.

A few minutes on Google should produce some useable examples of clients
for various protocols. It shouldn't take much work to read a basic
Telnet client written in Perl and transpose it into PHP.

Bob McConnell

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to