forked from DouglasFreshHabian/Bash_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudevMagic.sh
More file actions
187 lines (162 loc) · 5.84 KB
/
Copy pathudevMagic.sh
File metadata and controls
187 lines (162 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
#=======================#
# Color Escape Codes
#=======================#
# Regular Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
PURPLE='\033[0;35m'
WHITE='\033[0;37m'
RESET='\033[0m'
# Bold Colors
REDB='\033[1;31m'
GREENB='\033[1;32m'
YELLOWB='\033[1;33m'
BLUEB='\033[1;34m'
CYANB='\033[1;36m'
PURPLEB='\033[1;35m'
WHITEB='\033[1;37m'
# Bold High Intensity
BLACKH='\e[1;90m'
REDH='\e[1;91m'
GREENH='\e[1;92m'
YELLOWH='\e[1;93m'
BLUEH='\e[1;94m'
PURPLEH='\e[1;95m'
CYANH='\e[1;96m'
WHITEH='\e[1;97m'
#=======================#
# Script Constants
#=======================#
RULE_PATH="/etc/udev/rules.d/70-external-adapter.rules"
allcolors=("RED" "GREEN" "YELLOW" "BLUE" "CYAN" "PURPLE" "WHITE")
#=======================#
# Functions
#=======================#
# Ctrl+C Handler
trap ctrl_c INT
function ctrl_c() {
echo -e "\n${RED}Script was interrupted by the user. Exiting...${RESET}"
exit 1
}
# Print Random Colored Banner
ascii_banner() {
random_color="${allcolors[$((RANDOM % ${#allcolors[@]}))]}"
case $random_color in
"RED") color_code=$RED ;;
"GREEN") color_code=$GREEN ;;
"YELLOW") color_code=$YELLOW ;;
"BLUE") color_code=$BLUE ;;
"CYAN") color_code=$CYAN ;;
"PURPLE") color_code=$PURPLE ;;
"WHITE") color_code=$WHITE ;;
esac
echo -e "${color_code}"
cat << "EOF"
N: X
X: x0. K0 ..
cXXN Xx kX 0XXl
'NMN .Kc dX. .NMW.
NMN. l0' oK' ;WMx
OWN' NK. lKc dWW'
lWN: ,N0 cXO XWX
;NNo dNk :KN ;NWo
.NWk NNo ;KN. xNW'
XWK ,WXc ,KNc .XWN
0WN. dWK, 'KWx :NWd
xWN: XWK .KWK OWN,
lNWd .WWO KWW .NWX
;NW0 xOOokOOkkOOkdOkx lWWd
'NWWXNNNNN0'','';,'',;'',''kWWWWWWNWN,
.''...',,,,''...';
''......,;;......';
'''....'''''.....',
.. .''.
EOF
echo -e "${RESET}"
}
#=======================#
# Main
#=======================#
# Welcome Banner
ascii_banner
echo -e "${BLUE}Welcome to the Udev Rule Setup Script${RESET}"
echo -e "${YELLOW}This script will help you change the name of your external network adapter.${RESET}"
# User Input: Adapter Name
echo -e "${GREEN}Please enter the name of the adapter you want to change (e.g., eth0, wlan0):${RESET}"
read -p "Adapter Name: " ADAPTER_NAME
if ! ip link show "$ADAPTER_NAME" &>/dev/null; then
echo -e "${RED}Error: The adapter '$ADAPTER_NAME' does not exist. Please check the name and try again.${RESET}"
exit 1
fi
# Retrieve MAC Address
MAC_ADDRESS=$(ip link show "$ADAPTER_NAME" | awk '/ether/ {print $2}')
if [[ -z "$MAC_ADDRESS" ]]; then
echo -e "${RED}Error: Could not retrieve the MAC address for '$ADAPTER_NAME'.${RESET}"
exit 1
fi
# User Input: New Adapter Name
echo -e "${GREEN}Please enter the new name you want to assign to the adapter (e.g., my_adapter):${RESET}"
read -p "New Name: " NEW_NAME
if [[ -z "$NEW_NAME" ]]; then
echo -e "${RED}Error: New Name cannot be empty.${RESET}"
exit 1
fi
# Check for name collision
if ip link show "$NEW_NAME" &>/dev/null; then
echo -e "${RED}Error: The name '$NEW_NAME' is already in use. Please choose a different name.${RESET}"
exit 1
fi
# Backup existing Udev rule if present
if [[ -f "$RULE_PATH" ]]; then
sudo cp "$RULE_PATH" "${RULE_PATH}.bak"
echo -e "${YELLOW}Backup of existing rule created at ${RULE_PATH}.bak${RESET}"
fi
# Check for existing rule with same MAC address
if grep -q "ATTR{address}==\"$MAC_ADDRESS\"" "$RULE_PATH"; then
echo -e "${YELLOW}Warning: A rule with MAC address ${MAC_ADDRESS} already exists in ${RULE_PATH}.${RESET}"
echo -e "${CYAN}Existing rule:${RESET}"
grep "ATTR{address}==\"$MAC_ADDRESS\"" "$RULE_PATH"
echo -e "${WHITEB}"
read -p "Do you want to overwrite the existing rule? (y/n): " OVERWRITE_CHOICE
echo -e "${RESET}"
case "$OVERWRITE_CHOICE" in
[Yy]* )
echo -e "${YELLOW}Removing old rule and writing new one...${RESET}"
sudo sed -i "/ATTR{address}==\"$MAC_ADDRESS\"/d" "$RULE_PATH"
echo "SUBSYSTEM==\"net\", ACTION==\"add\", ATTR{address}==\"$MAC_ADDRESS\", NAME=\"$NEW_NAME\"" | sudo tee -a "$RULE_PATH" > /dev/null
;;
[Nn]* )
echo -e "${RED}Skipping rule creation for this MAC address.${RESET}"
;;
* )
echo -e "${RED}Invalid input. Skipping rule creation by default.${RESET}"
;;
esac
else
echo -e "${BLUE}Creating new Udev rule for your adapter...${RESET}"
echo "SUBSYSTEM==\"net\", ACTION==\"add\", ATTR{address}==\"$MAC_ADDRESS\", NAME=\"$NEW_NAME\"" | sudo tee -a "$RULE_PATH" > /dev/null
fi
# Apply Udev Rule
echo -e "${BLUE}Reloading Udev rules and applying changes...${RESET}"
sudo udevadm control --reload-rules
sudo udevadm trigger
# Confirmation
echo -e "${GREEN}Udev rule has been successfully created and applied!${RESET}"
echo -e "${YELLOW}Note: You must reboot your system for the changes to take effect.${RESET}"
# Prompt for Reboot
while true; do
echo -e "${WHITEB}"
read -p "Would you like to reboot now to apply changes? (y/n): " REBOOT_CHOICE
echo -e "${RESET}"
case "$REBOOT_CHOICE" in
[Yy]* ) echo -e "${BLUE}Rebooting system...${RESET}"; sudo reboot; break ;;
[Nn]* ) break ;;
* ) echo -e "${RED}Please answer y or n.${RESET}" ;;
esac
done
# Done
echo -e "${BLUE}Script completed successfully. Exiting...${RESET}"