Changes between Version 2 and Version 3 of Menu


Ignore:
Timestamp:
03/22/22 09:26:48 (3 years ago)
Author:
krit
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Menu

    v2 v3  
    77/home/admin/menu4.sh
    88}}}
     9
     10check 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
     23function 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
     42IP_ADDR=$1
     43IP_ADDR_NET=${IP_ADDR/\//\\/}
     44IP_ONLY=$(sed "s/\/.*//" <<< "$IP_ADDR")
     45echo $IP_ONLY
     46if valid_ip $IP_ONLY; then stat=true; else stat=false; fi
     47if [ "$stat" = true ] ; then
     48    echo "IP set"
     49    sudo /bin/sed -i "s/address.*/address $IP_ADDR_NET/g" /etc/network/interfaces
     50else
     51    echo "IP not valid"
     52fi
     53
     54}}}
     55
     56check 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
     69function 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
     89GW_ADDR=$1
     90
     91if valid_ip $GW_ADDR; then stat=true; else stat=false; fi
     92if [ "$stat" = true ] ; then
     93    echo "GW set"
     94    sudo /bin/sed -i "s/gateway.*/gateway  $GW_ADDR/g" /etc/network/interfaces
     95else
     96    echo "GW not valid"
     97fi
     98
     99}}}