Skip to content

Instantly share code, notes, and snippets.

@zekiahmetbayar
Created January 20, 2022 10:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zekiahmetbayar/73800fd1155abacf658b75a49abf4544 to your computer and use it in GitHub Desktop.
Save zekiahmetbayar/73800fd1155abacf658b75a49abf4544 to your computer and use it in GitHub Desktop.
Change ip automatically with bash script
#!/bin/bash
# Usage
## chmod +x ip_change.sh
## sudo ./ip_change.sh new_ip
path="/etc/network/interfaces"
current="$(hostname -I | cut -d' ' -f1 | xargs)"
new_ip=$1
if [ "$EUID" -ne 0 ]
then echo "Please run as root."
exit
fi
echo "Current ip is $current"
echo "New ip is $new_ip"
echo ""
echo "Ip change script has been started."
echo "$path file editing..."
sudo sed -i -e "s/^[[:blank:]]address.*/ address ${new_ip}/g" /etc/network/interfaces
echo "$path successfully edited."
echo "Flushing current ip.."
ip addr flush dev ens18
echo "Current ip flashed."
echo "Networking service restarting..."
systemctl restart networking
echo "Networking service successfully restarted."
echo ""
echo "Current ip ($current) successfully changed to new one($new_ip)."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment