1
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?

More than 3 years have passed since last update.

macOSのコマンドラインからネットワークの設定を大体自動でやる

Last updated at Posted at 2021-04-11

macOSでコマンドラインからネットワークの設定をやりたかったのですが、コマンドが長かったので自動化しました。
vlanはvlan0を指定すると、vlanインターフェースを消せます。

ipの部分を算出できるように改良するかもしれないです。

change.vlan.sh
#!/usr/bin/bash

old_vlan=$(ifconfig | grep vlan: | cut -c 8- | sed 's/parent.*//')
if [ -n "$old_vlan" ]; then
  networksetup -deleteVLAN vlan0 en5 $old_vlan
fi
read -p "vlan ID:" vlan
if [ $vlan = 0 ]; then
    exit $status
fi
networksetup -createVLAN vlan0 en5 $vlan
change.ip.sh
#!/usr/bin/bash

read -p "plz select interface(LAN:0,Wi-Fi:1) : " int
if [ $int == 0 ]; then
    read -p "plz select add type(DHCP:0,Manual:1) : " add_type
    if [ $add_type == 0 ]; then
        networksetup -setdhcp "USB 10/100/1000 LAN"
    else
        read -p "plz insert address : " address
        read -p "plz insert subnet : " subnet
        read -p "plz insert gateway : " gateway
        if [ $subnet == 1 ]; then
            subnet=128.0.0.0
        elif [ $subnet == 2 ]; then
            subnet=192.0.0.0
        elif [ $subnet == 3 ]; then
            subnet=224.0.0.0
        elif [ $subnet == 3 ]; then
            subnet=240.0.0.0
        elif [ $subnet == 5 ]; then
            subnet=248.0.0.0
        elif [ $subnet == 6 ]; then
            subnet=252.0.0.0
        elif [ $subnet == 7 ]; then
            subnet=254.0.0.0
        elif [ $subnet == 8 ]; then
            subnet=255.0.0.0
        elif [ $subnet == 9 ]; then
            subnet=255.128.0.0
        elif [ $subnet == 10 ]; then
            subnet=255.192.0.0
        elif [ $subnet == 11 ]; then
            subnet=255.224.0.0
        elif [ $subnet == 12 ]; then
            subnet=255.240.0.0
        elif [ $subnet == 13 ]; then
            subnet=255.248.0.0
        elif [ $subnet == 14 ]; then
            subnet=255.252.0.0
        elif [ $subnet == 15 ]; then
            subnet=255.254.0.0
        elif [ $subnet == 16 ]; then
            subnet=255.255.0.0
        elif [ $subnet == 17 ]; then
            subnet=255.255.128.0
        elif [ $subnet == 18 ]; then
            subnet=255.255.192.0
        elif [ $subnet == 19 ]; then
            subnet=255.255.224.0
        elif [ $subnet == 20 ]; then
            subnet=255.255.240.0
        elif [ $subnet == 21 ]; then
            subnet=255.255.248.0
        elif [ $subnet == 22 ]; then
            subnet=255.255.252.0
        elif [ $subnet == 23 ]; then
            subnet=255.255.254.0
        elif [ $subnet == 24 ]; then
            subnet=255.255.255.0
        elif [ $subnet == 25 ]; then
            subnet=255.255.255.128
        elif [ $subnet == 26 ]; then
            subnet=255.255.255.192
        elif [ $subnet == 27 ]; then
            subnet=255.255.255.224
        elif [ $subnet == 28 ]; then
            subnet=255.255.255.240
        elif [ $subnet == 29 ]; then
            subnet=255.255.255.248
        elif [ $subnet == 30 ]; then
            subnet=255.255.255.252
        elif [ $subnet == 31 ]; then
            subnet=255.255.255.254
        elif [ $subnet == 32 ]; then
            subnet=255.255.255.255
        fi
        networksetup -setmanual "USB 10/100/1000 LAN" $address $subnet $gateway
    fi
elif [ $int == 1 ]; then
    read -p "plz select add type(DHCP:0,Manual:1) : " add_type
    if [ $add_type == 0 ]; then
        networksetup -setdhcp "Wi-Fi"
    else
        read -p "plz insert address : " address
        read -p "plz insert subnet : " subnet
        read -p "plz insert gateway : " gateway
        if [ $subnet == 1 ]; then
            subnet=128.0.0.0
        elif [ $subnet == 2 ]; then
            subnet=192.0.0.0
        elif [ $subnet == 3 ]; then
            subnet=224.0.0.0
        elif [ $subnet == 3 ]; then
            subnet=240.0.0.0
        elif [ $subnet == 5 ]; then
            subnet=248.0.0.0
        elif [ $subnet == 6 ]; then
            subnet=252.0.0.0
        elif [ $subnet == 7 ]; then
            subnet=254.0.0.0
        elif [ $subnet == 8 ]; then
            subnet=255.0.0.0
        elif [ $subnet == 9 ]; then
            subnet=255.128.0.0
        elif [ $subnet == 10 ]; then
            subnet=255.192.0.0
        elif [ $subnet == 11 ]; then
            subnet=255.224.0.0
        elif [ $subnet == 12 ]; then
            subnet=255.240.0.0
        elif [ $subnet == 13 ]; then
            subnet=255.248.0.0
        elif [ $subnet == 14 ]; then
            subnet=255.252.0.0
        elif [ $subnet == 15 ]; then
            subnet=255.254.0.0
        elif [ $subnet == 16 ]; then
            subnet=255.255.0.0
        elif [ $subnet == 17 ]; then
            subnet=255.255.128.0
        elif [ $subnet == 18 ]; then
            subnet=255.255.192.0
        elif [ $subnet == 19 ]; then
            subnet=255.255.224.0
        elif [ $subnet == 20 ]; then
            subnet=255.255.240.0
        elif [ $subnet == 21 ]; then
            subnet=255.255.248.0
        elif [ $subnet == 22 ]; then
            subnet=255.255.252.0
        elif [ $subnet == 23 ]; then
            subnet=255.255.254.0
        elif [ $subnet == 24 ]; then
            subnet=255.255.255.0
        elif [ $subnet == 25 ]; then
            subnet=255.255.255.128
        elif [ $subnet == 26 ]; then
            subnet=255.255.255.192
        elif [ $subnet == 27 ]; then
            subnet=255.255.255.224
        elif [ $subnet == 28 ]; then
            subnet=255.255.255.240
        elif [ $subnet == 29 ]; then
            subnet=255.255.255.248
        elif [ $subnet == 30 ]; then
            subnet=255.255.255.252
        elif [ $subnet == 31 ]; then
            subnet=255.255.255.254
        elif [ $subnet == 32 ]; then
            subnet=255.255.255.255
        fi
        networksetup -setmanual "Wi-Fi" $address $subnet $gateway
    fi
fi    

このほかにも色々書いたのでよければ見てください。
https://github.com/Hicky1025/mac.shell

1
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
1
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?