Last active
February 23, 2023 02:58
-
-
Save sudoalx/cb46b2bfc8ecb3541b466c53db15eb7b to your computer and use it in GitHub Desktop.
NAT Interface Renamer for PNetLab and EVE-NG Lab Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script simplifies the process of renaming the NAT interface of PNetLab and EVE-NG lab files (.unl) by offering common naming options and the ability to enter a custom name. | |
# The script also includes error handling to assist users with unexpected inputs. | |
# After running the script, the original files will remain unchanged and new modified files will be created in a new directory called "renamed". | |
# To use the modified files, simply copy them back to the directory where your .unl files are stored. | |
# create renamed directory if it doesn't exist | |
if [ ! -d renamed ]; then | |
mkdir renamed | |
fi | |
# define color variables | |
GREEN='\033[0;32m' | |
RED='\033[0;31m' | |
YELLOW='\033[0;33m' | |
NC='\033[0m' # No Color | |
# define search and replace options | |
search_options=("cloud-nat" "nat0" "nat" "Other (enter custom term)") | |
replace_options=("cloud-nat" "nat0" "nat" "Other (enter custom term)") | |
# get search term from user | |
echo "Search for:" | |
for i in "${!search_options[@]}"; do | |
printf "%s. %s\n" "$((i+1))" "${search_options[$i]}" | |
done | |
read -p $'\e[33mSelect an option: \e[0m' search_choice | |
if ! [[ "$search_choice" =~ ^[1-4]$ ]]; then | |
echo "Error: Invalid input. Please select a valid option (1-4)." | |
echo "Usage: Place and run this script in the same directory as your .unl files to rename the NAT interface. Contact @Anthony_EdwardStark for help." | |
exit 1 | |
fi | |
search_term=${search_options[$((search_choice-1))]} | |
# get replace term from user | |
echo "Replace with:" | |
for i in "${!replace_options[@]}"; do | |
printf "%s. %s\n" "$((i+1))" "${replace_options[$i]}" | |
done | |
read -p $'\e[33mSelect an option: \e[0m' replace_choice | |
if ! [[ "$replace_choice" =~ ^[1-4]$ ]]; then | |
echo "Error: Invalid input. Please select a valid option (1-4)." | |
echo "Usage: Place and run this script in the same directory as your .unl files to rename the NAT interface. Contact @Anthony_EdwardStark for help." | |
exit 1 | |
fi | |
replace_term=${replace_options[$((replace_choice-1))]} | |
if [ "$replace_choice" -eq 4 ]; then | |
read -r replace_term | |
fi | |
# initialize counters | |
modified_count=0 | |
total_count=0 | |
for file in *.unl; do | |
if [ -f "$file" ]; then | |
modified=0 | |
line_num=0 | |
while IFS= read -r line; do | |
line_num=$((line_num+1)) | |
if [[ "$line" == *"$search_term"* ]]; then | |
line="${line//$search_term/$replace_term}" | |
modified=1 | |
echo -e "${GREEN}${file}:${line_num}:${NC} ${RED}${line}${NC}" | |
fi | |
echo "$line" >> "renamed/$file.tmp" | |
done < "$file" | |
if [ "$modified" -eq 1 ]; then | |
modified_count=$((modified_count+1)) | |
mv "renamed/$file.tmp" "renamed/$file" | |
else | |
rm "renamed/$file.tmp" | |
fi | |
total_count=$((total_count+1)) | |
fi | |
done | |
# output result | |
echo -e "${GREEN}$modified_count file(s) modified out of $total_count${NC}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This could actually batch rename more than just NAT interfaces, but NAT interfaces were the main goal 😂