Skip to content

Instantly share code, notes, and snippets.

@sarkrui
Last active August 2, 2023 13:21
Show Gist options
  • Save sarkrui/d7480a309c60c188c1db6c5f509d1229 to your computer and use it in GitHub Desktop.
Save sarkrui/d7480a309c60c188c1db6c5f509d1229 to your computer and use it in GitHub Desktop.
Creating danted services and config in bulk
#!/bin/bash
danted_path="/usr/sbin/danted"
function scan_ips() {
declare -a down_ips
echo "Pinging IP range 158.132.58.3 to 158.132.58.254 in 20 threads. This may take a moment..."
while read -r ip; do
down_ips+=("$ip")
done < <(for i in {3..254}; do echo 158.132.58.$i; done | xargs -I% -P 20 bash -c 'ping -c 1 -W 1 % > /dev/null && echo "% is down"')
echo "The following IP addresses are down:"
for ip in "${down_ips[@]}"; do
echo "$ip"
done
}
function set_eth() {
danted_path="/usr/sbin/danted"
while true; do
read -p "Please enter the IP beginning (158.132.58.xxx): " ip_begin
ip_address="158.132.58.$ip_begin"
# Ping the IP address to check its availability
ping -c 1 -W 1 $ip_address > /dev/null
if [ $? -eq 0 ]; then
echo "IP Address $ip_address is already in use. Please choose another."
else
echo "IP Address $ip_address is available."
break
fi
done
# List out all Ethernet devices
echo "Available Ethernet devices:"
ip a | grep -Eo '^[0-9]+: ens[0-9]+' | awk -F ': ' '{print $2}'
read -p "Please enter the Ethernet device number (only the number part of ensX): " eth
eth_dev="ens$eth"
serviced="danted-ens$eth"
service_file="danted-ens$eth.service"
config_file="/etc/danted-ens$eth.conf"
service_file="/lib/systemd/system/$service_file"
echo "IP Address: $ip_address"
echo "Ethernet Device: $eth_dev"
echo "Service: $serviced"
echo "Config file: $config_file"
echo "Service file: $service_file"
read -p "Are these settings correct? [y/N] " confirm
confirm=${confirm,,} # tolower
if [[ $confirm =~ ^(yes|y)$ ]]; then
ip addr add $ip_address/24 dev $eth_dev
ip link set dev $eth_dev up
systemctl restart $serviced
else
echo "Exiting due to user confirmation."
exit 1
fi
echo "user.privileged: root" > $config_file
echo "user.unprivileged: danteuser" >> $config_file
echo "" >> $config_file
echo "internal: ens$eth port = 1080" >> $config_file
echo "external: ens$eth" >> $config_file
echo "socksmethod: username" >> $config_file
echo "clientmethod: none" >> $config_file
echo "" >> $config_file
echo "client pass {" >> $config_file
echo " from: 0.0.0.0/0 to: 0.0.0.0/0" >> $config_file
echo "}" >> $config_file
echo "" >> $config_file
echo "socks pass {" >> $config_file
echo " from: 0.0.0.0/0 to: 0.0.0.0/0" >> $config_file
echo "}" >> $config_file
echo "[Unit]" > $service_file
echo "Description=Dante Server instance for ens$eth" >> $service_file
echo "After=network.target" >> $service_file
echo "" >> $service_file
echo "[Service]" >> $service_file
echo "ExecStart=$danted_path -f $config_file" >> $service_file
echo "Restart=always" >> $service_file
echo "User=root" >> $service_file
echo "KillMode=process" >> $service_file
echo "" >> $service_file
echo "[Install]" >> $service_file
echo "WantedBy=multi-user.target" >> $service_file
systemctl daemon-reload
systemctl start $serviced
systemctl enable $serviced
}
function reset_all() {
# Loop through all the Ethernet devices
for eth in $(ip a | grep -Eo '^[0-9]+: ens[0-9]+' | awk -F ': ' '{print $2}'); do
ip_address=$(ip -br a show $eth | awk '{print $3}' | awk -F '/' '{print $1}')
# Remove the IP address from the Ethernet device
if [[ ! -z $ip_address ]]; then
ip addr del $ip_address/24 dev $eth
fi
serviced="danted-$eth"
# Stop and disable the Dante service
systemctl stop $serviced
systemctl disable $serviced
config_file="/etc/danted-$eth.conf"
service_file="/lib/systemd/system/$serviced.service"
# Remove the Dante configuration file and service file
rm -f $config_file
rm -f $service_file
done
# Reload the systemd daemon
systemctl daemon-reload
}
function show_eth() {
ip a | awk '/^[0-9]: / {split($2, a, ":"); printf a[1] ": " a[2] " "; } /^ inet / {split($2, b, "/"); print b[1]; next}'
}
# Selection menu
PS3='Please enter your choice: '
options=("IP Scan" "Show Ethernet" "Set up Network" "Clean All" "Quit")
select opt in "${options[@]}"
do
case $opt in
"IP Scan")
clear
scan_ips
;;
"Show Ethernet")
clear
show_eth
;;
"Set up Network")
clear
set_eth
;;
"Clean All")
reset_all
;;
"Quit")
break
;;
*) echo "invalid option $REPLY";;
esac
done
@sarkrui
Copy link
Author

sarkrui commented Jul 17, 2023

bash -c "$(curl -fsSL https://danted-sh.verynerd.workers.dev)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment