Running Remote Commands on Server

Get help with using the PHP Secure Communications Library.

Moderator: Nuxius

Forum rules
The purpose of this forum is to provide support for phpseclib, a pure PHP SSH / SFTP / RSA library.

Posts by new users are held in a moderation queue and are not publicly visible until the post is approved.

Running Remote Commands on Server

Postby tbone587 » Sat Apr 07, 2012 12:27 am

Hey everyone I am new to this forum and I am looking for help on running remote commands on an ssh2 server. I was able to get "ls" to work, but I cant seem to navigate to a different directory using "cd /path/to/dir". Below is the script I have wrote, can someone provide any insight?

Code: Select all
<?php

        include('Net/SSH2.php');

        $ssh = new Net_SSH2('192.168.1.1);
        if (!$ssh->login('user', 'password'))
        {
                exit('Login Failed');
        }

        echo $ssh->exec('cd etc');
        echo $ssh->exec('ls');

?>
tbone587
Traveler
 
Posts: 6
Joined: Sat Apr 07, 2012 12:23 am

Re: Running Remote Commands on Server

Postby TerraFrost » Sat Apr 07, 2012 1:56 am

The following link explains why that doesn't work:

http://phpseclib.sourceforge.net/docume ... successive
TerraFrost
Legendary Guard
 
Posts: 12243
Joined: Wed Dec 04, 2002 6:37 am

Re: Running Remote Commands on Server

Postby tbone587 » Sat Apr 07, 2012 5:20 am

I dont understand why i cant navigate to another directory per your resonse. Can you elaborate? I am also trying to do an sftp connection and that is failing..
tbone587
Traveler
 
Posts: 6
Joined: Sat Apr 07, 2012 12:23 am

Re: Running Remote Commands on Server

Postby TerraFrost » Sat Apr 07, 2012 5:56 am

It's like if you do putty, do "cd ..", close putty, open it again, and do "pwd". It'll be as though the "cd .." never happened because it's a whole new CLI instance. That's just what $ssh->exec() does.

As for the SFTP problems you're having.. can you post a log? Here's an example of how to get one:

Code: Select all
<?php
include('Net/SFTP.php');

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}

echo $sftp->pwd() . "\r\n";
$sftp->put('filename.ext', 'hello, world!');
print_r($sftp->nlist());
?>
TerraFrost
Legendary Guard
 
Posts: 12243
Joined: Wed Dec 04, 2002 6:37 am

Re: Running Remote Commands on Server

Postby tbone587 » Sat Apr 07, 2012 1:38 pm

That command is not outputting any logs to the browser. I have posted the code below for my sftp script.. How would you go about changing directories than listing the content or running a command within another directory?

Code: Select all

<?php

        include('Net/SFTP.php');

        $listofips = file("ew_ips.txt");
        $listofhost = file("ew_names.txt");
        $counter = 0;

        echo "<pre>";
        foreach ($listofips as $ip)
        {

                $conf = ".conf1";
                $name = chop($listofhost[$counter]) . $conf;

                $sftp = new Net_SFTP($ip);
                if (!$sftp->login('user', 'password'))
                {
                        exit('Login Failed');
                }

                else
                {
                        $sftp->get("/path/to/$name");
                }

                $counter++;

        }

        print_r($sftp->nlist());
        echo "</pre>";
?>

tbone587
Traveler
 
Posts: 6
Joined: Sat Apr 07, 2012 12:23 am

Re: Running Remote Commands on Server

Postby TerraFrost » Sat Apr 07, 2012 3:36 pm

Look at your $sftp->get("/path/to/$name") function. get() returns a string but you're not saving that string anywhere. Either do file_put_contents() with the data you get back or do $sftp->get("path/to/$name", $localfilename); ie. note the second parameter.

Also, fwiw, I posted the wrong example code for logging. To do logging do this:

Code: Select all
<?php
include('Net/SFTP.php');
define('NET_SFTP_LOGGING', NET_SFTP_LOG_COMPLEX); // or NET_SFTP_LOG_SIMPLE

$sftp = new Net_SFTP('www.domain.tld');
if (!$sftp->login('username', 'password')) {
    exit('Login Failed');
}

print_r($sftp->nlist());
echo $ssh->getSFTPLog();
?>

Note the define() after the inclde() and teh $ssh->getSFTPLog().

Anyway,
TerraFrost
Legendary Guard
 
Posts: 12243
Joined: Wed Dec 04, 2002 6:37 am

Re: Running Remote Commands on Server

Postby tbone587 » Sat Apr 07, 2012 3:46 pm

I still get no logs.. The command I essentially need to replicate for multiple ips in an array is:

scp user@192.168.1.1:/etc/config/file.conf1 .
tbone587
Traveler
 
Posts: 6
Joined: Sat Apr 07, 2012 12:23 am

Re: Running Remote Commands on Server

Postby TerraFrost » Sun Apr 08, 2012 3:06 am

Did the tip I gave about $sftp->get() work?

Also, I posted the wrong logging code lol. I posted the code to get you SFTP logs. The code to get SSH logs is as follows:

<?php
include('Net/SSH2.php');
define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->getLog();
?>
TerraFrost
Legendary Guard
 
Posts: 12243
Joined: Wed Dec 04, 2002 6:37 am

Re: Running Remote Commands on Server

Postby tbone587 » Sun Apr 08, 2012 2:46 pm

Sftp get didnt work, because i cant establish a connection.. Oh well, i used scp through perl and it worked to download files off remote box.. I wanted to do everything via php, but if i cant estabilsh a connection, i cant use it.
tbone587
Traveler
 
Posts: 6
Joined: Sat Apr 07, 2012 12:23 am

Re: Running Remote Commands on Server

Postby TerraFrost » Sun Apr 08, 2012 3:32 pm

If it weren't for the fact that I gave you the wrong code sample twice I'd be more than a little annoyed with you for not posting the log files so we could figure out what's going on. Meh.
TerraFrost
Legendary Guard
 
Posts: 12243
Joined: Wed Dec 04, 2002 6:37 am

Re: Running Remote Commands on Server

Postby tbone587 » Sun Apr 08, 2012 3:48 pm

I would love to figure out what is going on. What do you need from me to help me get this sftp working?
tbone587
Traveler
 
Posts: 6
Joined: Sat Apr 07, 2012 12:23 am

Re: Running Remote Commands on Server

Postby TerraFrost » Tue Apr 10, 2012 4:44 am

So if you could get me the logs that'd be great. Let's do the SSH logs. Just add the parts in red:

<?php
include('Net/SSH2.php');
define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
exit('Login Failed');
}

echo $ssh->exec('pwd');
echo $ssh->getLog();
?>

The define() will need to be after you include Net/SFTP.php.

Thanks!
TerraFrost
Legendary Guard
 
Posts: 12243
Joined: Wed Dec 04, 2002 6:37 am


Return to phpseclib support

Who is online

Users browsing this forum: No registered users and 1 guest