如何通过命令修改IP地址

# 问题描述

经常需要切换两个本地ip,每次都是打开网络设置改太麻烦了,想要写个脚本一键切换。

# 核心命令

 networksetup -setmanual "Ethernet" 192.168.128.54 255.255.255.0 192.168.128.1

# 切换脚本

#!/bin/bash

ips=("192.168.130.167" "192.168.143.242")
gate="192.168.143.254"

next_ip=${ips[0]}
current_ip=(`ipconfig getifaddr en0`)

if [ "${current_ip}" == ${next_ip} ]; then next_ip=${ips[1]}; fi

networksetup -setmanual "Wi-Fi" ${next_ip} 255.255.255.0 ${gate}
echo "${current_ip} => ${next_ip}"

仓库地址:https://github.com/saltbo/devenv/blob/master/shell/ipsw.sh

# 参考内容

  • https://blog.csdn.net/u010953692/article/details/79752715