{"id":23,"date":"2019-05-28T08:52:00","date_gmt":"2019-05-28T08:52:00","guid":{"rendered":""},"modified":"2019-08-27T16:47:23","modified_gmt":"2019-08-27T11:17:23","slug":"where-there-is-a-shell-there-is-a-way","status":"publish","type":"post","link":"https:\/\/opstree.com\/blog\/2019\/05\/28\/where-there-is-a-shell-there-is-a-way\/","title":{"rendered":"Where there is a shell, There is a way."},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align:left;\">\n<div class=\"separator\" style=\"clear:both;text-align:center;\"><a style=\"margin-left:1em;margin-right:1em;\" href=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/05\/bc1db-blog.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/05\/bc1db-blog.jpg?w=300\" width=\"400\" height=\"265\" border=\"0\"><\/a><\/div>\n<p>Well, as a DevOps; I like to play around with shell scripts and shell commands especially on a remote system as it just adds some level of fun in it. But what&#8217;s more thrilling than running shell scripts and command on the remote server, making them return&nbsp;the dynamic web pages or JSON from that remote system.<\/p>\n<p>Yes for most of us it comes as a surprise that just like PHP, JSP, ASP shell scripts can also return us dynamic web pages but, as long time ago a wise man said: &#8220;where there is a shell there is a way&#8221;.<\/p>\n<h3 style=\"text-align:left;\">Isn&#8217;t PHP or JSP a better option for web development?<\/h3>\n<div style=\"text-align:left;\">For a web developer &#8230; yes, but as a DevOps, I want to do all possible stuff from a shell script. And it is quite useful for us to have a shell script as a server-side language for us as we all know the power of shell scripts.<\/div>\n<h3 style=\"text-align:left;\">Why do we need this exactly?<\/h3>\n<div>Isn&#8217;t &#8216;for fun&#8217; is an obvious reason. But for those who want more than that, I got some points<\/p>\n<ul style=\"text-align:left;\">\n<li>We can use it as a time series based data exporter.<\/li>\n<li>We might want an API that returns us the system info in the form of JSON, and we don&#8217;t have access to PHP<span style=\"background-color:white;font-size:15px;\">.<\/span><\/li>\n<li>We might want to see the system information as a web page when we hit a URL.<\/li>\n<li>It&#8217;s not only limited to system info you can do whatever you want from it.<\/li>\n<li>With bare minimum on your machine, you can get the max out of it<\/li>\n<\/ul>\n<\/div>\n<h3 style=\"text-align:left;\">Let&#8217;s get started<\/h3>\n<div>Now let&#8217;s get done with the boring part i.e. configuring Apache<\/div>\n<div>Now I am assuming that Apache is installed on that system as it is needed in order to serve your web pages. So, in order to let Apache serve your script, you need to enable&nbsp;the CGI&nbsp;config by simple commands.<\/div>\n<div>\n<pre class=\"linenums\">$ cd \/etc\/apache2\/mods-enabled\n$ sudo ln -s ..\/mods-available\/cgi.load<\/pre>\n<\/div>\n<div>and you are ready to go.<\/div>\n<div>Now move to dir where you are going to put your shell scripts.<\/div>\n<div>\n<pre class=\"linenums\">$ cd \/usr\/lib\/cgi-bin<\/pre>\n<\/div>\n<div>Once in the dir create a new file hello.sh<\/div>\n<div>\n<pre class=\"linenums\">$ vim hello.sh<\/pre>\n<\/div>\n<div>and write the following scripts<\/div>\n<div>\n<pre class=\"linenums\" style=\"background-color:whitesmoke;border-radius:4px;border:1px solid #cccccc;box-sizing:border-box;line-height:1.42857;margin-bottom:10px;overflow-wrap:break-word;overflow:auto;padding:9.5px;word-break:break-all;\"><span style=\"font-size:16px;\">#!\/bin\/bash\necho \"Content-type: text\/html\"\necho \"\"\necho \"hello world! from shell script\"<\/span>\n<\/pre>\n<\/div>\n<div>Make sure you make that file executable.<\/div>\n<div><\/div>\n<div>Now I think you have got the pretty much idea what your webpage is going to display.<\/div>\n<div>So restart the Apache server<\/div>\n<div>\n<pre class=\"linenums\">$ sudo systemctl restart apache2.service<\/pre>\n<\/div>\n<div class=\"separator\" style=\"clear:both;text-align:center;\"><\/div>\n<div class=\"separator\" style=\"clear:both;text-align:center;\"><a style=\"margin-left:1em;margin-right:1em;\" href=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/05\/38976-screenshot2bfrom2b2019-03-222b12-32-20.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/05\/38976-screenshot2bfrom2b2019-03-222b12-32-20.png?w=300\" width=\"400\" height=\"224\" border=\"0\"><\/a><\/div>\n<h3 style=\"clear:both;text-align:left;\">Let&#8217;s take it to the next level<\/h3>\n<div>Now let&#8217;s see what else can we do, Unlike PHP or JAVA or Python we don&#8217;t have any framework for shell scripts, so we might have to work a bit. But that&#8217;s the fun part, right?<br \/>\nSo let&#8217;s get started<\/p>\n<div style=\"text-align:left;\">Now we are simply going to display that which user is using \/usr\/sbin\/nologin shell<\/div>\n<div style=\"text-align:left;\"><\/div>\n<div style=\"text-align:left;\">So here are some files that I created in cgi-bin directory in order to display that data as the web page<\/div>\n<div style=\"text-align:left;\"><\/div>\n<div style=\"text-align:left;\">Header file<\/div>\n<pre class=\"linenums\" style=\"background-color:whitesmoke;border-radius:4px;border:1px solid #cccccc;box-sizing:border-box;line-height:1.42857;margin-bottom:10px;overflow-wrap:break-word;overflow:auto;padding:9.5px;word-break:break-all;\"><\/pre>\n<pre>&lt;!doctype html&gt;\n&lt;html lang=\"en\"&gt;\n  &lt;head&gt;\n    &lt;!-- Required meta tags --&gt;\n    &lt;meta charset=\"utf-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\"&gt;\n\n    &lt;!-- Bootstrap CSS --&gt;\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.3.1\/css\/bootstrap.min.css\" integrity=\"sha384-ggOyR0iXCbMQv3Xipma34MD+dH\/1fQ784\/j6cY\/iJTQUOhcWr7x9JvoRxT2MZw1T\" crossorigin=\"anonymous\"&gt;\n\n    &lt;title&gt;Hello, world!&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;h1&gt;All the user using \/usr\/sbin\/nologin shell&lt;\/h1&gt;\n \n &lt;table class=\"table\"&gt;\n  &lt;thead&gt;\n    &lt;tr&gt;\n      &lt;th scope=\"col\"&gt;Name&lt;\/th&gt;\n      &lt;th scope=\"col\"&gt;User Id&lt;\/th&gt;\n      &lt;th scope=\"col\"&gt;Group Id&lt;\/th&gt;\n    &lt;\/tr&gt;\n  &lt;\/thead&gt;\n  &lt;tbody&gt;<\/pre>\n<div>Footer file<\/p>\n<pre class=\"linenums\">&lt;\/tbody&gt;\n&lt;\/table&gt;\n\n    &lt;!-- Optional JavaScript --&gt;\n    &lt;!-- jQuery first, then Popper.js, then Bootstrap JS --&gt;\n    <a href=\"https:\/\/code.jquery.com\/jquery-3.3.1.slim.min.js\" target=\"_blank\" rel=\"noopener\">https:\/\/code.jquery.com\/jquery-3.3.1.slim.min.js<\/a>\n    <a href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/popper.js\/1.14.7\/umd\/popper.min.js\" target=\"_blank\" rel=\"noopener\">https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/popper.js\/1.14.7\/umd\/popper.min.js<\/a>\n    <a href=\"https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.3.1\/js\/bootstrap.min.js\" target=\"_blank\" rel=\"noopener\">https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.3.1\/js\/bootstrap.min.js<\/a>\n  &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<\/div>\n<table class=\"table\" style=\"height:18px;\" width=\"411\">\n<thead>\n<tr>\n<th scope=\"col\"><\/th>\n<th scope=\"col\"><\/th>\n<th scope=\"col\"><\/th>\n<\/tr>\n<\/thead>\n<\/table>\n<div class=\"separator\" style=\"clear:both;text-align:center;\"><\/div>\n<p>hello.sh<\/p>\n<\/div>\n<div>\n<pre class=\"linenums\" style=\"background-color:whitesmoke;border-radius:4px;border:1px solid #cccccc;box-sizing:border-box;line-height:1.42857;margin-bottom:10px;overflow-wrap:break-word;overflow:auto;padding:9.5px;word-break:break-all;\"><span style=\"font-size:16px;\">#!\/bin\/bash\necho \"Content-type: text\/html\"\necho \"\"\ncat header\ncat \/etc\/passwd | awk -F ':' '{if($7 == \"\/usr\/sbin\/nologin\"){print \"\"$1\"\"$3\"\"$4\"\"}}'\ncat footer <\/span>\n<\/pre>\n<\/div>\n<div><\/div>\n<div>So let&#8217;s just see what all those files are<\/div>\n<div><\/div>\n<div>Header file and footer file basically contains the starter template of bootstrap that gives you a prebuild web template, and in hello.sh we are extracting those file by using cat and in the middle, we are writing a shell command in order to get the users that are using \/usr\/sbin\/nologin shell and making a template from it using awk.<\/div>\n<div><\/div>\n<div>So now when you hit the same URL output will be like<\/p>\n<\/div>\n<div class=\"separator\" style=\"clear:both;text-align:center;\"><a style=\"margin-left:1em;margin-right:1em;\" href=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/05\/d8e01-screenshot2bfrom2b2019-04-252b14-39-59.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/opstree.com\/blog\/\/wp-content\/uploads\/2019\/05\/d8e01-screenshot2bfrom2b2019-04-252b14-39-59.png?w=300\" width=\"400\" height=\"227\" border=\"0\"><\/a><\/div>\n<div><\/div>\n<div>Now I guess we got the base idea that how can we use a shell script to display web pages of our need. We can also use it as an API as it can return JSON as well. But it&#8217;s up to the individual how well we can use it for.<\/p>\n<\/div>\n<div>\n<h3 style=\"clear:both;\">Summary<\/h3>\n<\/div>\n<div>So, in this blog, we saw how with bare minimum we can get most out of it. It is not limited to just some use cases it can be used to create an API which can return valuable information of system or services running on the system. With some good scripting and some tricky HTML template designing, we can achieve a lot.<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Well, as a DevOps; I like to play around with shell scripts and shell commands especially on a remote system as it just adds some level of fun in it. But what&#8217;s more thrilling than running shell scripts and command on the remote server, making them return&nbsp;the dynamic web pages or JSON from that remote &hellip; <a href=\"https:\/\/opstree.com\/blog\/2019\/05\/28\/where-there-is-a-shell-there-is-a-way\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Where there is a shell, There is a way.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":152279032,"featured_media":29900,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[3768,2674,768739285,290243,12397870,7585,768739284,69070,332413],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/11\/DevSecOps-1.jpg","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pfDBOm-n","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/23"}],"collection":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/users\/152279032"}],"replies":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":7,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":1214,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions\/1214"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media\/29900"}],"wp:attachment":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}