shell-script-pt
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [shell-script] ip,mascara de rede!!!


From: Fernando Lemes da Silva
Subject: Re: [shell-script] ip,mascara de rede!!!
Date: Wed, 12 Nov 2003 16:43:43 -0300

   O Aurelio postou isso na lista ontem e eu fiz o programinha por
diversão... mas é interessante voce entender o que cada linha do programa
faz, ok?

   []'s
   Fernando Lemes




#!/bin/bash

function errorexit () {
  echo -e "\nSCAN IPs\nUsage : scanips <ip> <netmask>\n"
}
ip1=$(echo "$1..." | cut -d "." -f 1)
ip2=$(echo "$1..." | cut -d "." -f 2)
ip3=$(echo "$1..." | cut -d "." -f 3)
ip4=$(echo "$1..." | cut -d "." -f 4)
if [ -z "$ip1" ]; then errorexit; exit 1; fi
if [ "$ip1" -lt "0" -o "$ip1" -gt "255" ]; then errorexit; exit 1; fi
if [ -z "$ip2" ]; then errorexit; exit 1; fi
if [ "$ip2" -lt "0" -o "$ip2" -gt "255" ]; then errorexit; exit 1; fi
if [ -z "$ip3" ]; then errorexit; exit 1; fi
if [ "$ip3" -lt "0" -o "$ip3" -gt "255" ]; then errorexit; exit 1; fi
if [ -z "$ip4" ]; then errorexit; exit 1; fi
if [ "$ip4" -lt "0" -o "$ip4" -gt "255" ]; then errorexit; exit 1; fi
nm1=$(echo "$2..." | cut -d "." -f 1)
nm2=$(echo "$2..." | cut -d "." -f 2)
nm3=$(echo "$2..." | cut -d "." -f 3)
nm4=$(echo "$2..." | cut -d "." -f 4)
if [ "$nm1" ] && [ -z "$nm2" ]&& [ -z "$nm3" ] && [ -z "$nm4" ]; then
nm=$nm1
else
  if [ -z "$nm1" ]; then errorexit; exit 1; fi
  if [ "$nm1" -lt 0 ] || [ "$nm1" -gt 255 ]; then errorexit; exit 1; fi
  if [ -z "$nm2" ]; then errorexit; exit 1; fi
  if [ "$nm2" -lt 0 ] || [ "$nm2" -gt 255 ]; then errorexit; exit 1; fi
  if [ -z "$nm3" ]; then errorexit; exit 1; fi
  if [ "$nm3" -lt 0 ] || [ "$nm3" -gt 255 ]; then errorexit; exit 1; fi
  if [ -z "$nm4" ]; then errorexit; exit 1; fi
  if [ "$nm4" -lt 0 ] || [ "$nm4" -gt 252 ]; then errorexit; exit 1; fi
  nm=0
  while [ "$nm1" -gt 0 ]; do nm=$[$nm + $nm1%2]; nm1=$[$nm1/2]; done
  while [ "$nm2" -gt 0 ]; do nm=$[$nm + $nm2%2]; nm2=$[$nm2/2]; done
  while [ "$nm3" -gt 0 ]; do nm=$[$nm + $nm3%2]; nm3=$[$nm3/2]; done
  while [ "$nm4" -gt 0 ]; do nm=$[$nm + $nm4%2]; nm4=$[$nm4/2]; done
fi
if [ "$nm" -gt "30" ]; then errorexit; exit 1; fi
network=$(ipcalc -n $ip1.$ip2.$ip3.$ip4/$nm | cut -d "=" -f 2)
broadcast=$(ipcalc -b $ip1.$ip2.$ip3.$ip4/$nm | cut -d "=" -f 2)

a1=$(echo "$network" | cut -d "." -f 1)
a2=$(echo "$network" | cut -d "." -f 2)
a3=$(echo "$network" | cut -d "." -f 3)
a4=$[$(echo "$network" | cut -d "." -f 4) + 1]
b1=$(echo "$broadcast" | cut -d "." -f 1)
b2=$(echo "$broadcast" | cut -d "." -f 2)
b3=$(echo "$broadcast" | cut -d "." -f 3)
b4=$[$(echo "$broadcast" | cut -d "." -f 4) - 1]

echo "Scanning network $network/$nm..."
for c1 in $(seq $a1 $b1); do
  for c2 in $(seq $a2 $b2); do
    for c3 in $(seq $a3 $b3); do
      for c4 in $(seq $a4 $b4); do
        ping -nc 1 -w 1 $c1.$c2.$c3.$c4 > /dev/null
        if [ "$?" -eq 0 ]; then echo "$c1.$c2.$c3.$c4"; fi
      done
    done
  done
done









----- Original Message ----- 
From: "juliocesar_martins" <address@hidden>
To: <address@hidden>
Sent: Wednesday, November 12, 2003 3:04 PM
Subject: [shell-script] ip,mascara de rede!!!


Ai pessoal, preciso desenvolver um shel script para verificar quais
ips estão em uma determinada rede , mas para tal deve ser solicitado
em ip valido e a mascara de rede , depois verificar quais ips estão
ativos , usando o comando ping.
Alguem me ajuda????




reply via email to

[Prev in Thread] Current Thread [Next in Thread]