?
Solved

SSH session via PHP script

Posted on 2013-12-19
6
Medium Priority
?
2,743 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
darren-w- earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 27

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 27

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Suggested Courses

801 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