Java永久性修改Linux(Debian/Ubuntu)网络

文章目录

  • 前言
  • 一、实现思路
  • 二、手动配置网络
    • (一)编辑/etc/network/interfaces 文件
    • (二)重启网络
    • (三)查看网络配置
  • 三、shell脚本配置网络
    • (一)脚本执行流程
    • (二)编写脚本
    • (三)完整代码
    • (四)脚本执行方式
    • (五)直接覆盖版
    • (六)使用工具包的实现方案
  • 四、通过Java调用shell脚本
    • (一)SSH远程连接调用
    • (二)使用本地指令调用的方案
  • 总结


前言

对于一些私有化部署的系统,部署完毕后经常需要对服务器网络进行配置,对于非技术人员来说,可能存在一定难度,因此将网络配置界面化可以大大提升实施效率。那么实现这个需求最关键的一步便是能够通过后端代码配置服务器的网络环境,本文讲述了如何编写shell脚本去实现网络配置以及如何使用Java代码去远程调用shell脚本。


一、实现思路

  1. 采用/etc/network/interfaces文件配置网络。
  2. 通过字符串的追加或替换修改配置文件。
  3. 通过shell脚本实现网络配置过程。
  4. Java程序通过调用shell脚本实现网络配置。

二、手动配置网络

(一)编辑/etc/network/interfaces 文件

  1. 配置格式。
auto 网卡名称
iface 网卡名称 inet static
    address ip地址
    netmask 子网掩码
    gateway 路由地址
    dns-nameservers 首选dns 备用dns
  1. 实际效果,多个网卡直接在下方添加即可。
auto eth0
iface eth0 inet static
    address 192.168.30.133
    netmask 255.255.255.0
    gateway 192.168.30.1
    dns-nameservers 8.8.8.8 8.8.4.4
auto eth1
iface eth1 inet static
    address 192.168.2.5
    netmask 255.255.255.0
    gateway 192.168.2.1
    dns-nameservers 8.8.8.8 8.8.4.4

(二)重启网络

systemctl restart networking

(三)查看网络配置

ifconfig

三、shell脚本配置网络

(一)脚本执行流程

  1. 网络配置数据以脚本参数形式输入,参数格式为:网卡名称,ip地址,子网掩码,路由地址。
  2. 判断/etc/network/interfaces是否存在,不存在自动生成。
  3. 遍历所有脚本参数。
  4. 以英文逗号(,)为分割符分割脚本参数。
  5. 判断是否已经配置过该网卡数据,存在更新否则追加。
  6. 重启网络。

(二)编写脚本

  1. 创建并编辑.sh文件,脚本名称为set_network(路径以及脚本名称可根据实际情况修改)。
vim set_network.sh
  1. shell脚本标准开头。
#!/bin/sh
  1. 脚本执行过程需要多次使用 /etc/network/interfaces路径,因此将其设置为变量放置在开头。
network_file=/etc/network/interfaces
  1. 判断配置文件是否存在,不存在自动创建。
if [ -e $network_file ]; then
    :
else
    echo $network_file"不存在,自动创建"
    touch $network_file
    #这一句是默认的
    echo "source /etc/network/interfaces.d/*" >> $network_file
fi
  1. 遍历脚本参数,依次进行参数分割、网卡设置是否存在的判断及处理。
  • 使用"<被分割字符串>" | cut -d ‘<分割符号>’ -f <序号>分割脚本参数。
name=$(echo "$arg" | cut -d ',' -f 1)
ip=$(echo "$arg" | cut -d ',' -f 2)
netmask=$(echo "$arg" | cut -d ',' -f 3)
gateway=$(echo "$arg" | cut -d ',' -f 4)
dns=$(echo "$arg" | cut -d ',' -f 5)
back_dns=$(echo "$arg" | cut -d ',' -f 6)
  • 使用grep -n “<匹配字符串>” "<原文路径>"找出网卡名称所在行, 使用cut -d: -f1截取行数具体数值,如果有值,代表配置存在。
