Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3008
  • Last Modified:

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?
0
Ashraf Hassanein
Asked:
Ashraf Hassanein
  • 3
  • 2
1 Solution
 
darren-w-Commented:
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
 
Ashraf HassaneinAuthor Commented:
Thanks I will try tomorrow in office thank you so much
0
 
Ashraf HassaneinAuthor Commented:
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.

 
skullnobrainsCommented:
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
 
Ashraf HassaneinAuthor Commented:
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
 
skullnobrainsCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now