Page 1 of 1

web interface to run scripts/commands on remote PCs usin ssh

Posted: Sat Aug 30, 2008 4:20 pm
by ub007
Hello,

This is my first post and hope i wont be disappointed .

I have setup an intranet of 10 machines(all running Ubuntu).
I use ssh from command line to administer this n/w & to run scripts on each of them.I just thought about creating a browser interface to do this job so that someone else could easily do the same in a user friendly convenient way.In doing so I would be providing a layer of abstraction to the new user.I'm not thinking bout security at the moment!."After an hour of googling,i think PHP could do the job.
In a nutshell
-a browser interface under Apache web server that would connect to any of the machines in the intranet (as selected by the user).
-the connection would be via ssh

To give a more clearer picture:

At the end of it,i would like to have an interface on machine A,say with 9 PCs listed on the GUI.I select machine 3-the click connects to machine 3 from machine A via ssh.I would then have say eg-'buttons' for 3 tasks...select task 1 and it would run a perl script which resides on machine 3.....so on...

Could anyone tell me how to go about accomplishing this.....I have little programming knowledge but trying to work on it,heres what i did so far....

created a form admin.php
<html>
<head><title>ClientForm</title></head>
<body>
<h1>Register for an Account:</h1>
<form action="admin_action.php" method="GET">

servername: <input type="text" name="name" /><br />
Password: <input type="password" name="pword" /><br />
<input type="submit" value="GO" />
</form>

</body>
</html>
then in another file admin_action.php,put in this code
<html>
<title>ClientTerminal</title>
<body>
<p>You entered:</p>
<?php
$servername = $_GET['name'];
$password = $_GET['pword'];
//echo "<p>servername= " . $servername . "</p>";
//echo "<p>Password = " . $password . "</p>";
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22
if(!($con = ssh2_connect($servername, 22))){
echo "fail: unable to establish connection\n";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "root", $password)) {
echo "fail: unable to authenticate\n";
} else {
// allright, we're in!
echo "okay: logged in...\n";
// execute a command
$stream = ssh2_exec($con, 'ps ax');
$output = stream_get_contents($stream);

}

}
?>
</body>
</html>
Now i access http://localhost/admin.php

Code: Select all

server name:ubuntu.surey.com
password:*******
click the GO button
i get

Code: Select all

okay: logged in...
:D

I assume the ssh is working fine:thumbsup:
but the rest of it is not....how do i get this code to execute the command :banghead:

Cheers

David

Re: web interface to run scripts/commands on remote PCs usin ssh

Posted: Sat Aug 30, 2008 7:52 pm
by Christopher
Why not just create a website that requires HTTPS and authentication. Still encrypted connections but with a web interface. If you created some web services then a single console could manage all servers.

Re: web interface to run scripts/commands on remote PCs usin ssh

Posted: Sun Aug 31, 2008 4:36 pm
by VladSun
Your approach will require all of these ubuntu servers to have a web server installed and running. I would prefer doing it like this:
- one single server with web interface;
- key based SSH auth to the other servers;
- all commands to the other servers are scripted, manage ssh command execution and optionally are wrapped by sudo;

This way a user connects to a single web interface, chooses a machine to manage and then chooses a predefined command to execute.
A typical command script would be something like:

Code: Select all

#!/bin/bash
ssh root@$1 'ps ax'

Re: web interface to run scripts/commands on remote PCs usin ssh

Posted: Mon Sep 01, 2008 4:40 pm
by ub007

Code: Select all

Your approach will require all of these ubuntu servers to have a web server installed and running.


8O why is it so,the ssh command in the code is working fine......

Re: web interface to run scripts/commands on remote PCs usin ssh

Posted: Tue Sep 02, 2008 4:54 am
by VladSun
ub007 wrote:

Code: Select all

Your approach will require all of these ubuntu servers to have a web server installed and running.


8O why is it so,the ssh command in the code is working fine......
I reread your post it seems that I misunderstood you. Sorry :)

But still, transferring username/password for SSH users is not a good idea. Also, I don't think you should use PHP SSH libraries - use predefined scripts that are run by Apache (by using sudo). Pass arguments to them and use their output.

"I'm not thinking about security at the moment!" - you should always think about security. I think that using the SSH PHP functions is a security issue.
Imagine that someone gets Apache (or FTP) user privileges and overwrite your files - "$stream = ssh2_exec($con, 'rm / -rf');" ... In other words - you give root privileges to Apache user and you make it worse - you give root privileges for all of these ubuntu servers.

And if you still want to use it this way - take a look at http://sourceforge.net/projects/shcmd/