对于一些私有化部署的系统,部署完毕后经常需要对服务器网络进行配置,对于非技术人员来说,可能存在一定难度,因此将网络配置界面化可以大大提升实施效率。那么实现这个需求最关键的一步便是能够通过后端代码配置服务器的网络环境,本文讲述了如何编写shell脚本去实现网络配置以及如何使用Java代码去远程调用shell脚本。
auto 网卡名称
iface 网卡名称 inet static
address ip地址
netmask 子网掩码
gateway 路由地址
dns-nameservers 首选dns 备用dns
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
vim set_network.sh
#!/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
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 "auto $name" "$network_file" | cut -d: -f1
echo "auto $name
iface $name inet static address
address $ip
netmask $netmask
gateway $gateway
dns-nameservers $dns $back_dns" >> $network_file
#变量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
#查看当前连接
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 <配置名称>
<dependency>
<groupId>org.apache.sshdgroupId>
<artifactId>sshd-coreartifactId>
<version>2.9.3version>
dependency>
SshClient sshClient = SshClient.setUpDefaultClient();
sshClient.start();
ConnectFuture connectFuture = sshClient.connect("用户名", "服务ip", 服务端口);
ClientSession session = connectFuture.getClientSession();
session.addPasswordIdentity("密码");
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");
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();
}
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");
如有问题请大家指正,或者有更好的实现方案,欢迎研讨。