grep -n "auto $name" "$network_file" | cut -d: -f1
  • 如果不存在配置,使用echo “<追加内容>” >> 文件路径。
echo "auto $name
iface $name inet static address 
    address $ip
    netmask $netmask
    gateway $gateway
    dns-nameservers $dns $back_dns" >> $network_file
  • 如果存在配置,使用sed -i “<行数>s/.*/<替换内容>/” 文件路径替换指定行内容。
#变量row是"auto 网卡名称"字符串所在行的行数值,所以row+1行不变,ip地址在row+2行,
#子网掩码在row+3行,路由地址在row+4行,dns在row+5行
sed -i "$((row + 2))s/.*/    address ${ip}/" $network_file
sed -i "$((row + 3))s/.*/    netmask ${netmask}/" $network_file
sed -i "$((row + 4))s/.*/    gateway ${gateway}/" $network_file
sed -i "$((row + 5))s/.*/    dns-nameservers ${dns} ${back_dns}/" $network_file
  • 遍历参数的完整代码。
#分割参数,写入配置
for arg in "$@"; do
    name=$(echo "$arg" | cut -d ',' -f 1)
    ip=$(echo "$arg" | cut -d ',' -f 2)
    netmask=$(echo "$arg" | cut -d ',' -f 3)
    gateway=$(echo "$arg" | cut -d ',' -f 4)
    row=$(grep -n "auto $name" "$network_file" | cut -d: -f1)
    if [ -z "$row" ]; then
    	echo "auto $name
iface $name inet static address 
    address $ip
    netmask $netmask
    gateway $gateway" >> $network_file
    else
    	echo "值存在,更新"
        sed -i "$((row + 2))s/.*/    address ${ip}/" $network_file
        sed -i "$((row + 3))s/.*/    netmask ${netmask}/" $network_file
        sed -i "$((row + 4))s/.*/    gateway ${gateway}/" $network_file
        sed -i "$((row + 5))s/.*/    dns-nameservers ${dns} ${back_dns}/" $network_file
    fi
done
  • 重启网络。
systemctl restart networking

(三)完整代码

#!/bin/sh
network_file=/etc/network/interfaces
if [ -e $network_file ]; then
    :
else
    echo $network_file"不存在,自动创建"
    touch $network_file
    #默认的
    echo "source /etc/network/interfaces.d/*" >> $network_file
fi
#分割参数,写入配置
for arg in "$@"; do
    name=$(echo "$arg" | cut -d ',' -f 1)
    ip=$(echo "$arg" | cut -d ',' -f 2)
    netmask=$(echo "$arg" | cut -d ',' -f 3)
    gateway=$(echo "$arg" | cut -d ',' -f 4)
    dns=$(echo "$arg" | cut -d ',' -f 5)
    back_dns=$(echo "$arg" | cut -d ',' -f 6)
    row=$(grep -n "auto $name" "$network_file" | cut -d: -f1)
    if [ -z "$row" ]; then
    	echo "auto $name
iface $name inet static address 
    address $ip
    netmask $netmask
    gateway $gateway
    dns-nameservers $dns $back_dns" >> $network_file
    else
    	echo "值存在,更新"
        sed -i "$((row + 2))s/.*/    address ${ip}/" $network_file
        sed -i "$((row + 3))s/.*/    netmask ${netmask}/" $network_file
        sed -i "$((row + 4))s/.*/    gateway ${gateway}/" $network_file
        sed -i "$((row + 5))s/.*/    dns-nameservers ${dns} ${back_dns}/" $network_file
    fi
done
#重启网络
systemctl restart networking

(四)脚本执行方式

./set_network.sh eth0,192.168.30.134,255.255.255.0,192.168.30.1,8.8.8.8,8.8.4.4

(五)直接覆盖版

  • 这种方式不会对已存在的配置进行修改,而是直接清空原文后进行追加。
#!/bin/sh
network_file=/etc/network/interfaces
if [ -e $network_file ]; then
    :
else
    echo $network_file"不存在,自动创建"
    touch $network_file
