さて、以前ホスト名を引数にした半自動IPアドレス設定バッチを作成したが、今回は更に自動化出来ないかとやってみた。 内容としては、上記バッチファイルをベースにして、ここ最近記述しているシリアルナンバーを用いた設定スクリプトの内容を落とし込んだものだ。

まずは、もはやお馴染みの外部ファイル「host.list」から。 Linuxとの使い回しを考えて、フォーマットは全く同じものを使用している。

●host.list

hostname,serialno,defaultgw,nic0,nic1
Test-UbuntuOS001,564D33C9F632AB40148800E9CA90D059,192.168.0.1,192.168.0.31/24,172.26.0.31/24
Test-UbuntuOS002,564DAB560AE59CD285EDCFE97F5A9E64,192.168.0.1,192.168.0.32/24,172.26.0.32/24
Test-UbuntuOS003,564DF163D810265B56707D081C45716F,192.168.0.1,192.168.0.33/24,172.26.0.33/24
Test-UbuntuOS004,564DE8B12B9FFE98E364AA9375CDB04A,192.168.0.1,192.168.0.34/24,172.26.0.34/24
Test-Windows0001,564DACFAF08362E9A4B71FE53135349A,192.168.0.1,192.168.0.35/24,172.26.0.35/24

次にバッチファイル本体。 Windowsの場合、BIOSのシリアルナンバーを整形して保持するため、ちょっと手を入れないといけないのが面倒。

●setip.sh

@echo off

rem === set value start ==============================================
  set NIC0="ローカル エリア接続"
  set NIC1="ローカル エリア接続 2"
  set WORK01=Work01.txt
  set WORK02=Work02.txt
  set WORK03=Work03.txt
rem === set value end ================================================

rem === initial value start ==========================================
  set GATEWAY=
  set PREFIX=

  set NIC0ADDR=
  set NIC0MASK=

  set NIC1ADDR=
  set NIC1MASK=
rem === initial value end ============================================

rem === Check Serial Number Start ====================================
  FOR /F "tokens=2 usebackq delims=^:" %%i in (`wmic csproduct get uuid ^| findstr /n /r "." ^| findstr /r "^2:"`) DO @set UUID_ORG=%%i
  set UUID_F=%UUID_ORG:-=%
  echo %UUID_F%

  set UUID_4=%UUID_F:~0,2%
  set UUID_3=%UUID_F:~2,2%
  set UUID_2=%UUID_F:~4,2%
  set UUID_1=%UUID_F:~6,2%

  set UUID_6=%UUID_F:~8,2%
  set UUID_5=%UUID_F:~10,2%
  set UUID_8=%UUID_F:~12,2%
  set UUID_7=%UUID_F:~14,2%

  set UUID_9=%UUID_F:~16,17%
  set UUID=%UUID_1%%UUID_2%%UUID_3%%UUID_4%%UUID_5%%UUID_6%%UUID_7%%UUID_8%%UUID_9: =%
rem === Check Serial Number End ======================================

