Linux---常用shell脚本

目录

一.网络服务

开启network服务

网口IP配置

聚合口配置


前言

秋招拿到了科大讯飞的offer,可是由于某些原因无法完成三方签署,心情还是比较失落的,或许写一篇技术博客,活跃一下大脑思维也是一种不错的放松方式。


一.网络服务

开启network服务

此脚本用于关闭NetworkManger服务重启network防止服务冲突导致IP配置无法生效

bash 复制代码
#!/bin/bash

# 获取 NetworkManager 的 Active 状态(去除括号)
status=$(systemctl status NetworkManager | grep 'Active:' | awk '{print $3}' | tr -d '()')

# 检查状态是否不是 dead
if [ "$status" != "dead" ]; then
    echo "NetworkManager is not dead. Stopping NetworkManager and restarting network..."

    # 停止 NetworkManager
    systemctl stop NetworkManager

    # 检查 systemctl stop 的退出状态
    if [ $? -ne 0 ]; then
        echo "Failed to stop NetworkManager."
        exit 1
    fi

    # 重启 network 服务(注意:这个服务名可能因系统而异,例如在某些系统上可能是 'networking')
    systemctl restart network

    # 检查 systemctl restart 的退出状态
    if [ $? -ne 0 ]; then
        echo "Failed to restart network service."
        exit 1
    fi

    echo "NetworkManager stopped and network service restarted successfully."
else
    echo "NetworkManager

网口IP配置

此脚本用于配置网口IP地址,简化命令行操作

bash 复制代码
#!/bin/bash
interfaces=$(ip a | awk '/^[0-9]: / {printf "%s ", $2} END {print ""}')
echo "可选择网卡如下:$interfaces"
read -p "请输入网口:" eth
read -p "请输入IP:" eth_ip
read -p "请输入mask:" eth_mask
echo "IPADDR=$eth_ip" >> /etc/sysconfig/network-scripts/ifcfg-$eth
echo "NETMASK=$eth_mask" >> /etc/sysconfig/network-scripts/ifcfg-$eth
echo "正在重启网络"
systemctl restart network
echo "重启成功"

聚合口配置

此脚本用于自动配置聚合口

bash 复制代码
#!/bin/bash
status=$(systemctl status NetworkManager | grep 'Active:' | awk '{print $3}' | tr -d '()')
if [ "$status" != "dead" ]
then
    echo "NetworkManager服务已开启"
else
    # 开启 NetworkManager
    echo "正在重启NetworkManager"
    systemctl restart NetworkManager
    echo "重启成功"
fi

# 检查是否存在 bond0 连接
if nmcli connection show | grep -q 'bond0'; then
    echo "Warning: 'bond0' connection already exists."
    exit 1  # 或者执行其他适当的操作,例如删除现有连接再重新创建
fi

if [ $# -ne 3 ]; then
    echo "Usage: $0 <ip_address> <interface1> <interface2>"
    exit 1
fi

ip_address=$1
interface1=$2
interface2=$3


nmcli connection add type bond con-name bond0 ifname bond0 bond.options "mode=active-backup,miimon=100"
nmcli connection add type ethernet slave-type bond con-name bond0-port1 ifname ${interface1} master bond0
nmcli connection add type ethernet slave-type bond con-name bond0-port2 ifname ${interface2} master bond0
nmcli connection modify bond0 ipv4.addresses ${ip_address}/24 ipv4.method manual  connection.autoconnect yes
nmcli connection modify bond0 connection.autoconnect-slaves 1
nmcli connection up bond0
echo "Bond connection created successfully."

总结

相关推荐
iconball21 分钟前
个人用云计算学习笔记 --18(NFS 服务器、iSCSI 服务器)
linux·运维·笔记·学习·云计算
广药门徒1 小时前
Linux驱动开发与BuildRoot是什么关系与其的应用场景
linux·运维·驱动开发
czhc11400756631 小时前
Linux108 shell:.bashrc 正则表达式:. * .* ^ $ ^$ [ ] [^] ^[] ^[^ ] \< \>
linux·正则表达式
野猪疯驴1 小时前
Linux shell学习(更新中....)
linux·shell
努力学习的小廉1 小时前
深入了解linux网络—— TCP网络通信(下)
linux·网络·tcp/ip
Bruce_Liuxiaowei3 小时前
MQTT协议在物联网环境中的安全风险与防范指南
运维·网络·物联网·安全·网络安全
Paul_09203 小时前
golang面经——内存相关模块
服务器·网络·golang
Lu Zelin8 小时前
单片机为什么不能跑Linux
linux·单片机·嵌入式硬件
-dzk-9 小时前
【3DGS复现】Autodl服务器复现3DGS《简单快速》《一次成功》《新手练习复现必备》
运维·服务器·python·计算机视觉·3d·三维重建·三维
CS Beginner9 小时前
【Linux】 Ubuntu 开发环境极速搭建
linux·运维·ubuntu