Here are a few Unix/Bash scripts I use for myself.
I use these scripts in my own LAN and maybe “my” security is not enough for “your” security.
So check the code before you use the scripts “blind” 😉
If you copy/paste the script from here, don’t forget to
chmod +x scriptname.sh
to make it executable.
Start then the script with a “./” before the script name
./scriptname.sh
content :
├── fm-tree.sh / tree command handeling
├── fm-git.sh / github sync
├── fm-arpwatch.sh / arpwatch handeling
fm-tree.sh
Wikipedia: tree is a recursive directory listing command or program that produces a depth-indented listing of files. Originating in PC- and MS-DOS, it is found in Digital Research FlexOS,[1] IBM/Toshiba 4690 OS,[2] PTS-DOS,[3] FreeDOS,[4] IBM OS/2,[5] Microsoft Windows,[6] and ReactOS. A version for Unix and Unix-like systems is also available.
- In this script you are first asked which directory should be displayed.
- Then a selection (orders&files, only orders, only files) is displayed which you can select by selecting the corresponding numbers.
- At least the script displays the output of the corresponding tree command on the bash.
- If “tree” is not installed on the system, the script will display a message.
- perfect way to commit a local file structure to an AI-(ASCII-) input 😉
#!/bin/bash
echo "Please enter the directory path:"
read directory
# Check if tree is installed
if ! command -v tree &> /dev/null; then
echo "The 'tree' command is not installed."
echo "Please install it using 'sudo apt-get install tree' or your package manager."
exit 1
fi
echo "Choose an option:"
echo "1. Display folders and files"
echo "2. Display only directories"
echo "3. Display only files"
read choice
case $choice in
1)
tree "$directory"
;;
2)
tree -d "$directory"
;;
3)
tree -f "$directory"
;;
*)
echo "Invalid choice."
;;
esac
fm-git.sh
Script to automate github-sync.
- 3 choices “push;pull;overwrite” github with local content
- if overwrite, confirmation needed
- (yes I am using code-sever, change the variable on your own if you don’t like it 😉
- (hopefully) selfexplaining script …
- if not, drop me a note ->
- and yes, saving a git-hub-token in a bash script is a really bad idea, do your best to protect that script if you are using it !
#!/bin/bash
# Set your GitHub repository name
REPO_NAME="YOUR_REPO_NAME"
# Set the path to your local code-server project directory
CODE_SERVER_DIR="FULL-PATH-TO-THE-DIRECTORY-TO-SYNC-WITH-GITHUB"
# Set your GitHub personal access token
GITHUB_TOKEN="YOUR-GITHUB-TOKEN"
# Construct the repository URL with username and token
REPO_URL="https://YOUR-GITHUB-USERNAME:$GITHUB_TOKEN@github.com/YOUR-GITHUB-USERNAME/$REPO_NAME.git"
# Function to push changes to GitHub
push_changes() {
cd "$CODE_SERVER_DIR"
git add .
git commit -m "Update: $(date '+%Y-%m-%d %H:%M:%S')"
git push "$REPO_URL" main
echo "Changes pushed to GitHub."
}
# Function to pull changes from GitHub
pull_changes() {
cd "$CODE_SERVER_DIR"
git pull "$REPO_URL" main
echo "Changes pulled from GitHub."
}
# Function to overwrite GitHub repository with local content
overwrite_repo() {
echo "Are you sure you want to overwrite your existing GitHub repository? (yes/no)"
read confirmation
if [ "$confirmation" = "yes" ]; then
cd "$CODE_SERVER_DIR"
git add .
git commit -m "Overwrite: $(date '+%Y-%m-%d %H:%M:%S')"
git push -f "$REPO_URL" main
echo "Local content overwritten on GitHub."
else
echo "Git-overwrite canceled, quit app..."
exit 1
fi
}
# Main menu
echo "Select an option:"
echo "1. Push changes to GitHub"
echo "2. Pull changes from GitHub"
echo "3. Overwrite GitHub repository with local content"
read choice
case $choice in
1)
push_changes
;;
2)
pull_changes
;;
3)
overwrite_repo
;;
*)
echo "Invalid option"
;;
esac
fm-arpwatch.sh
Script to provide some kind of a “bash-GUI” to arpwatch.
Well, maybe this is more an application than a script, but AI scripting was a lot of fun, so I added more and more stuff in to the script 😉
Read comments, the script should be more or less self-explanatory, if not, send me a message.
If you like this, maybe ArpWatch2HTML is also something for you.
#!/bin/bash
# Check if script is run with root or sudo privileges
if [ "$(id -u)" != "0" ]; then
echo "This script needs to be run with root or sudo privileges."
exit 1
fi
# Function to validate yes or no choices
validate_yes_no() {
while true; do
read -p "Enter your choice (yes/no): " user_choice
case $user_choice in
[Yy]* ) echo "yes"; return 0;;
[Nn]* ) echo "no"; return 0;;
[YyNn] ) echo $user_choice; return 0;;
* ) echo "Invalid choice. Please enter 'yes' or 'no'.";;
esac
done
}
# Function to generate an empty ARP file
generate_empty_arp_file() {
local path="$1"
local filename="$2"
touch "$path/$filename"
echo ""
echo "Empty ARP file generated: $path/$filename"
echo ""
}
# Print header
clear
echo "+------------------------------------------------+"
echo "| arpwatch bash GUI ;-) |"
echo "| man pages for arpwatch : \"man arpwatch\" |"
echo "| (if you want to configure it on your own) |"
echo "+------------------------------------------------+"
echo ""
# Choice 1 / Choose save path
echo "1.) Where to save the arpwatch output files?"
read -p "Enter path to the directory (or press Enter to use current directory): " directory
if [ -z "$directory" ]; then
directory="$PWD"
echo "Using the current directory as the save path: $directory"
fi
filename="arpwatch_$(date +%y%m%d%H%M)"
output_log_filename="arpwatch_output_$(date +%y%m%d%H%M).log"
echo "Full path with filename for ARP file: $directory/$filename"
echo "Full path with filename for output log: $directory/$output_log_filename"
echo ""
# Choice 2 / Choose interface
default_interface=$(ip route get 1 | awk '{print $5}')
echo "2.) Would you like to use the default interface?"
echo "Default interface: $default_interface"
echo "Default IP address: $(ip addr show $default_interface | awk '/inet / {print $2}')"
use_default_interface=$(validate_yes_no)
if [ "$use_default_interface" = "no" ]; then
echo "Available interfaces:"
ip link show | awk -F ": " '{print $2}'
read -p "Enter the interface you would like to use: " chosen_interface
fi
echo "Your choice for using the default interface is: $use_default_interface"
echo ""
# Choice 3 / Enable/disable reporting of bogons
echo "3.) Would you like to DISABLE reporting of any bogons?"
bogons_choice=$(validate_yes_no)
echo "Your choice to disable reporting of any bogons: $bogons_choice"
echo ""
# Choice 4 / Enable/disable debugging
echo "4.) Would you like to DISABLE debugging?"
debug_choice=$(validate_yes_no)
echo "Your choice for enabling debugging: $debug_choice"
echo ""
# Compose arpwatch command based on user input
arpwatch_cmd=("arpwatch")
arpwatch_cmd+=("-f" "$directory/$filename")
if [ "$use_default_interface" = "no" ]; then
arpwatch_cmd+=("-i" "$chosen_interface")
else
arpwatch_cmd+=("-i" "$default_interface") # Use -i flag for interface
fi
if [ "$bogons_choice" = "no" ]; then
arpwatch_cmd+=("-N")
fi
if [ "$debug_choice" = "no" ]; then
arpwatch_cmd+=("-d")
fi
# Display composed command to the user
echo ""
echo " Composed arpwatch command:"
echo ""
echo " ${arpwatch_cmd[@]}"
echo ""
#!/bin/bash
# Ask user to execute the command
while true; do
read -p "Do you want to execute the above command? (yes/no): " execute_choice
if [[ "$execute_choice" =~ ^[Yy]$|^yes$ ]]; then
echo "Executing command in background..."
"${arpwatch_cmd[@]}" > "$directory/$output_log_filename" &
echo "Command executed."
# Generate the empty ARP file
generate_empty_arp_file "$directory" "$filename"
# Show arpwatch output log
read -p "Do you want to show the arpwatch output log? (yes/no): " show_log_choice
if [[ "$show_log_choice" =~ ^[Yy]$|^yes$ ]]; then
echo ""
echo "Full path to the output log: $directory/$output_log_filename"
echo "Press CTRL & c to cancel log-output to bash"
echo "-------------------------------------------"
echo ""
tail -f "$directory/$output_log_filename"
fi
break
elif [[ "$execute_choice" =~ ^[Nn]$|^no$ ]]; then
echo ""
echo "+------------------------------+"
echo "| Script aborted by user. |"
echo "+------------------------------+"
echo ""
break
else
echo "Invalid choice. Please enter 'yes' or 'no'."
fi
done
# Done
echo ""
echo "*********************"
echo "* End of the script *"
echo ""
echo "*********************"
echo ""
Comments are closed.