0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ログインユーザの入力コマンド自動表示

Last updated at Posted at 2024-12-23

はじめに

リモート研修(teams使用)の講師をやっています。複数のUnix未経験者のBash入力コマンドをチェックするため自動で表示確認するものを作成しました。

仕様

  1. 生徒の.bashrcのPROMPT_COMMAN変数で生徒の.bash_historyを専用のファイルmmshistへコピーする
  2. .bashrcと空の.bash_historyは生徒のアカウントを作るときに組み込む
  3. ブラウザアプリでpasswdファイルから該当生徒の専用ファイルmmshistを定期的に読み、コマンドの最新部分から数行分表示する

Bashrcの組み込み部分

bashrc
 1 shopt -s histappend
 2 PROMPT_COMMAND="history -n; history -a; cp .bash_history ${HOME}/mmshist; chmod 666 ${HOME}/mmshist"
 3 unset HISTFILESIZE
 4 HISTSIZE=500

Webアプリ

commandviewer.php
 1 <?php
 2 $loginkey='student';   /// 表示したいログインID部分 
 3 print "<html><head";
 4 print '<meta http-equiv="Refresh" content="4" >';
 5 print '</head>';
 6 print "<body>";
 7 print "<h2><font color='brown'>Command viewer</h2>";
 8 print "<h4><font color='brown'>Refresh 4 seconds<br>Latest at the bottom<br>{$loginkey}</font></h2>";
 9 $fp=fopen("/etc/passwd","r");
10 $num=0;
11 while ($line = fgets($fp)) {
12   $larr=explode(':',$line);
13   $passname=substr($larr,0,strlen($loginkey));
14   if ($passname == $loginkey) {
15     $num++;
16     $output=null;
17     $rtnval=null;
18     print "<table border='1'>";
19     print "<tr><td><b>Login account: ".$logname."</b></td></tr>";
20     print "</table>";
21     $output=null;
22     $rtnval=null;
23     exec("tail -3 /home/".$logname."/mmshist",$output,$rtnval);
24     foreach ($output as $cmditem){
25       print "$ <font color='brown'><b>".${cmditem}."</b></font><br>";
26     }
27     print "----------------------------------------<br>";
  }
28 }
29 print "</body>";
30 print "</html>";
31 ?>

アプリ説明
* $loginkeyへ表示するログインIDを設定する
* 表示ユーザと同じ長さのログインIDを/etc/passwdファイルから探す
* Refreshタイムは<metaで指定、表示コマンド行はtailコマンドを実行する
* プログラムcommandviewer.phpをWebサーバのドキュメントルートに置き、URL localhost/commandviewer.phpで動作させる

表示サンプル

image-3.png

留意事項

  • .bash_historyが存在しないと最初のログインで警告が出るので空の.bash_historyを作成しておきます。
  • Webアプリからアクセスするディレクトリ、ファイルのパーミッションを考慮します。

おわりに

Teamsの共有機能で生徒のunixターミナルを監視していましたが、複数人を切り替えながら行う効率の悪さからこの方式に切り替えました。生徒のエディタで作成した中身のチェックもアプリに手を加えれば出来ます。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?