LoginSignup
1
0

More than 1 year has passed since last update.

【bashスクリプト実装】ブラウザからAPIのようにbashスクリプトを実行する

Last updated at Posted at 2022-09-11

背景

スクリプトを実行する際にTeratermからコマンドを実行するのに慣れてない場合
操作するのが戸惑うかと思います。

ブラウザから実行できるようにするには、

・HTML→php→python→bash
・HTML→javascript→python→bash

のような構成で実現できるのですが、phpやjavascriptを作成するのに時間がかかります。

実施内容

下記の方法で、GUIベースでもスクリプトをワンクリックで実行できるようにします。
やりたいことは

①ブラウザ→Apache→アクセスログ(access_log)にログを出力
②出力されたログをリアルタイムでtail監視
③結果を定期的に監視し、アクセスがあればファイルとして一時的なディレクトリに格納
※①~③ジョブキューのようにスクリプトを実行できるようにしました。

Apacheを標準インストール

下記で標準インストールします。

yum install httpd

htmlファイル

スクリプトを実行するためのwebブラウザです。
htmlファイルを作成し、リンクを静的コンテンツにします。

[root@homeserver001 homeserver_execbatchscript]# cat jobexec.html 
<h1>This site is job exec menu</h1></pre>
<ul><a href="https://homeserver/homeserver_execbatchscript/dummybashscript/dummy_testhoge_access_curl"><img src="" alt="testhoge.sh"></a></ul>
<ul><a href="https://homeserver/homeserver_execbatchscript/dummybashscript/dummy_phpexectest_access_curl"><img src="" alt="phpexectest.sh"></a></ul>
<ul><a href="https://homeserver/homeserver_execbatchscript/dummybashscript/dummy_testsleep_access_curl"><img src="" alt="testsleep.sh"></a></ul>
<ul><a href="https://homeserver/homeserver_execbatchscript/dummybashscript/dummy_javascripttest_access_curl"><img src="" alt="javascripttest.sh"></a></ul>
[root@homeserver001 homeserver_execbatchscript]#

監視スクリプト

60秒ごとにスクリプト実行キューを監視して、
・あればファイル名からスクリプト名を取得して、実際のスクリプトを実行する。
・なければ60秒まってから再度確認する。
仕様になっています。

[root@homeserver ~]# cat online_shellexec_extractpoling.sh
#!/bin/bash
while true
do
scriptqueuePath=/var/www/html/homeserver_execbatchscript/dummybashscript/scriptqueue/
scriptFilePath=/root
ls -1rt ${scriptqueuePath}*dummy*curl 2>/dev/null
res_2=`echo $?`
if [ ${res_2} = 0 ]; then
   queueOldest=`ls -1rt ${scriptqueuePath}*dummy*curl | head -1 2>/dev/null`
   queueCount=`ls -1rt ${scriptqueuePath}*dummy*curl | wc -l 2>/dev/null`
   if [ ${queueCount} = 0 ]; then
      sleep 60
   else
      screptFilename=`ls ${queueOldest} | head -1 | cut -b95-3000 | sed 's/_access_curl/.sh/g'`
      mv ${queueOldest} ${queueOldest}_used
      ${scriptFilePath}/${screptFilename}
      res=`echo $?`
      if [ ${res} = 0 ]; then
         echo queue is delete | logger
         rm -fv ${queueOldest}_used | logger
      fi
   fi
fi
done

Apacheアクセスログtail監視スクリプト

Apacheのアクセスログを監視するスクリプトです。
accessがきたらダミーファイルを生成します。

[root@homeserver ~]# cat online_wrap_poling_tailex.sh
#!/bin/bash
TARGET_LOG="/var/log/httpd/homeserver_execbatchscript-access_log"
scriptqueuePath=/var/www/html/homeserver_execbatchscript/dummybashscript/scriptqueue/

_senten_conditions="access_curl"

hit_action() {
    while read i
    do
        echo $i | grep -q "${_senten_conditions}"
        if [ $? = "0" ];then
            tmpfilename=`date +%Y%m%d%H%M%S%N`_`tail -1 /var/log/httpd/homeserver_execbatchscript-access_log | awk -F ' ' '{print $7}' | awk -F '/' '{print $NF}'`
            touch ${scriptqueuePath}${tmpfilename}
        fi
    done
}

tail -n 0 --follow=name --retry $TARGET_LOG | hit_action

[root@homeserver ~]#

常駐スクリプトの起動

Apacheが起動していることを確認し、上記の常駐スクリプトを&指定で実行します。

systemctl status httpd
systemctl start httpd(※上がっていなければ不要)
systemctl status httpd(※上がっていなければ不要)
online_shellexec_poling.sh &
online_shellexec_extractpoling.sh &

アクセスログとキューの確認

こんな感じのログが出力され、キューの出力先にファイルが作成されていればOK

[root@homeserver001 homeserver_execbatchscript]# tail /var/log/httpd/access_log* | grep test | tail -1
162.168.0.231 - - [14/Aug/2022:19:59:00 +0900] "GET /homeserver_execbatchscript/jobexec.html HTTP/1.1" 304 - "https://homeserver/homeserver_execbatchscript/dummybashscript/dummy_javascripttest_access_curl" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0"
[root@homeserver001 homeserver_execbatchscript]#
[root@homeserver001 homeserver001_execbatchscript]# ls -lrt dummybashscript/scriptqueue/*
-rw-r--r--. 1 root root 0  8月 14 18:57 dummybashscript/scriptqueue/testhoge_access_curl
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0