Howto send command over ssh using sockets

Howto send command over ssh using sockets

am 05.04.2010 00:26:23 von radek.krejca

Hello,

I am trying send command to remote host over ssh with sockets. But I need t=
o set up username/password. I am trying to modify this script (from www.php=
..net - function fsockopen), but I dont know, where set username/password be=
cause 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 ser=
ver.

Thank you
Radek


/*********************************************************** *
* Author: Richard Lajaunie
* Mail : richard.lajaunie@cote-azur.cci.fr
*
* 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 page
************************************************************ **/

if ( array_key_exists(1, $argv) ){
$cfgServer =3D $argv[1];
}else{
echo "ex: 'php test.php 10.0.0.0' \n";
exit;
}

$cfgPort =3D 23; //port, 22 if SSH
$cfgTimeOut =3D 10;

$usenet =3D fsockopen($cfgServer, $cfgPort, $errno, $errstr), $cfgTimeOut);

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
=20
// this skip non essential text
$j =3D 0;
while ($j<16){
fgets($usenet, 128);
$j++;
}
stream_set_timeout($usenet, 2); // set the timeout for the fgets
$j =3D 0;
while (!feof($usenet)){
$ret =3D fgets($usenet, 128);
$ret =3D str_replace("\r", '', $ret);
$ret =3D str_replace("\n", "", $ret);
if (ereg("FastEthernet", $ret)){
echo "$ret \n";
}
if (ereg('--More--', $ret) ){
fputs ($usenet, " "); // for following page
}
$info =3D stream_get_meta_data($usenet);
if ($info['timed_out']) {
$j++;
}
if ($j >2){
fputs ($usenet, "lo");
break;
}
}
}
echo "End.\r\n";
?>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Howto send command over ssh using sockets

am 05.04.2010 03:09:54 von ahlin.hans

Instead of ssh, you could use telnet to connect to the Cisco router
(which incidentally runs on port 23, but is likely to be disabled on
the cisco router, unless you have a pre-SSH capable IOS running on it
(like my old cisco crap :( ) ), because i strongly doubt you have
written or are willing to write your own encryption libraries for this
project, you might also want to read IETF RFC 854
[http://tools.ietf.org/html/rfc854] about the telnet protocol, as you
are writing your own client, and not using a pre-made one, judging
from your script.
Or if you do not like the idea of sending clear-text passwords to the
router, you might want to learn about proc_open() (or popen()) and use
the native ssh utility that most likely is present on the server,
taking great care to READ THE MANUAL for the ssh command, because you
most likely do _not_ want it to spit out ANSI-escapes to you script.

Kind regards from
Johan Lidström
Örnsköldsvik, Sweden
irc://irc.freenode.net/Dr_Kao
frozendude+phpdev@gmail.com

P.S. currently borrowing a friends account.

2010/4/5 Radek Krejča :
> Hello,
>
> 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 www.p=
hp.net - 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 s=
erver.
>
> Thank you
> Radek
>
>
> > /*********************************************************** *
> * Author: Richard Lajaunie
> * Mail : richard.lajaunie@cote-azur.cci.fr
> *
> * 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 page
> ************************************************************ **/
>
> if ( array_key_exists(1, $argv) ){
>   $cfgServer =3D $argv[1];
> }else{
>   echo "ex: 'php test.php 10.0.0.0' \n";
>   exit;
> }
>
> $cfgPort    =3D 23;             =
   //port, 22 if SSH
> $cfgTimeOut =3D 10;
>
> $usenet =3D fsockopen($cfgServer, $cfgPort, $errno, $errstr), $cfgTimeOut=
);
>
> 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
>
>       // this skip non essential text
>       $j =3D 0;
>       while ($j<16){
>       fgets($usenet, 128);
>       $j++;
>       }
>   stream_set_timeout($usenet, 2); // set the timeout for the fgets
>   $j =3D 0;
>       while (!feof($usenet)){
>       $ret =3D fgets($usenet, 128);
>       $ret =3D str_replace("\r", '', $ret);
>       $ret =3D str_replace("\n", "", $ret);
>       if  (ereg("FastEthernet", $ret)){
>           echo "$ret \n";
>       }
>       if (ereg('--More--', $ret) ){
>           fputs ($usenet, " "); // for following=
page
>       }
>       $info =3D stream_get_meta_data($usenet);
>       if ($info['timed_out']) {
>           $j++;
>       }
>       if ($j >2){
>           fputs ($usenet, "lo");
>           break;
>       }
>   }
> }
> echo "End.\r\n";
> ?>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Howto send command over ssh using sockets

am 05.04.2010 09:56:46 von radek.krejca

SGVsbG8sDQoNCnRoYW5rIHlvdSBmb3IgcmVzcG9uc2UsIG1vcmUgaW4geW91 IHRleHQ6DQoNCklu
c3RlYWQgb2Ygc3NoLCB5b3UgY291bGQgdXNlIHRlbG5ldCB0byBjb25uZWN0 IHRvIHRoZSBDaXNj
byByb3V0ZXINCih3aGljaCBpbmNpZGVudGFsbHkgcnVucyBvbiBwb3J0IDIz LCBidXQgaXMgbGlr
ZWx5IHRvIGJlIGRpc2FibGVkIG9uDQoNCkkgd2FudCB0byB1c2UgbXkgc2Ny aXB0IGFnYWluc3Qg
RnJlZUJTRCByb3V0ZXIgYW5kIGFnYWluc3QgUm91dGVyT1Mgc28gSSBuZWVk IHNzaC4gSSBjYW4g
dXNlIHN5c3RlbSBmdW5jdGlvbiB0byBjYWxsIHNzaCBjb21tYW5kLCBidXQg SSBjYW4gdHJ5IGl0
IG92ZXIgcGhwIHRvb2xzLg0KDQpPciBpZiB5b3UgZG8gbm90IGxpa2UgdGhl IGlkZWEgb2Ygc2Vu
ZGluZyBjbGVhci10ZXh0IHBhc3N3b3JkcyB0byB0aGUNCnJvdXRlciwgeW91 IG1pZ2h0IHdhbnQg
dG8gbGVhcm4gYWJvdXQgcHJvY19vcGVuKCkgKG9yIHBvcGVuKCkpIGFuZCB1 c2UNCnRoZSBuYXRp
dmUgc3NoIHV0aWxpdHkgdGhhdCBtb3N0IGxpa2VseSBpcyBwcmVzZW50IG9u IHRoZSBzZXJ2ZXIs
DQp0YWtpbmcgZ3JlYXQgY2FyZSB0byBSRUFEIFRIRSBNQU5VQUwgZm9yIHRo ZSBzc2ggY29tbWFu
ZCwgYmVjYXVzZSB5b3UNCm1vc3QgbGlrZWx5IGRvIF9ub3RfIHdhbnQgaXQg dG8gc3BpdCBvdXQg
QU5TSS1lc2NhcGVzIHRvIHlvdSBzY3JpcHQuDQoNCkl0IHdpbGwgYmUgcHJv YmFibHkgYmV0dGVy
IHdheSB0aGFuIHN5c3RlbSBmdW5jdGlvbi4gSWYgSSBmYWlsIHdpdGggdXNp bmcgc29ja2V0cyBJ
IHVzZSB0aGlzLiBUaGFuayB5b3UgdmVyeSBtdWNoLg0KDQpSYWRlaw0KDQo=

RE: Howto send command over ssh using sockets

am 05.04.2010 14:56:37 von Bob McConnell

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 www.php.net - function fsockopen), but I dont know, where set
> username/password because I got this message:
> Bad protocol version identification 'password' from ip
>=20
> Library ssh2 is not currentu userfull for me, because I am not
> admin of server.

