Skip to content

Commit 0ec9bab

Browse files
feat: Improvement of the network cvm assistant (#28)
1 parent 46dd5b3 commit 0ec9bab

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed
Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,53 @@
11
#!/usr/bin/env bash
2-
BASEDIR="$( cd "$( dirname "$0" )" && pwd )"
32

4-
function config() {
3+
###############################################################################
4+
# Script: network-config.sh
5+
# Description: Configure network interface on Ubuntu system
6+
#
7+
# This script configures a network interface with static IP configuration
8+
# on Ubuntu systems (TDX Trusted Domain Environment).
9+
#
10+
# Prerequisites:
11+
# - Must be run with root privileges
12+
# - Must run on Ubuntu OS (TDX Trusted Domain Environment)
13+
#
14+
# Environment Variables Required:
15+
# - ifName: Network interface name (e.g., eth0)
16+
# - ifIp: IP address to assign to the interface
17+
# - ifNetmask: Network subnet mask
18+
# - ifGateway: Gateway IP address
19+
#
20+
###############################################################################
21+
22+
function configureNetwork() {
23+
# Check if running on Ubuntu
24+
if ! grep -q "ID=ubuntu" /etc/os-release; then
25+
echo "This script only supports Ubuntu. Current OS is not supported."
26+
exit 1
27+
fi
28+
29+
# Check if all required environment variables are set
30+
if [ -z "${ifName}" ] || [ -z "${ifIp}" ] || [ -z "${ifNetmask}" ] || [ -z "${ifGateway}" ]; then
31+
echo "Error: Missing required environment variables."
32+
echo "Required variables: ifName, ifIp, ifNetmask, ifGateway"
33+
exit 1
34+
fi
35+
536
echo "nameserver 8.8.8.8" > /etc/resolv.conf
6-
7-
OS_TYPE=`cat /etc/os-release |grep -w ID |awk -F= '{print $2}'`
837

9-
case "$OS_TYPE" in
10-
ubuntu)
11-
cat>/etc/network/interfaces<<EOF
38+
cat>/etc/network/interfaces<<EOF
1239
auto ${ifName}
1340
iface ${ifName} inet static
1441
address ${ifIp}
1542
netmask ${ifNetmask}
1643
gateway ${ifGateway}
1744
EOF
18-
/etc/init.d/networking restart
19-
if [ $? != 0 ];then
20-
echo config network failed !
21-
exit -1
22-
fi
23-
;;
24-
centos)
25-
echo not support now
26-
exit -1
27-
;;
28-
alpine)
29-
echo not support now
30-
exit -1
31-
;;
32-
*)
33-
echo unknow os $OS_TYPE exit!
34-
return
35-
;;
36-
esac
37-
}
3845

39-
function main() {
40-
config
46+
/etc/init.d/networking restart
47+
if [ $? != 0 ];then
48+
echo "Network configuration failed!"
49+
exit 1
50+
fi
4151
}
4252

43-
main
53+
configureNetwork

0 commit comments

Comments
 (0)