#!/bin/bash
#set -x
# Check if the user is root
if [ "$EUID" -ne 0 ]; then
echo "This script must be run as root. Use sudo."
exit 1
fi
# Check if the file /usr/local/bin/newt exists and is executable
if [ -e /usr/local/bin/newt ]; then
if [ -x /usr/local/bin/newt ]; then
echo "The file /usr/local/bin/newt exists and is executable. All good."
else
echo "The file /usr/local/bin/newt exists but is not executable."
echo "Changing permissions to make it executable..."
chmod +x /usr/local/bin/newt
if [ $? -eq 0 ]; then
echo "Permissions updated successfully: the file is now executable."
else
echo "Error: unable to update permissions of /usr/local/bin/newt."
exit 1
fi
fi
else
echo "The file /usr/local/bin/newt does not exist. Run the following command:"
echo "curl -fsSL https://digpangolin.com/get-newt.sh | bash"
exit 0
fi
# Request the 3 variables as input
read -p "Enter the ID: " ID
read -p "Enter the SECRET: " SECRET
read -p "Enter the ENDPOINT: " ENDPOINT
# Ask if client connections should be accepted
default_accept="n"
read -p "Should it be reachable by client connections? (y/N): " ACCEPT
ACCEPT=${ACCEPT:-$default_accept}
# Verify that all variables are set
if [ -z "$ID" ] || [ -z "$SECRET" ] || [ -z "$ENDPOINT" ]; then
echo "Error: All variables are mandatory."
exit 1
fi
# Create the user 'newt' and add it to the 'daemon' group
useradd -s /usr/sbin/nologin -G daemon newt
# Check if the user was created successfully
if [ $? -eq 0 ]; then
echo "User 'newt' created and added to the 'daemon' group."
else
echo "Error creating the user 'newt'."
exit 1
fi
# Create the systemd service file for newt
SERVICE_FILE=/etc/systemd/system/newt.service
echo "Creating the service file $SERVICE_FILE..."
# Build the ExecStart string based on the choice
EXEC_CMD="/usr/local/bin/newt --id ${ID} --secret ${SECRET} --endpoint ${ENDPOINT}"
if [[ "$ACCEPT" == "y" || "$ACCEPT" == "Y" ]]; then
EXEC_CMD+=" --accept-clients"
fi
cat <<EOF > $SERVICE_FILE
[Unit]
Description=Newt VPN Client
After=network.target
[Service]
ExecStart=${EXEC_CMD}
Restart=always
User=newt
[Install]
WantedBy=multi-user.target
EOF
# Verify that the service file was created
if [ -f "$SERVICE_FILE" ]; then
echo "Service file successfully created at $SERVICE_FILE."
else
echo "Error: unable to create the service file
Nessun commento:
Posta un commento