> > /*********************************************************** *
> * Author: Richard Lajaunie
> * Mail : richard.lajaunie@cote-azur.cci.fr
> *
> * 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
page
> ************************************************************ **/
>=20
> if ( array_key_exists(1, $argv) ){
> $cfgServer =3D $argv[1];
> }else{
> echo "ex: 'php test.php 10.0.0.0' \n";
> exit;
> }
>=20
> $cfgPort =3D 23; //port, 22 if SSH
> $cfgTimeOut =3D 10;
>=20
> $usenet =3D fsockopen($cfgServer, $cfgPort, $errno, $errstr),
$cfgTimeOut);
>=20
> 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
> =20

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 (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Howto send command over ssh using sockets

am 07.04.2010 17:16:03 von zelnaga

phpseclib does SSH without PECL extension and only with fsockopen:

http://phpseclib.sourceforge.net/

On Sun Apr 4 21:09:54 2010, Hans_Åhlin wrote:
> Instead of ssh, you could use telnet to connect to the Cisco router
> (which incidentally runs on port 23, but is likely to be disabled on
> the cisco router, unless you have a pre-SSH capable IOS running on it
> (like my old cisco crap :( ) ), because i strongly doubt you have
> written or are willing to write your own encryption libraries for this
> project, you might also want to read IETF RFC 854
> [http://tools.ietf.org/html/rfc854] about the telnet protocol, as you
> are writing your own client, and not using a pre-made one, judging
> from your script.
> Or if you do not like the idea of sending clear-text passwords to the
> router, you might want to learn about proc_open() (or popen()) and use
> the native ssh utility that most likely is present on the server,
> taking great care to READ THE MANUAL for the ssh command, because you
> most likely do _not_ want it to spit out ANSI-escapes to you script.
>
> Kind regards from
> Johan Lidström
> Örnsköldsvik, Sweden
> irc://irc.freenode.net/Dr_Kao
> frozendude+phpdev@gmail.com
>
> P.S. currently borrowing a friends account.
>
> 2010/4/5 Radek Krejča :
>> Hello,
>>
>> I am trying send command to remote host over ssh with sockets. But I nee=
d to set up username/password. I am trying to modify this script (from www.=
php.net - 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.
>>
>> Thank you
>> Radek
>>
>>
>> >> /*********************************************************** *
>> * Author: Richard Lajaunie
>> * Mail : richard.lajaunie@cote-azur.cci.fr
>> *
>> * 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 pag=
e
>> ************************************************************ **/
>>
>> if ( array_key_exists(1, $argv) ){
>>   $cfgServer =3D $argv[1];
>> }else{
>>   echo "ex: 'php test.php 10.0.0.0' \n";
>>   exit;
>> }
>>
>> $cfgPort    =3D 23;             =
   //port, 22 if SSH
>> $cfgTimeOut =3D 10;
>>
>> $usenet =3D fsockopen($cfgServer, $cfgPort, $errno, $errstr), $cfgTimeOu=
t);
>>
>> 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
>>
>>       // this skip non essential text
>>       $j =3D 0;
>>       while ($j<16){
>>       fgets($usenet, 128);
>>       $j++;
>>       }
>>   stream_set_timeout($usenet, 2); // set the timeout for the fgets
>>   $j =3D 0;
>>       while (!feof($usenet)){
>>       $ret =3D fgets($usenet, 128);
>>       $ret =3D str_replace("\r", '', $ret);
>>       $ret =3D str_replace("\n", "", $ret);
>>       if  (ereg("FastEthernet", $ret)){
>>           echo "$ret \n";
>>       }
>>       if (ereg('--More--', $ret) ){
>>           fputs ($usenet, " "); // for followin=
g page
>>       }
>>       $info =3D stream_get_meta_data($usenet);
>>       if ($info['timed_out']) {
>>           $j++;
>>       }
>>       if ($j >2){
>>           fputs ($usenet, "lo");
>>           break;
>>       }
>>   }
>> }
>> echo "End.\r\n";
>> ?>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php