Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BASH scripts 2

Status
Not open for further replies.

Hondy

Technical User
Mar 3, 2003
864
0
0
GB
Hi

2 questions...

1. can you execute a BASH script from within HTML without using a link? ( want the script to run when the page is loaded rather than have someone click a link)

2. How do you make a HTML page update as the script is running rather than wait until the entire script has ran then showing all the results at once?

Thanks
 
Umm, 1) you're asking to run server side scripting? (cgi-bin)
2) you want a holding page waiting for output whilst the external command is spawned and output?
 
Jad

1. Yes, i want to run it like:
<html>
<body>
/cgi-bin/myscript.sh
</body>
</html>


2. I have done it before and is wasn't too complicated but I can't remember how I did it. Imagine a ping script and it outputs each response as it is returned rather than wait until all 4 pings are completed and dumping the whole lot to the screen in one go.

Thanks
 
the first one needs to be more like:

#!/usr/bin/perl
print <<EOM
Content-type: text/html\r\n\r\n
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<META HTTP-EQUIV="Pragma" NAME="no-cache" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" NAME="Expires" CONTENT="-1">
</HEAD>
<BODY>
EOM
;
open(MYSCRIPT, "/cgi-bin/myscript.sh|");

while (<MYCRIPT>) {
print;
}

print '</BODY></HTML>' . "\n";

Assuming that you can run Perl in your cgi-bin.

The second one needs you to run one cgi-bin, creating the spawned process and setting a <META HTTP-EQUIV="Refresh" CONTENT="300; URL=MyUrl?"> tag in the Header
 
thanks for the response Jad, I see what you are saying but is there no way to call the bash script from a web page? You can do by putting it into a link, obviously this puts the script path into the browsers address bar - if it was PHP you could mark the code out with <? "script" ?> for example.

I'm not sure about the second question, I'm sure theres a way to do it without refreshing the page. :S

Thanks
 
why not use SHELL_EXEC in php?

QatQat

If I could have sex each time I reboot my server, I would definitely prefer Windoz over Linux!
 
There is no reason why you can't implement a CGI script in bash.

Code:
#!/usr/bin/bash

echo "Content-type: text/html"
echo
echo "<html><body><pre>"
uptime
echo "</pre></html></body>"

Let's say that is called uptime.cgi, place it in a directory on your web server which allows ExecCGI (assuming Apache here), typically aliased to , and it should work as you expect.

Not sure it's a very secure mechanism though!

Annihilannic.
 
Qatqat - I need to run bash because I have some scripts that do some tftp and telneting, unless you can use php to do the same things?

Anni - I want to be able to call the scripts from a html page rather than generate the page using bash. I'm not too worried about the security of it because its in an enclosed internal environment.
 
Generally you can run shell scripts from a PHP page.

If you write a shell script that transfers files and runs without interaction then you can simply execute it from a php page.

If you instead want to run File transfer managed through a web page, PHP has its own FTP functions; you can also create a fairly interactive FTP session from a web interface.


I recommend anyway you have a look at SCP (uses SSH protocol and encryption). It is quite easy to script file transfers using SCP, after you have correclty setup your certificates to prevent having to pass passwords through web pages.
It is also quite easy to interact with scp using php.

Concerning telnet or better SSH there are many articles on the web on how you can achieve that with PHP. Coding a page that allows an interactive SSH session on the web is no trivial task though. You can start by getting libssh2 and recompiling your PHP with support for it.
For the rest you better post on the PHP forum as it will get rather complex.

QaTQat

If I could have sex each time I reboot my server, I would definitely prefer Windoz over Linux!
 
thanks for your detailed response, have a star :)

Can i give you an example of what I am trying to do? The annoying thing is I have done it before but I can't remember what I did.

If you look at
you can see what I mean, the page updates as it's going along :s

Thanks
 
well first of all, all commands you run from a PHP script are executed as the apache user so you will need a lot of "sudoing" to do. Info about sudo, "man sudo".

coming to the example you have pointed out, if you run a traceroute command using shell_exec you will notice that it behaves as any server-side generated page. PHP renders the page in cache and then sends it out to the browser when page is complete.



Try the following example

Code:
<?php
$display_output = shell_exec('traceroute [URL unfurl="true"]www.redhat.com');[/URL]
echo "<pre>$display_output</pre>";

?>

It should do nothing at the beginning and then it should spit out the command output at once.

If you want to get output "as it happens" you probably need to loop through the lines of $display_output and use flush() function to send the output to the browser immediately.


QaTQat

If I could have sex each time I reboot my server, I would definitely prefer Windoz over Linux!
 
Thanks Qatqat - flush looks like it will do what I need.

The odd thing is that I have done this before without it :s

Looking at it another way, is there any way to get a bash script to execute then move on whilst the rest of the page is loaded.

My main issue is that the page takes too long to render and the user doesn't really know what is going on.
 
A quick (and very very cheap) solution would be to execute the script in a iframe.

QatQat

If I could have sex each time I reboot my server, I would definitely prefer Windoz over Linux!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top