MAC下切换多个IP的Shell脚本

该脚本演示了如何在Shell中创建一个菜单,让用户选择不同的网络配置选项,如设置192.168.0.155或10.71.2.1的IP地址,显示网络接口列表,获取特定接口信息,以及退出脚本。用户输入选择后,脚本会根据选择执行相应的网络命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 参考:如何在shell脚本中使用goto语句 - VoidCC

效果:

 

 注意修改以下几处:

show you my code:

#!/usr/bin/env bash 
 
# Define the choices to present to the user, which will be 
# presented line by line, prefixed by a sequential number 
# (E.g., '1) copy', ...)
target_eth='USB 10/100/1G/2.5G LAN' 
choices=('ip for 192.168/24' 'ip for 10.71/24' 'show eth list' "get $target_eth info" 'exit')

# Present the choices. 
# The user chooses by entering the *number* before the desired choice. 

while [ "$menu" != 1 ]; do
printf "\nMain Menu:\n\n"
select choice in "${choices[@]}"; do 

    # If an invalid number was chosen, $choice will be empty. 
    # Report an error and prompt again. 
    [[ -n $choice ]] || { echo "Invalid choice." >&2; continue; } 

    # Examine the choice. 
    # Note that it is the choice string itself, not its number 
    # that is reported in $choice. 
    case $choice in 
    ip\ for\ 192.168/24) 
     echo "Swithing..."
     networksetup -setmanual "$target_eth" 192.168.0.155 255.255.255.0 192.168.0.1 
     # Set flag here, or call function, ... 
     echo "already changed. the new ip info is: " 
     networksetup getinfo "$target_eth"
     break
     ;;
    show\ eth\ list)
     networksetup -listnetworkserviceorder
     # Set flag here, or call function, ...
     break
     ;; 
    get\ $target_eth\ info)
     networksetup getinfo "$target_eth"
     break
     ;;
    ip\ for\ 10.71/24) 
     echo "Swithing..." 
     networksetup -setmanual "$target_eth" 10.71.2.155 255.255.255.0 10.71.2.1     
     echo "already changed. the new ip info is: "
     networksetup getinfo "$target_eth" 
     break
     ;;
    exit)
     echo "Exiting now.."
     menu=1
     break
     ;;
    *)
     echo "operation not avaliable!"
     break
     ;;    
    esac 

    # Getting here means that a valid choice was made, 
    # so break out of the select statement and continue below, 
    # if desired. 
    # Note that without an explicit break (or exit) statement, 
    # bash will continue to prompt. 
    #break 
    
done
done
exit 0 

Over.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值