fi
#清空配置文件
cat /dev/null > $network_file
#默认的
echo "source /etc/network/interfaces.d/*" >> $network_file
#分割参数,写入配置
for arg in "$@"; do
    name=$(echo "$arg" | cut -d ',' -f 1)
    ip=$(echo "$arg" | cut -d ',' -f 2)
    netmask=$(echo "$arg" | cut -d ',' -f 3)
    gateway=$(echo "$arg" | cut -d ',' -f 4)
    dns=$(echo "$arg" | cut -d ',' -f 5)
    back_dns=$(echo "$arg" | cut -d ',' -f 6)
    echo "auto $name
iface $name inet static address 
    address $ip
    netmask $netmask
    gateway $gateway
    dns-nameservers $dns $back_dns" >> $network_file
done
#重启网络
systemctl restart networking

(六)使用工具包的实现方案

  1. NetworkManager。
  • NetworkManager 是 Linux 系统中的一个工具,用于简化网络配置和管理。它支持多种网络连接(如以太网、Wi-Fi、移动宽带等),并提供图形用户界面和命令行接口来管理网络设置。
  • 如果基于NetworkManager修改网络,那么实现网络配置更改会更加简单,不需要像本文方案一样,在Shell脚本中对/etc/network/interfaces 文件进行繁杂的修改,而只是通过简单的nmcli指令即可实现功能。
  • NetworkManager工具的常用指令。
#查看当前连接
nmcli connection show
# 设置 IPv4 地址
nmcli connection modify <配置名称> ipv4.addresses /<子网掩码有效位数>
# 设置网关
nmcli connection modify <配置名称> ipv4.gateway <网关地址>
# 设置 DNS
nmcli connection modify <配置名称> ipv4.dns 
# 设置方法为手动
nmcli connection modify <配置名称> ipv4.method manual
# 设置方法为自动获取(DHCP)
nmcli connection modify <配置名称> ipv4.method auto
# 关闭网络配置
nmcli connection down <配置名称>
# 开启网络配置
nmcli connection up <配置名称>

四、通过Java调用shell脚本

(一)SSH远程连接调用

  1. 导入sshd-core工具包。
  • maven依赖坐标。
<dependency>
  <groupId>org.apache.sshdgroupId>
  <artifactId>sshd-coreartifactId>
  <version>2.9.3version>
dependency>
  1. 连接服务,服务端需支持SSH连接。
SshClient sshClient = SshClient.setUpDefaultClient();
sshClient.start();
ConnectFuture connectFuture = sshClient.connect("用户名", "服务ip", 服务端口);
ClientSession session = connectFuture.getClientSession();
session.addPasswordIdentity("密码");
  1. 发送命令,调用网络配置脚本。
session.executeRemoteCommand("./set_network.sh eth0,192.168.30.188,255.255.255.0,192.168.30.1,8.8.8.8,8.8.4.4");
  1. 完整代码。
SshClient sshClient = SshClient.setUpDefaultClient();
try {
    sshClient.start();
    ConnectFuture connectFuture = sshClient.connect("root", "192.168.2.5", 22).verify(5, TimeUnit.SECONDS);
    if (connectFuture.isConnected()) {
        ClientSession session = connectFuture.getClientSession();
        session.addPasswordIdentity("*****");
        AuthFuture verify = session.auth().verify(5, TimeUnit.SECONDS);
        if (verify.isSuccess()) {
            session.executeRemoteCommand("./set_network.sh eth0,192.168.30.188,255.255.255.0,192.168.30.1,8.8.8.8,8.8.4.4");
        }
    }
} finally {
    sshClient.stop();
}

(二)使用本地指令调用的方案

  1. 当Java程序被部署到服务器本地的时候,可以采用调用本地指令的方案实现网络配置,但这种方式不方便调试。
  2. 调用方式。
Process process = Runtime.getRuntime().exec("./set_network.sh eth0,192.168.30.188,255.255.255.0,192.168.30.1,8.8.8.8,8.8.4.4");

总结

如有问题请大家指正,或者有更好的实现方案,欢迎研讨。

你可能感兴趣的:(linux,java,debian,ubuntu,网络)