Solved

SSH session via PHP script

Posted on 2013-12-19
6
2,494 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Hello to you all, I hear of many people congratulate AWS (Amazon Web Services) on how easy it is to spin up and create new EC2 (Elastic Compute Cloud) instances, but then fail and struggle to connect to them using simple tools such as SSH (Secure…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

777 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