Newbie to using PHP to run commands on a server.

deceptive

Banned
Joined
Nov 1, 2015
Messages
82
Reaction score
0
Location
Pretoria
Hey all,

Just playing around with ssh2 and would like some advice. My end goal would be having a form that a user fills, then have the parameters of the form parsed to ssh2 a command executed on the remote server with the parameters defined above, all in php.

What I have so far is the HTML form and some PHP to set variables as the form input.

<form action="test.php" method="post"><br>
<p>string: </p> <input type="text" name="string" /><br>
<br><input type="submit" value="send" /><input type="reset" />
</form>

and
<?php
$string = $_POST['string'];

Problem comes in with the ssh2 function ssh2_exec syntax. I want to execute 'mkdir <string>' on the remote server. I have the basic SSH auth (with key) so I just need to know how to define the command.

ssh2_exec($connection, 'command');
is the normal syntax, but how would I add the $string to it?

ssh2_exec($connection, 'mkdir ', $string);
does not work.


I am very new to programming and this is just because I like playing around with it, I know PHP isn't the best option, so please just try and assist me with this.

Thanks
 
try:
Code:
ssh2_exec($connection, 'mkdir '.$string);

Use the . to join the 2 strings ('mkdir' and $string)


I presume this is for something controlled and internal to your network and not a visible to the internet thing?
Just make sure some ass can't input something like ' new_directory;rm -rf /;'
 
Last edited:
try:
Code:
ssh2_exec($connection, 'mkdir '.$string);

Use the . to join the 2 strings ('mkdir' and $string)


I presume this is for something controlled and internal to your network and not a visible to the internet thing?
Just make sure some ass can't input something like ' new_directory;rm -rf /;'

Indeed, I'm playing around with a local "Control my home" site my family can use to do various things without having to know how to use a terminal.

Seems I got it sorted!
 
How do you control your home ?

lights, security, electric usage, internet, netflix region, remote reboot things, PUSH alerts etc all controlled from a locally hosted web site so people can do things without having to "do" them from a browser when you're home, or via OpenVPN when away.

using 4 rapsis for it all, seems to work okish, will be debugging and learning more as time goes on
 
Top
Sign up to the MyBroadband newsletter
X