Solved

SSH session via PHP script

Posted on 2013-12-19
6
2,407 Views
Last Modified: 2014-01-05
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?
0
Comment
Question by:Ashraf Hassanein
  • 3
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
darren-w- earned 500 total points
ID: 39730681
With exec I think you are calling it incorrectly, should be :
$output = array();
$return_var = array();
exec('sshpass -p password ssh -1 usernamel@server xxxxx command',$output,$return_var);

print_r(array($output,$return_var)));

Open in new window


try this and see if you get any output in the 2 variables.
0
 

Author Comment

by:Ashraf Hassanein
ID: 39730703
Thanks I will try tomorrow in office thank you so much
0
 

Author Comment

by:Ashraf Hassanein
ID: 39731441
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?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 26

Expert Comment

by:skullnobrains
ID: 39731557
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
0
 

Author Comment

by:Ashraf Hassanein
ID: 39731670
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?
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 39732511
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

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now