| 9 | |
| 10 | check valid script "./checkIP.sh 192.168.8.9/24" |
| 11 | |
| 12 | {{{ |
| 13 | #!/bin/bash |
| 14 | |
| 15 | # Test an IP address for validity: |
| 16 | # Usage: |
| 17 | # valid_ip IP_ADDRESS |
| 18 | # if [[ $? -eq 0 ]]; then echo good; else echo bad; fi |
| 19 | # OR |
| 20 | # if valid_ip IP_ADDRESS; then echo good; else echo bad; fi |
| 21 | # |
| 22 | |
| 23 | function valid_ip() |
| 24 | { |
| 25 | local ip=$1 |
| 26 | local stat=1 |
| 27 | |
| 28 | if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then |
| 29 | OIFS=$IFS |
| 30 | IFS='.' |
| 31 | ip=($ip) |
| 32 | IFS=$OIFS |
| 33 | [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ |
| 34 | && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] |
| 35 | stat=$? |
| 36 | fi |
| 37 | return $stat |
| 38 | } |
| 39 | |
| 40 | |
| 41 | # set static LAN IP in /etc/network/interfaces |
| 42 | IP_ADDR=$1 |
| 43 | IP_ADDR_NET=${IP_ADDR/\//\\/} |
| 44 | IP_ONLY=$(sed "s/\/.*//" <<< "$IP_ADDR") |
| 45 | echo $IP_ONLY |
| 46 | if valid_ip $IP_ONLY; then stat=true; else stat=false; fi |
| 47 | if [ "$stat" = true ] ; then |
| 48 | echo "IP set" |
| 49 | sudo /bin/sed -i "s/address.*/address $IP_ADDR_NET/g" /etc/network/interfaces |
| 50 | else |
| 51 | echo "IP not valid" |
| 52 | fi |
| 53 | |
| 54 | }}} |
| 55 | |
| 56 | check valid GW ip "./checkGW.sh 192.168.0.1" |
| 57 | {{{ |
| 58 | #!sh |
| 59 | #!/bin/bash |
| 60 | |
| 61 | # Test an IP address for validity: |
| 62 | # Usage: |
| 63 | # valid_ip IP_ADDRESS |
| 64 | # if [[ $? -eq 0 ]]; then echo good; else echo bad; fi |
| 65 | # OR |
| 66 | # if valid_ip IP_ADDRESS; then echo good; else echo bad; fi |
| 67 | # |
| 68 | |
| 69 | function valid_ip() |
| 70 | { |
| 71 | local ip=$1 |
| 72 | local stat=1 |
| 73 | |
| 74 | if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then |
| 75 | OIFS=$IFS |
| 76 | IFS='.' |
| 77 | ip=($ip) |
| 78 | IFS=$OIFS |
| 79 | [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ |
| 80 | && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] |
| 81 | stat=$? |
| 82 | fi |
| 83 | return $stat |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | # set static LAN IP in /etc/network/interfaces |
| 89 | GW_ADDR=$1 |
| 90 | |
| 91 | if valid_ip $GW_ADDR; then stat=true; else stat=false; fi |
| 92 | if [ "$stat" = true ] ; then |
| 93 | echo "GW set" |
| 94 | sudo /bin/sed -i "s/gateway.*/gateway $GW_ADDR/g" /etc/network/interfaces |
| 95 | else |
| 96 | echo "GW not valid" |
| 97 | fi |
| 98 | |
| 99 | }}} |