简体   繁体   中英

SSH connect using PHP

I am trying connect to a server via SSH using below code:

include('Net/SSH2.php'); 
$ssh = new Net_SSH2('mydomain.com'); 
if (!$ssh->login('xxxx', 'xxxx')) { 
   exit('Login Failed'); 
}else{ 
   echo "connected"; 
} 
echo $ssh->exec('pwd');

It says connected but it shows no output for the code echo $ssh->exec('pwd');

Why?

LOG MESSAGES

-> NET_SSH2_MSG_CHANNEL_REQUEST (since last: 0.0002, network: 0s)
00000000  00:00:00:00:00:00:00:04:65:78:65:63:01:00:00:00  ........exec....
00000010  03:64:69:72                                      .dir

<- NET_SSH2_MSG_CHANNEL_FAILURE (since last: 0.0611, network: 0.0001s)
00000000  00:00:00:00                                      ....

you can use a 'ssh2_connect' function in php. After use a 'ssh2_exec' and stock the result in a $var.

at the end you can use a 'stream_get_contents($var);' for return the result code command ssh

Don't forget the 'sleep(1)' function php for prevent bug after launch ssh command.

'ssh2_exec' is a resource php.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM