在不同的脚本/终端中使用 wlan0 或 eth0



我有一个使用 eth0 和 wlan0 的 debian 安装。 对于所有我想使用 eth0 的应用程序,除了我想使用 wlan0 的一个脚本。

是否有可能强制例如在终端会话或例如 shell 脚本中为此特定脚本使用 wlan0?

非常感谢您的帮助

是的,默认情况下使用eth0非常容易,然后如果您需要使用wlan0进行一次运行,只需将wlan0作为命令行参数传递即可。您还应该检查您使用的值是eth0还是wlan0,或者您应该认为参数无效。

实现该逻辑的简短脚本是:

#!/bin/sh
iface=${1:-eth0}  ## use eth0 by default or use the first argument
## if the iface entered is not eth0 or wlan0, handle error
if [ "$iface" != "eth0" ] && [ "$iface" != "wlan0" ]
then
printf "error: invalid interface '%s'n" "$iface"
exit 1
fi
printf "using: %sn" "$iface"  ## output interface being used

示例使用/输出

$ sh useiface.sh
using: eth0
$ sh useiface.sh wlan0
using: wlan0
$ sh useiface.sh eth1
error: invalid interface 'eth1'

当用户尝试使用被视为无效的eth1调用脚本并且脚本提供错误并退出时,您可以在上面看到。您可以根据需要进行调整。

最新更新