Create check_sudo.sh

This commit is contained in:
spiritLHLS 2022-12-18 13:24:47 +08:00 committed by GitHub
parent 195790f514
commit 43946c99bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

54
check_sudo.sh Normal file
View File

@ -0,0 +1,54 @@
#!/bin/bash
# Check if the hostname is set correctly in /etc/hosts
HOSTNAME=$(cat /etc/hostname)
HOSTS_LINE="$(grep $HOSTNAME /etc/hosts)"
if [ -z "$HOSTS_LINE" ]; then
# Hostname not found in /etc/hosts. Add it.
echo "Updating /etc/hosts with hostname: $HOSTNAME"
sudo bash -c "echo '127.0.0.1 $HOSTNAME' >> /etc/hosts"
else
# Hostname found in /etc/hosts. Check if the IP address is correct.
HOSTS_IP="$(awk '{print $1}' <<<$HOSTS_LINE)"
if [ "$HOSTS_IP" != "127.0.0.1" ]; then
# IP address is incorrect. Update it.
echo "Updating IP address for $HOSTNAME in /etc/hosts"
sudo sed -i "s/$HOSTS_IP/127.0.0.1/g" /etc/hosts
else
# Hostname and IP address are correct. No changes needed.
echo "Hostname and IP address in /etc/hosts are correct."
fi
fi
# Check if the sudo command works
sudo echo "Testing sudo command..."
if [ $? -eq 0 ]; then
# Sudo command works. Exit the script.
echo "Fix successful. Exiting script."
else
# Sudo command failed. Try restarting the networking interface.
echo "Sudo command still failing. Restarting networking interface."
sudo service networking restart
# Check if the sudo command works after restarting the networking interface
sudo echo "Testing sudo command after restarting networking interface..."
if [ $? -eq 0 ]; then
# Sudo command works. Exit the script.
echo "Fix successful. Exiting script."
else
# Sudo command failed. Try restarting the DNS server.
echo "Sudo command still failing. Restarting DNS server."
sudo service bind9 restart
# Check if the sudo command works after restarting the DNS server
sudo echo "Testing sudo command after restarting DNS server..."
if [ $? -eq 0 ]; then
# Sudo command works. Exit the script.
echo "Fix successful. Exiting script."
else
# Sudo command still failing. Exiting script.
echo "Unable to fix problem. Exiting script."
fi
fi
fi