#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0

    SSH get uptime multiple servers


    Hi,

    I am trying to get the uptime from a list of servers from an array.

    PHP Code:
    $servers = array(
        array(
    'ip'=>'server1','user'=>'my_username','pass'=>'my_pw'),
        array(
    'ip'=>'server2','user'=>'my_username','pass'=>'my_pw'),
        array(
    'ip'=>'server3','user'=>'my_username','pass'=>'my_pw'));

    foreach(
    $servers as $server)
    {
        
    $connection ssh2_connect($server['ip'], 22);
        if (
    ssh2_auth_password($connection$server['user'], $server['pass']))
        {
            
    $server['ip'] = stream_get_contents(ssh2_exec($connection'uptime'));
    echo 
    stream_get_contents($server['ip']);
        }

    It fails with the following error:

    PHP Warning: stream_get_contents() expects parameter 1 to be resource, string given in.

    Any ideas?,

    Thanks.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    Put some error handling in here. var_dump the results of your ssh2_exec call, see what's in there.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,932
    Rep Power
    4033
    Originally Posted by darrenmccabe
    PHP Code:
            $server['ip'] = stream_get_contents(ssh2_exec($connection'uptime'));
    echo 
    stream_get_contents($server['ip']); 
    You're calling stream_get_contents twice. The second time on the return value of the previous call rather than a stream.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    How do you suggest I modify?,


    Thank you for your help.
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    Don't do it the second time.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    Hi Dan,

    I removed echo stream_get_contents($server['ip']); and it does not display anything still.
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    Do you know PHP at all? The echo line contains an unnecessary call to stream_get_contents. Remove that call. NOT THE WHOLE LINE.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,969
    Rep Power
    374
    what you are saying is:

    variable holds the contents of a file i am getting from somewhere
    display the contents of a a file called "variable"..

    does that make sense?

    clearly you should just do echo stream_get_contents(ssh2_exec($connection, 'uptime')); & get rid of second line!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    11
    Rep Power
    0
    PHP Code:
    $servers = array(
        array(
    'ip'=>'server1','user'=>'my_username','pass'=>'my_pw'),
        array(
    'ip'=>'server2','user'=>'my_username','pass'=>'my_pw'),
        array(
    'ip'=>'server3','user'=>'my_username','pass'=>'my_pw')); 

    foreach(
    $servers as $server)
    {
        
    $connection ssh2_connect($server['ip'], 22);
        if (
    ssh2_auth_password($connection$server['user'], $server['pass']))
        {
            echo 
    $server['ip'] = stream_get_contents(ssh2_exec($connection'uptime'));

    var_dump($server['ip']);

        }

    When ran it displays the following from var_dump, it looks like it's not passing $server['ip']




    string(0) ""
    string(0) ""
    string(0) ""
  18. #10
  19. Web Developer/Musician
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Nov 2004
    Location
    Tennessee Mountains
    Posts
    2,408
    Rep Power
    1031
    Don't rewrite values in the array you are looping through.

    PHP Code:
    $servers = array(
        array(
    'ip'=>'server1','user'=>'my_username','pass'=>'my_pw'),
        array(
    'ip'=>'server2','user'=>'my_username','pass'=>'my_pw'),
        array(
    'ip'=>'server3','user'=>'my_username','pass'=>'my_pw')); 
    $result = array();
    foreach(
    $servers as $server)
    {
        
    $connection ssh2_connect($server['ip'], 22);
        if (
    ssh2_auth_password($connection$server['user'], $server['pass']))
        {
           
    $result[$server['ip']] = stream_get_contents(ssh2_exec($connection'uptime'));

           
    var_dump($result[$server['ip']]);

        }

    I haven't used the ssh2 extension (I use phpseclib) before so I'm not even sure that is the right way to get the output of ssh2_exec but you needed a separate array to put results into.
    Last edited by Hammer65; August 22nd, 2013 at 09:39 AM.

IMN logo majestic logo threadwatch logo seochat tools logo