Advertisement
Guest User

Untitled

a guest
Sep 17th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.75 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. usage() {
  4.         echo -e "Usage: $0 <newip/mask> <interface>\n\tExample: $0 192.168.1.5/24 eth0"
  5.         exit 1
  6. }
  7.  
  8. NEWIP=$(echo $1 | grep -Eo "^(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])(\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])){3}\/[0-9]{1,2}$" | head -n 1)
  9. IFACE=$(echo $2 | grep -Eoi '^[-a-z0-9_]*$' | head -n 1)
  10.  
  11. if [ "$NEWIP" = "" ]; then
  12.         echo "Incorrect IP address"
  13.         usage
  14. fi
  15.  
  16. if [ "$IFACE" = "" ]; then
  17.         echo "Incorrect interface name"
  18.         usage
  19. fi
  20.  
  21. if [ $(ip link show | sed '/^\s/d;s/://g' | awk '{ print $2 }' | grep -Eo "^$IFACE$" | wc -l) -lt 1 ]; then
  22.         echo "Unknown interface"
  23.         usage
  24. fi
  25.  
  26. ip address flush dev $IFACE
  27. ip address add $NEWIP dev $IFACE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement