Bash scriptをhttp get経由で起動

ssh経由よりは簡単だけど……。

のIndy版です。ということはIOSでなくPCからの操作ということになります。上記の記事のように、sshクライアントからリモートでスクリプトを実行してもいいのですが、Indyだけでは困難なことがわかりました。のでサーバ側で工夫します。サーバ側で、

pi@raspberrypi:/var/www/html $ ls -alt
合計 92
drwxr-xr-x 5 root root  4096  1月 11 09:01 .
-rw-r--r-- 1 root root   148  1月 11 09:01 tvip.php
-rw-r--r-- 1 root root   149  6月 22  2023 nfoff.php
-rw-r--r-- 1 root root   147  6月 22  2023 nf.php
-rw-r--r-- 1 root root   139 10月 18  2022 ps5.php
-rw-r--r-- 1 root root   159 10月 18  2022 .htaccess
-rw-r--r-- 1 root root   164 10月 18  2022 hdmi.php
-rw-r--r-- 1 root root  1000 10月 17  2022 index.php
-rw-r--r-- 1 root root   977 10月 17  2022 index.php.before.ps5
-rw-r--r-- 1 root root   825  5月 14  2022 index.php.2022.5.14
drwxr-xr-x 8 root root  4096 10月 22  2021 .git
-rw-r--r-- 1 root root   616 10月 22  2021 index.old
-rw-r--r-- 1 root root   576 10月  3  2021 index.php.old.2021.10.3
-rw-r--r-- 1 root root   576  4月 13  2021 index.php.old
-rw-r--r-- 1 root root   604 11月 15  2020 index.php.get
-rw-r--r-- 1 root root   511 11月 14  2020 index.html.old
drwxr-xr-x 2 root root  4096 11月 14  2020 bu
-rw-r--r-- 1 root root   451 11月 14  2020 ir.php
drwxr-xr-x 2 root root  4096 11月 14  2020 postest
-rw-r--r-- 1 root root 10701 11月 13  2020 index.html.debian
drwxr-xr-x 3 root root  4096 11月 13  2020 ..
pi@raspberrypi:/var/www/html $ cat ps5.php
<?php

if(isset($_GET["ps5"])) {
        echo "extra parms";
//  system("/home/pi/ir/ps5");
} else {
  system("/home/pi/ir/ps5");
  echo "ps5";
}

となっていて、http://ip_of_raspberry_pi_machine/ps5というurlへアクセスするだけで、インタラクティブな操作なしで、つまりこのurlへのgetだけで/home/pi/ir/ps5を起動させることが可能です。これを少しだけ直して使います。

pi@raspberrypi:/var/www/html $ cat chrome.php
<?php

if(isset($_GET["ps5"])) {
        echo "extra parms";
//  system("/home/pi/ir/ps5");
} else {
  system("/home/pi/ir/chrome");
//  echo "ps5";
}

さてブラウザからアクセスして試してみましょう。

うまくいきました。Indyから実行するのは何度か出てきた、TIdHTTPを使えば良いですね。

ヘッダーに以下を足して、

#include <IdBaseComponent.hpp>
#include <IdComponent.hpp>
#include <IdHTTP.hpp>
#include <IdTCPClient.hpp>
#include <IdTCPConnection.hpp>

エラーチェックやリターンコードを省いた素のプログラム全体は、

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

void activate_chrome(void)
{
	String url = "http://raspberrypizero_local_ip/chrome";

	TIdHTTP* http = new TIdHTTP();

	http->Get(url);

	delete http;

}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
	activate_chrome();

}

これだけですね。LAN内部の話なので、エラーが出るかは微妙ですが、一応エラーチェックし、サーバのリターンコードOK(200)を確認するのだとactivate_chrome()を、

void activate_chrome(void)
{
	String url = "http://raspberrypizero_local_ip/chrome";

	try {

		TIdHTTP* http = new TIdHTTP();

		http->Get(url);
		Form1->Label1->Caption = http->ResponseCode;
		delete http;

	} catch (const Exception &e) {

		ShowMessage(e.Message);
	}



}

実行結果は、

404とかのエラーが出ると、

とIDE内部でトラップ出来てますね。

コメント