4
3

More than 5 years have passed since last update.

PiBakeryの固定IPアドレス設定ブロックを自作する

Posted at

Raspberry Pi に固定IPアドレスを振りたい

.NetCoreをRaspberry Pi上のdockerでパッと動かすPiBakeryレシピRun Commandを使って固定IPアドレスをRaspberry Piに振った。

  • 固定IPはRaspberryPiをセットアップする時に簡単に指定できた方がいい
  • Run Commandはちょっと「生」すぎる

ということで、Contributing Blocks to PiBakeryに従って固定IPアドレスを振るためのブロック[staticip_setup]を作成してみた。作ったものはこちら。

image.png

手順

1. シェルスクリプトを作成する

Raspberry Piに固定IPアドレスを振るためのスクリプトを作成する。ブロックから設定したい項目は引数にしておく。

staticip_setup.sh
# interface名(wi-fiならwlan0、LANケーブルならeth0)
echo "interface $1" >> /etc/dhcpcd.conf
# IPアドレスの設定
echo "  static ip_address=$2" >> /etc/dhcpcd.conf
# デフォルトゲートウェイの設定
echo "  static routers=$3" >> /etc/dhcpcd.conf
# DNSサーバーの設定
echo "  static domain_name_servers=$4" >> /etc/dhcpcd.conf

2. ブロックを定義するjsonファイルを作成する

staticip_setup.jsonファイルを作る。プロパティについてはContributing Blocks to PiBakeryを参考に。

nameプロパティはjsonファイル名と同じにしておく。

"name": "staticip_setup",

textプロパティにはブロックに表示したいテキストを設定。%dで引数を埋め込む

"text": "Setup Static IP\\nType: %1\\nIP address with subnet mask: %2\\nDefault gate way: %3\\nDNS server: %4",

scriptプロパティは先程作ったスクリプトを指定

"script": "staticip_setup.sh"

スクリプトで$1-$4の引数を必要としているので、argsにはそれぞれに対応する4つのオブジェクトを用意する。

"args": [
    {
        "type": "menu",
        "options": ["eth0", "wlan0"]
    },
    {
        "type": "text",
        "default": "192.168.1.1/24",
        "maxLength":18
    },
    {
        "type": "text",
        "default": "192.168.1.1",
        "maxLength":15
    },
    {
        "type": "text",
        "default": "192.168.1.1",
        "maxLength":15
    }
]

残りのプロパティはこんな感じに。この設定自体にはnetworkへの接続は不要なのでnetwork:falseで良い。

"network": false,
"continue": true,
"type": "network",
"category":"network",
"supportedOperatingSystems": [
    "raspbian-pibakery.img",
    "raspbian-lite-pibakery.img"
],
"shortDescription":"Set static IP address to ethernet(eth0) or wifi(wlan0).",
"longDescription":"Set static IP address to ethernet(eth0) or wifi(wlan0)."

3. PiBakery下に配置し、info.jsonを追記

  1. PiBakery本体下のpibakery-blocksディレクトリ(C:\Program Files (x86)\PiBakery\resources\app\pibakery-blocksなど)にjsonファイル名と同じ名称のフォルダstaticip_setupを作成
  2. 作成した2つのファイルを上記フォルダ下にコピー
  3. pibakery-blocks/info.jsonloadOrderプロパティの要素にstaticip_setupを追記

以上で完成。PiBakeryを開いたらNetworkカテゴリに今回作成したstaticip_setupが出てくるようになる。

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