Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 40 additions & 30 deletions cvmassistants/network-tool/network-config.sh
Original file line number Diff line number Diff line change
@@ -1,43 +1,53 @@
#!/usr/bin/env bash
BASEDIR="$( cd "$( dirname "$0" )" && pwd )"

function config() {
###############################################################################
# Script: network-config.sh
# Description: Configure network interface on Ubuntu system
#
# This script configures a network interface with static IP configuration
# on Ubuntu systems (TDX Trusted Domain Environment).
#
# Prerequisites:
# - Must be run with root privileges
# - Must run on Ubuntu OS (TDX Trusted Domain Environment)
#
# Environment Variables Required:
# - ifName: Network interface name (e.g., eth0)
# - ifIp: IP address to assign to the interface
# - ifNetmask: Network subnet mask
# - ifGateway: Gateway IP address
#
###############################################################################

function configureNetwork() {
# Check if running on Ubuntu
if ! grep -q "ID=ubuntu" /etc/os-release; then
echo "This script only supports Ubuntu. Current OS is not supported."
exit 1
fi

# Check if all required environment variables are set
if [ -z "${ifName}" ] || [ -z "${ifIp}" ] || [ -z "${ifNetmask}" ] || [ -z "${ifGateway}" ]; then
echo "Error: Missing required environment variables."
echo "Required variables: ifName, ifIp, ifNetmask, ifGateway"
exit 1
fi

echo "nameserver 8.8.8.8" > /etc/resolv.conf

OS_TYPE=`cat /etc/os-release |grep -w ID |awk -F= '{print $2}'`

case "$OS_TYPE" in
ubuntu)
cat>/etc/network/interfaces<<EOF
cat>/etc/network/interfaces<<EOF
auto ${ifName}
iface ${ifName} inet static
address ${ifIp}
netmask ${ifNetmask}
gateway ${ifGateway}
EOF
/etc/init.d/networking restart
if [ $? != 0 ];then
echo config network failed !
exit -1
fi
;;
centos)
echo not support now
exit -1
;;
alpine)
echo not support now
exit -1
;;
*)
echo unknow os $OS_TYPE exit!
return
;;
esac
}

function main() {
config
/etc/init.d/networking restart
if [ $? != 0 ];then
echo "Network configuration failed!"
exit 1
fi
}

main
configureNetwork