[racktables-users] Re: SSH commands from PHP

  • From: J Lee <juleedev@xxxxxxxxx>
  • To: racktables-users@xxxxxxxxxxxxx
  • Date: Tue, 28 Aug 2012 10:59:10 -0400

Here's an example of something i already have.  You should be able to
modify quite easily to suit your needs.  Some key points:

1.  always use read() and write() in pairs.
2.  you'll have to change the "nameofprompt" to a proper regular
expression.  should be pretty straight forward.
3.  If you get the following message from phpseclib: "Notice: Undefined
offset: 0 in /var/www/phpseclib/Net/SSH2.php on line 1907"

you will need to implement a little fix with their code.  Let me know if
you need and I will forward along.

If i have some time later on, i'll write up some sample code for you using
the specs you outlined below and post back.

hope that helps.
j

<?php

    // sample code to demo how to use phpseclib to create an interactive
ssh session.
    set_include_path(get_include_path() . PATH_SEPARATOR . '../phpseclib');
    include('Net/SSH2.php');

    //define('NET_SSH2_LOGGING', true); //turn on logging.
    //define('NET_SSH2_LOGGING', 'NET_SSH2_LOG_COMPLEX');


    $ssh = new Net_SSH2('10.10.10.10'); //starting the ssh connection to
localhost
    if (!$ssh->login('id', '123')) { //if you can't log on...
      exit('Login Failed');
    }
    else  {
    echo 'logged in<br>';
    }
    $output= $ssh->write("\n"); //press any key to continue prompt;
    $prompt=$ssh->read('/(nameofprompt#\s*)/i', NET_SSH2_READ_REGEX);
    $output = $ssh->write("no page\n");
    $prompt=$ssh->read('/(nameofprompt#\s*)/i', NET_SSH2_READ_REGEX);
    $output = $ssh->write("show interfaces brief\n");
    if ($output) {
     $output=$ssh->read('/(nameofprompt#\s*)/i', NET_SSH2_READ_REGEX);
     echo $output;
    }

On Tue, Aug 28, 2012 at 10:26 AM, Marc <marc@xxxxxxxxxxxxxxxxx> wrote:

>  http://phpseclib.sourceforge.net/index.html
>
> Here you can find some examples and from the looks off it maybe it can be
> implementated with racktables.
>
> On 8/28/12 3:50 PM, Denis Ovsienko wrote:
>
> 28.08.2012, 17:37, "J Lee" <juleedev@xxxxxxxxx> <juleedev@xxxxxxxxx>:
>
>  hey there.  I saw a message problems using the 802.1q section in 
> racktables... and also about using SSH in scripts.
>
> I don't know too much about racktables yet but just fyi. using phpseclib, it 
> looks like you can run multiple SSH commands from php script.
> if you'd like i can post some sample code i've been playing around with.
> it may or may not help.
>
> j
>
>  Sure, a working sample could help here. What is required is a script that 
> could take an input file with commands like this:
>
> conf t
> int gi0/10
> switchport access vlan 2
> end
>
> ...and produce an output file with an exact copy of the session.
>
>
>
> --
> *Met vriendelijke groet,*
>
> Marc de Rooy
> Technische medewerker
> Datacenter Engineer
> Flexwebhosting/FXW
>
> DISCLAIMER De informatie opgenomen in dit bericht kan vertrouwelijk zijn
> en is uitsluitend bestemd voor de geadresseerde. Indien u dit bericht
> onterecht ontvangt, vragen wij u het volgende: gebruik de inhoud niet;
> informeer de afzender door het bericht te retourneren; en vernietig de
> inhoud.
>
> The information contained in this message may be confidential and is
> intended to be exclusively for the addressee. If you have received this
> message in error, please do not use the contents, inform us immediately and
> delete its content.
>

Other related posts: