LoginSignup
5
3

More than 5 years have passed since last update.

[メモ] らずぱい WebUIでシャットダウン (セキュリティ考慮無し)

Posted at

概要

  • shutdown とか rebootって、タイプするのが、ダるい。
    => ボタンおすと、再起動とか、シャットダウン
    => Webサーバ立てて、php で、system関数呼べば...
  • セキュリティは考慮してないので、外部から接続されないように、おうちのLANだけとか、ラズパイをWifi-APにしてしまうとか、しましょう。
  • 参考: Basic way to shutdown via web

環境

  • Raspberry Pi 2 / 3
  • Raspbian: 2017-04-10-raspbian-jessie
  • lighttpdを使用
  • 知らないヒトが入ってこない、ネットワーク環境.

手順

  1. いつもの

    • Raspbianを焼いて
    • 適当に初期設定
    • パッケージ更新:
      sudo apt-get update && sudo apt-get upgrade -y
  2. Webサーバ(lighttpd)の導入、phpの有効化

    # パッケージ取得
    sudo apt-get update && sudo apt-get install lighttpd php5-cgi
    # モジュールを有効化
    sudo lighty-enable-mod fastcgi fastcgi-php
    # サービス再起動
    sudo systemctl restart lighttpd.service
    
    • /var/www/html が htmlのルートディレクトリ
    • ブラウザから、http://<らずぱいのIPアドレス>/にあくせすしてみよう
  3. sudores の設定

    シェルにこぴぺ
    # www-dataユーザが、sudo shutdownを呼べるようにする.
    # ファイル:/etc/sudoers.d/www-data_shutdown
    echo 'www-data ALL= NOPASSWD: /sbin/shutdown' | sudo tee /etc/sudoers.d/www-data_shutdown
    
  4. html,phpファイルの準備

    シェルにこぴぺ
    # ファイル:/var/www/html/shutdown.php
    cat <<EOF | sudo tee /var/www/html/shutdown.php
    <script>setTimeout(function(){ history.go(-1); }, 1000);</script>
    <?php system('sudo /sbin/shutdown -h now > /dev/null &'); ?>
    Shutting down
    EOF
    #
    # ファイル: /var/www/html/reboot.php
    cat <<EOF | sudo tee /var/www/html/reboot.php
    <script>setTimeout(function(){ history.go(-1); }, 1000);</script>
    <?php system('sudo /sbin/shutdown -r now > /dev/null &'); ?>
    Rebooting
    EOF
    #
    # ファイル: /var/www/html/index.html
    cat <<EOF | sudo tee /var/www/html/index.html
    <html>
    <form action="reboot.php ">    <input type="submit" value="Reboot" style="height: 240px; width: 50% ;font-size: 50px;" /></form>
    <br><br><br><br><br><br>
    <form action="shutdown.php ">    <input type="submit" value="Shutdown" style="height: 240px; width: 50% ;font-size: 50px;" /></form>
    </html>
    EOF
    
    • /var/www/html/ 以下に

      • ボタンを表示。押すと、phpのファイルへジャンプ

        index.html
        <html>
        <form action="reboot.php ">    <input type="submit" value="Reboot" style="height: 240px; width: 50% ;font-size: 50px;" /></form>
        <br><br><br><br><br><br>
        <form action="shutdown.php ">    <input type="submit" value="Shutdown" style="height: 240px; width: 50% ;font-size: 50px;" /></form>
        </html>
        
      • シャットダウン

        shutdown.php
        <script>setTimeout(function(){ history.go(-1); }, 1000);</script>
        <?php system('sudo /sbin/shutdown -h now > /dev/null &'); ?>
        Shutting down
        
      • 再起動

        reboot.php
        <script>setTimeout(function(){ history.go(-1); }, 1000);</script>
        <?php system('sudo /sbin/shutdown -r now > /dev/null &'); ?>
        Rebooting
        
  5. ブラウザから、http://<らずぱいのIPアドレス>/にアクセス。
    ナイスなUIデザインがほしい
    デザインがいまいちだと、使う気が...(ぉ

5
3
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
5
3