Link to home
Start Free TrialLog in
Avatar of Ashraf Hassanein
Ashraf Hassanein

asked on

SSH session via PHP script

I have a server which can be accessed only using sshv1 when I run the command from my portal towards this server":
       sshpass -p password ssh -1 usernamel@server xxxxx command            
      
  I am getting output which requires to press enter to get more something like this:
 Info: The max number of VTY users is 10, and the number
      of current VTY users on line is 1.
<Server>
       Output
               bla = bla
               bla = bla
  ---- More ----
                    bla = bla

  ---- More ----
                    bla = bla
  ---- More ----
                    bla = bla
  ---- More ----
                     bla = bla      
  ---- More ----
                    bla = bla
 
(Number of Results = 1)
<Server>
Info: The max number of VTY users is 10, and the number
      of current VTY users on line is 0.

 Then the session is terminated.
 I want to create a php script which runs this command, catch the output in an array and parse it in an html table, but I am failing to get the output.

  I have installed lib-ssh2 (But as I can see it will be using SSH2),
  I did the following attempts but with no luck:
Attempt1:
<?php
$raw  = array();
exec('sshpass -p password ssh -1 usernamel@server xxxxx command',$raw);
var_dump($raw);
?>

Here I get the following:
Read from remote host Server IP: Connection reset by peer
array(0) {
}

Attempt2:
<?php
$raw  = array();
$connection = ssh2_connect('Server IP', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, 'command');
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
echo stream_get_contents($stream_out);
?>

Here the script keeps hanging with nothing back

Can someone please guide me what to do?
ASKER CERTIFIED SOLUTION
Avatar of darren-w-
darren-w-
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ashraf Hassanein
Ashraf Hassanein

ASKER

Thanks I will try tomorrow in office thank you so much
Well I can see some output now if the output is small, but if the output the big and the session waiting at "---- More ----"waiting to press enter so the session is hanging, is it possible to send in the exec keypress to kill the session (i.e. ctrl c) or keep sending keypress enter until the output is done?
Avatar of skullnobrains
you problem is more system-related because the remote server assumes you are running through a terminal which is not the case. most likely, the use of sshpass is the culprit since it emulates a tty/pty

i'd advise you don't use sshpass but rather setup keys or even use something entirely different to get your information

alternatively you can use an expect script to do what sshpass does (easy to find on the web, i can provide a working one if you want) and hack it to send a carriage return when it reads "---more---"

expect is also available in php but i only have a workable version for the shell command

i don't recommend this solution anyway. setting up keys is much cleaner

---

if you need to read/write to a process's stdin/stdout, you can use proc_open instead of exec in php. i'm not sure this will work in your case because sshpass may not hand what it reads on it's stdin to ssh

if you use proc_open, most likely you can send the password yourself using proc_open instead of using sshpass like you would in expect
But if I have the expect script I can execute by php correct? can you send me a copy where it sends enter when it pauses on more?
i found this script i've been using some time ago in an achive. it should be quite trivial to edit it to fit your needs. then  i don't see a reason to go through this hassle instead of setting up key-based authentication or using something else than ssh to reach your goal. this is both more complicated and insecure.

#!/usr/bin/expect -fi
spawn sh -c "ssh -o TCPKeepAlive=yes -o ServerAliveInterval=30 -o PubkeyAuthentication=no $argv"
set timeout 60
while { true } {
	expect	"Password"		{ send "PASSWORD\n" }	\
		"assword:*"		{ send "PASSWORD\n" }	\
		"DSA"			{ send "yes\n" }	\
		"(yes/no)? "		{ send "yes\n" }	\
		eof			{ exit 0 }		\
		"lost connection"	{ exit 6 }		\
		"Warning: Permanently added * to the list of known hosts." { exp_continue }	\
		"Warning:*"		{ exp_continue }	\
		"Offending:*"		{ exp_continue }	\
		"Matching:*"		{ exp_continue }	\
		"*passphrase*"		{ send "PASSPHRASE\n" }	\
		timeout			{ send_user "\n==> session timeout\n" ; exit 1 }		\
		busy       {puts busy\n ; exp_continue} \
		"Connection to * closed." { exit 4 } \
		"?*"			{ send_user "\n==> your turn\n" ; set interact "yes" ; break }
}
	#	-re "^\s*$"	{ exp_continue } \

if { $interact == "yes" } {
	interact
}

Open in new window