1

Can anyone guide me on how to modify the first two octets in the IP address using shell script?

I tried to modify this code but it didn't work. Any guidance or help will be appreciated.

This example will replace the last octet:

cat test.sh
ip=$1
baseip=`echo $ip | cut -d"." -f1-3`
echo $baseip".0"
./test.sh 192.168.133.14
192.168.133.0

6 Answers6

6
ip=$1
tailip=`echo $ip | cut -d"." -f3-4`
echo "x.x."$tailip
dchirikov
  • 244
  • 1
  • 3
3
ip="10.1.2.3"
echo $ip | awk -F '.' '{printf("192.168.%d.%d", $3, $4)}'

Output: 192.168.2.3
Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • echo 192.168.133.14 | sed 's/[0-9][0-9][0-9].[0-9][0-9][0-9]./*.*./' *.*.133.14 –  Jan 08 '13 at 21:05
  • @user152523 masochistic. Also doesn't work if the first two octects are not 3 digits. – Sammitch Jan 08 '13 at 21:07
  • @user152523 `echo 192.168.133.14 | sed 's/\([0-9]*\)\.\([0-9]*\)\.[0-9]*\.[0-9]*/\1.\2.133.14/'` works, but it still more of a pain in the ass than awk or cut. – Sammitch Jan 08 '13 at 21:14
1

A good way using bash's PE ability:

ip="10.1.2.3"; tmpip="${ip#*.*.}";  echo "192.168.$tmpip"

Also a nice thing to notice is that you can randomize the IP's to be generated. Just providing some general ideas.

ip="10.1.2.3"; tmpip="${ip#*.*.}";  echo "$[RANDOM%256].$[RANDOM%256].$tmpip"
Valentin Bajrami
  • 229
  • 2
  • 14
0
bytes12=10.0
ip=$(echo $1 | sed "s/\([0-9]\+\.\)\{2\}\(.*$\)/$bytes12.\2/")

sed: s/a/b/ finds a and replaces it by b. \(\) encapsulate sub expressions, which are to be adressed by \1, \2, ....

First subexpression matches sequences like 192.168., i.e. two times (\{2\}) at least one digit (\+) followed by a dot. The second subexpression matches whatever comes between that and the end of the line ($).

Since we are not interested in \1, we don't need to mention it. We replace it by our fixed higher order bytes representation $bytes12. The last to bytes are exptected to be contained by \2, so we just append it.

J. Katzwinkel
  • 1,923
  • 16
  • 22
0
INET_NTOA () { 
    local IFS=. num quad ip e
    num=$1
    for e in 3 2 1
    do
        (( quad = 256 ** e))
        (( ip[3-e] = num / quad ))
        (( num = num % quad ))
    done
    ip[3]=$num
    echo "${ip[*]}"
}

INET_ATON () {
    local IFS=. ip num e
    ip=($1)
    for e in 3 2 1
    do
        (( num += ip[3-e] * 256 ** e ))
    done
    (( num += ip[3] ))
    echo "$num"
}

found here: How to efficiently convert long int to dotted quad IP in bash

Community
  • 1
  • 1
0

I use this type of feature for automating enabling or disabling firewall rules, specifically for services I only want on the local network, and not accessible from anywhere else.

I pieced this together from various parts of a server installation script that I use. It will pull the network interface name, pull the network and mask info, then parse it to give you the correct masked subnet and CIDR notation. I actually just added the last part in today using this page as a guide, but it will completely automate the process for you:

#!/bin/bash

subnetip=""
subnetmask=""
nicname=""

runme()
{
    # pull name for primary network interface with internet access
    nicname=$(ip route get 8.8.8.8 | awk '{print $5}')

    # pull interface IP Address/subnet info assigned by DHCP
    local subnetmasktemp=$(ip -o -f inet addr show $nicname | awk '{print $4}')

    # split ip/subnet into two variables, subnetip and subnetmask
    local IFS=/
    set $subnetmasktemp
    subnetip=$1
    subnetmask=$2

    # parse ipaddress and subnet info and output appropriate info for client subnet
    local baseip=0
    if [ "$subnetmask" -le 31 ] && [ "$subnetmask" -ge 24 ]; then
        baseip=`echo $subnetip | cut -d"." -f1-3`
        subnetip=($baseip".0")
    elif [ "$subnetmask" -le 23 ] && [ "$subnetmask" -ge 16 ]; then
        baseip=`echo $subnetip | cut -d"." -f1-2`
        subnetip=($baseip".0.0")
    elif [ "$subnetmask" -le 15 ] && [ "$subnetmask" -ge 8 ]; then
        baseip=`echo $subnetip | cut -d"." -f1`
        subnetip=($baseip".0.0.0")
    fi

    echo "$subnetip/$subnetmask"
}


runme;