Bash Script to Display in browser

Hi all, Is it possible to call my bash script and it will run to the browser. if yes what should be programming language need to use.

Hello,

The short answer here is: yes, it is possible. And as for the programming language you'd use: well, you've said you want to make a Bash script accessible via the Web, so the language would be Bash.

You can make Bash scripts available via your Web server's cgi-bin functionality the same way you'd make any other executable script available, like a Perl script or similar. Now, you also have to write your Bash script to be able to be called properly via CGI, and to be able to return its output in such a way that a Web browser will be able to parse and display it.

But if your script produces no output, and takes no input, and you just want to be able to get it to run and then it does its own thing without needing any further information from the user, then it really is just as simple as making it available in your Web server's cgi-bin directory, and calling the URL of the script to execute it.

Now, one final point I do have to make. This is definitely one of those "just because you can doesn't mean you should" moments. Unless your script definitely takes no arguments and doesn't need to produce any output, or unless you are totally, absolutely, 100% certain you have written your script in such a way that it parses all HTTP headers correctly, prints all HTTP output correctly, and is in no way vulnerable to any kind of parameter injection attacks or other similar shenanigans, you probably don't want to do this. There are a lot of security implications here, and many things you need to consider to write a Bash CGI application safely.

Also, writing CGI applications in Bash is a chore. You have to do all the heavy lifting in terms of HTTP headers, CGI variables, and suchlike yourself, with no built-in functions to handle any of it. There are many other languages - Perl, PHP, Python, Ruby, etc - that make it so much easier, and safer.

So, in summary: yes, you can do this. But unless there is some truly pressing need why you absolutely have to do it, you probably shouldn't.

2 Likes

But I need some changes when it displays in the browser like I will put a search field and submit button. currently, my script runs like this sample.sh DOMAIN, I want in the browser to display like this, the DOMAIN into a search field then a submit button to follow. it is possible in CGI. thanks in advance.

Hello,

The topic of CGI programming needs far more than a simple response on a forum thread to explain it, I think. It's certainly possible to write a Bash script that displays a form when called via a Web site's CGI-BIN, takes input from that form, does something with it, and returns output based on the results. But it's not as simple as your normal methods that you'd use if you were reading command-line input, or input from a file, at a shell prompt.

For CGI programming, you have to start considering things like reading the CGI enviroment variables that the form populates, parsing them so that they have non-printable characters and other undesired information converted into a format you can use, and then doing something with them after verifying that they are safe to depend on.

Now, as I mentioned in my earlier post, many programming languages provide nice, easy ways of doing all that work for you. Bash doesn't. You'll need to write your own functions to handle all of that, and it is not straightforward to both get it right, and to make the implementation secure.

Lastly, if this question relates to your script from your other thread, which is used to SSH to remote servers - I strongly, strongly urge you to think very carefully before proceeding. Putting a script on the Web that is capable of exerting remote control over multiple servers, using SSH keys that are already trusted and which requires no password authentication, is inherently dangerous. Now there are ways of locking that down so that trusted clients only can connect to it, and of password protecting access to it, and so on and so forth, of course. But there are other things to consider too.

For instance: when a CGI script runs via a Web server, it does so with the user, group and permissions of the Web site that it is part of. Very often that translates to the 'apache' user, or the 'nobody' user. Sometimes each site on a Web server has its own specific user and group that it runs as. But in any case, it won't have access to the SSH keys of any other account except the user account that it executes as - and you wouldn't want it to be otherwise, for reasons of security and safety.

So, in short: there's a lot to consider here. Even if this isn't related to your SSH script and is something else altogether, you would still have to get to grips with the fundamentals of CGI programming, and of how to implement those methods in a Bash script. If you search the Web for 'bash cgi' you'll find various guides here and there on this topic, but it is very complex, and (particularly if you're just starting out with shell scripting) not something to jump straight into.

I'm sorry if perhaps this answer comes across as a little less enthusiastic than you would like, or discourages you. But in this day and age there are very few occasions, personally, where I would consider putting a Bash script live as a CGI application.

2 Likes

Thanks for this info mate. for you mate what should be the compatible scripting of my problem. I think I need to transfer in python scripting on this task.

Hello,

That might be better, yes. Of course, you could always take a hybrid approach and have your Python script handle the Web interface side of things, and then it could call the Bash script to do the SSH connections, get the output from the Bash script, and then return that to the Web client. But it is definitely better not to expose Bash directly to the Web as a CGI script, if better alternatives are viable for you.

1 Like

As a full-stack web developer these days, I am not understanding this topic.

When we say "in the browser" we "dev developers" mean "the code runs in the browser" and the only code which runs "in the brower" is Javascript.

Python, shell CGI, PHP, etc all run "on the server", not in the browser.

When you render HTML "in the browser" there is never any shell code, bash, python, etc. which runs in the browsers. Browsers are applications which render HTML and run Javascript.

So, if you want to:

I think this is actually your question, right @jmthecillo ?

Hi all, Is it possible to call my bash script and the output will display in the browser.

If so, you are not talking about running bash in the browser. Bash will not ever run in a browser (I do not think anyone will ever design a mainstream browser where bash runs in the browser, for sure) because browsers have code that renders HTML and the programming language used in browsers is Javascript.

All other languages mentioned in this topic are server side code. Server side code runs on the server, not in the browser.

It you want to run code in the browser, you must run Javascript. There is plenty of JS code which will run in a browser and permit you to login to another site. Here is an example:

See also:

The bottom line is that if you want to do things like SSH "in the browser" you need Javascript. Bash does not run "in the browser".

On the other hand, as @drysdalk mentioned, it is easy to run bash on the server side and display the results "in the browser" but that means you must use bash to output HTML.

Most webdev'ers would call the bash script (as an API) and render the results in JSON and then use JS in the browser to display the data; however, the idea of doing ssh in the browser as an application requires Javascript.

HTH.

See also:

Closing:

Most web developers would not call a Bash CGI on the server side which then runs SSH to another server. That's not how these kinds of tasks are done these days. However, without the OP being very specific about their actual task and what they are really trying do accomplish, it's hard to comment further, to be hoenst.

2 Likes

Thank you.

Thank you, seems I need to transfer to python and use javascript to call in the broswer.

That's one way to do it.

You could also use Ruby on Rails, which uses Ruby on the server side and gives you a structured approach for designing web apps.

Or you could go with many other technology approaches; as you like. No need to use Python for server side unless you are a Python person. I have been building all my server-side web apps using Rails for the past year and a half; and before that I was a PHP guy on the server side.

Today, I was looking into possible Ruby gems for calculating Minkowski space and Riemann manifolds because I woke up and had the idea to create a new web visualization for four dimensional spacetime; but I could not find any suitable gems (modular Ruby libs for importing into projects), so I decided to move on to other tasks today.

This topic reminds me that I have yet to search for Javascript libs for the math above; so maybe I'll search for that tomorrow.

Also, FYI... many people are now running JS on the server-side as well as the browser side; but I recommend you go with Ruby, Python or PHP on the server-side and, of course JS for code that must run in the client (the browser).

1 Like

Thanks, mate thanks for the insights.

You might also find this code example useful:

3 Likes