Jump to content

Hi all

 

I have been set with the task of creating an automated pen test box for work.

 

Now the device needs to obtain the IP address range automatically and I am struggling on how I can achieve this without user input.

The reason I need the IP range is so I can pump this to NMAP and Fping to check for live devices.

 

Now I have used the following command to obtain the IP address from ifconfig /sbin/ifconfig $adapter | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'"

Now when I run the above directly in a terminal I get the IP address only, however when I put this into a bash script , it shows be the following output : "192.168.0.39 Bcast"

 

Below is my bash script

 

#Varibles

adapter="enp0s3"

ipaddress="/sbin/ifconfig $adapter | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'"

ip=$(eval $ipaddress) #Runs the command above

echo $ip

 

 

I also need this script to pull the broadcast address, which I am also struggling with.

However when I obtain the IP address I need to change the last digit after the 3 dot to a 0.

 

Im also hoping I can use a IF statement to translate the broadcast address to /24 for example

 

Anyhelp would be appreciated!! 

 

 

 

Link to comment
https://linustechtips.com/topic/662795-bash-programming/
Share on other sites

Link to post
Share on other sites

9 minutes ago, blaze1993 said:

Hi all

 

I have been set with the task of creating an automated pen test box for work.

 

Now the device needs to obtain the IP address range automatically and I am struggling on how I can achieve this without user input.

The reason I need the IP range is so I can pump this to NMAP and Fping to check for live devices.

 

Now I have used the following command to obtain the IP address from ifconfig /sbin/ifconfig $adapter | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'"

Now when I run the above directly in a terminal I get the IP address only, however when I put this into a bash script , it shows be the following output : "192.168.0.39 Bcast"

 

Below is my bash script

 

#Varibles

adapter="enp0s3"

ipaddress="/sbin/ifconfig $adapter | grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'"

ip=$(eval $ipaddress) #Runs the command above

echo $ip

 

 

I also need this script to pull the broadcast address, which I am also struggling with.

However when I obtain the IP address I need to change the last digit after the 3 dot to a 0.

 

Im also hoping I can use a IF statement to translate the broadcast address to /24 for example

 

Anyhelp would be appreciated!! 

 

 

 

 
function network_address_to_ips() {
  # define empty array to hold the ip addresses
  ips=()
  # create array containing network address and subnet
  network=(${1//\// })
  # split network address by dot
  iparr=(${network[0]//./ })
  # check for subnet mask or create subnet mask from CIDR notation
  if [[ ${network[1]} =~ '.' ]]; then
    netmaskarr=(${network[1]//./ })
  else
    if [[ $((8-${network[1]})) -gt 0 ]]; then
      netmaskarr=($((256-2**(8-${network[1]}))) 0 0 0)
    elif  [[ $((16-${network[1]})) -gt 0 ]]; then
      netmaskarr=(255 $((256-2**(16-${network[1]}))) 0 0)
    elif  [[ $((24-${network[1]})) -gt 0 ]]; then
      netmaskarr=(255 255 $((256-2**(24-${network[1]}))) 0)
    elif [[ $((32-${network[1]})) -gt 0 ]]; then 
      netmaskarr=(255 255 255 $((256-2**(32-${network[1]}))))
    fi
  fi
  # correct wrong subnet masks (e.g. 240.192.255.0 to 255.255.255.0)
  [[ ${netmaskarr[2]} == 255 ]] && netmaskarr[1]=255
  [[ ${netmaskarr[1]} == 255 ]] && netmaskarr[0]=255
  # generate list of ip addresses
  for i in $(seq 0 $((255-${netmaskarr[0]}))); do
    for j in $(seq 0 $((255-${netmaskarr[1]}))); do
      for k in $(seq 0 $((255-${netmaskarr[2]}))); do
        for l in $(seq 1 $((255-${netmaskarr[3]}))); do
          ips+=( $(( $i+$(( ${iparr[0]}  & ${netmaskarr[0]})) ))"."$(( $j+$(( ${iparr[1]} & ${netmaskarr[1]})) ))"."$(($k+$(( ${iparr[2]} & ${netmaskarr[2]})) ))"."$(($l+$((${iparr[3]} & ${netmaskarr[3]})) )) )
        done
      done
    done
  done
}

I found that off 

http://stackoverflow.com/questions/16986879/bash-script-to-list-all-ips-in-prefix

Link to comment
https://linustechtips.com/topic/662795-bash-programming/#findComment-8564224
Share on other sites

Link to post
Share on other sites

Ahh I borrowed this and It now works

 

o1=$(echo $ip | cut -d '.' -f4);

        o2=$(echo $ip | cut -d '.' -f3);

        o3=$(echo $ip | cut -d '.' -f2);

        o4=$(echo $ip | cut -d '.' -f1);

 

echo "$o4.$o3.$o2.0" #This echos the ip address with the last character as a 0

Link to comment
https://linustechtips.com/topic/662795-bash-programming/#findComment-8564413
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×