|
1 | 1 | #!/usr/bin/env bash |
2 | | -BASEDIR="$( cd "$( dirname "$0" )" && pwd )" |
3 | 2 |
|
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 | + |
5 | 36 | echo "nameserver 8.8.8.8" > /etc/resolv.conf |
6 | | - |
7 | | - OS_TYPE=`cat /etc/os-release |grep -w ID |awk -F= '{print $2}'` |
8 | 37 |
|
9 | | - case "$OS_TYPE" in |
10 | | - ubuntu) |
11 | | - cat>/etc/network/interfaces<<EOF |
| 38 | + cat>/etc/network/interfaces<<EOF |
12 | 39 | auto ${ifName} |
13 | 40 | iface ${ifName} inet static |
14 | 41 | address ${ifIp} |
15 | 42 | netmask ${ifNetmask} |
16 | 43 | gateway ${ifGateway} |
17 | 44 | 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 | | -} |
38 | 45 |
|
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 |
41 | 51 | } |
42 | 52 |
|
43 | | -main |
| 53 | +configureNetwork |
0 commit comments