rem === set Host Name And IP Addr Start ==============================
  type host.list | find "%UUID%" > %WORK01%
  type %WORK01% | find /v /c "" > %WORK02%
  for /f "tokens=1" %%i in (%WORK02%) do @SET NUM=%%i
  if %NUM% == 0 goto End

  rem === set Host Name Start ========================================
  for /f "tokens=1 delims=," %%i in (%WORK01%) do @SET HOSTN=%%i
  wmic ComputerSystem WHERE name="%computername%" Call Rename name="%HOSTN%"
  rem === set Host Name End ==========================================

  rem === set Gateway Start ==========================================
  for /f "tokens=3 delims=," %%i in (%WORK01%) do @SET GATEWAY=%%i
  rem === set Gateway End ============================================

  rem === set NIC0 IP Addr Start =====================================
  for /f "tokens=4 delims=," %%i in (%WORK01%) do echo %%i > %WORK02%
  for /f "tokens=1 delims=/" %%i in (%WORK02%) do @SET NIC0ADDR=%%i
  set PREFIX=
  for /f "tokens=2 delims=/" %%i in (%WORK02%) do @SET PREFIX=%%i
  echo %PREFIX%
  set NETMASK=
  Call :GetSubnetMask %PREFIX%
  echo %NETMASK%

  echo netsh interface ipv4 set address %NIC0% static %NIC0ADDR% %NETMASK% %GATEWAY%
  netsh interface ipv4 set address %NIC0% static %NIC0ADDR% %NETMASK% %GATEWAY%
  rem === set NIC0 IP Addr End =======================================

  rem === set NIC1 IP Addr Start =====================================
  for /f "tokens=5 delims=," %%i in (%WORK01%) do echo %%i > %WORK02%
  for /f "tokens=1 delims=/" %%i in (%WORK02%) do @SET NIC1ADDR=%%i
  set PREFIX=
  for /f "tokens=2 delims=/" %%i in (%WORK02%) do @SET PREFIX=%%i
  echo %PREFIX%
  set NETMASK=
  Call :GetSubnetMask %PREFIX%
  echo %NETMASK%

  echo netsh interface ipv4 set address %NIC1% static %NIC1ADDR% %NETMASK%
  netsh interface ipv4 set address %NIC1% static %NIC1ADDR% %NETMASK%
  rem === set NIC1 IP Addr End =======================================

rem === set Host Name And IP Addr End ================================

goto End

rem ==================================================================
:GetSubnetMask
    if /i %PREFIX% == 1 @SET NETMASK=128.0.0.0
    if /i %PREFIX% == 2 @SET NETMASK=192.0.0.0
    if /i %PREFIX% == 3 @SET NETMASK=224.0.0.0
    if /i %PREFIX% == 4 @SET NETMASK=240.0.0.0
    if /i %PREFIX% == 5 @SET NETMASK=248.0.0.0
    if /i %PREFIX% == 6 @SET NETMASK=252.0.0.0
    if /i %PREFIX% == 7 @SET NETMASK=254.0.0.0
    if /i %PREFIX% == 8 @SET NETMASK=255.0.0.0
    if /i %PREFIX% == 9 @SET NETMASK=255.128.0.0
    if /i %PREFIX% == 10 @SET NETMASK=255.192.0.0
    if /i %PREFIX% == 11 @SET NETMASK=255.224.0.0
    if /i %PREFIX% == 12 @SET NETMASK=255.240.0.0
    if /i %PREFIX% == 13 @SET NETMASK=255.248.0.0
    if /i %PREFIX% == 14 @SET NETMASK=255.252.0.0
    if /i %PREFIX% == 15 @SET NETMASK=255.254.0.0
    if /i %PREFIX% == 16 @SET NETMASK=255.255.0.0
    if /i %PREFIX% == 17 @SET NETMASK=255.255.128.0
    if /i %PREFIX% == 18 @SET NETMASK=255.255.192.0
    if /i %PREFIX% == 19 @SET NETMASK=255.255.224.0
    if /i %PREFIX% == 20 @SET NETMASK=255.255.240.0
    if /i %PREFIX% == 21 @SET NETMASK=255.255.248.0
    if /i %PREFIX% == 22 @SET NETMASK=255.255.252.0
    if /i %PREFIX% == 23 @SET NETMASK=255.255.254.0
    if /i %PREFIX% == 24 @SET NETMASK=255.255.255.0
    if /i %PREFIX% == 25 @SET NETMASK=255.255.255.128
    if /i %PREFIX% == 26 @SET NETMASK=255.255.255.192
    if /i %PREFIX% == 27 @SET NETMASK=255.255.255.224
    if /i %PREFIX% == 28 @SET NETMASK=255.255.255.240
    if /i %PREFIX% == 29 @SET NETMASK=255.255.255.248
    if /i %PREFIX% == 30 @SET NETMASK=255.255.255.252
    if /i %PREFIX% == 31 @SET NETMASK=255.255.255.254
    if /i %PREFIX% == 32 @SET NETMASK=255.255.255.255

    exit /b 0

:End
    echo "Please Reboot Computer"
rem ==================================================================

これで、シリアルナンバーを元に各筐体に個別の静的IPアドレスの設定が可能になる。 sysprepと組み合わせて使うと、クローンの構築が結構楽になるのではなかろうか。