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: Mateus Caruccio
Subject: Re: [shell-script] ip,mascara de rede!!!
Date: Thu, 13 Nov 2003 02:52:38 +0800

opa,

 aqui vai uma versao mais simplificada (eh necessario o programa netmask, 
padrao no slackware):

#!/bin/bash

# descobre o 
if [ $# -eq 1 ]; then
        netip=$1
else
        read -p 'Informe a mascara de rede: ' mask
        read -p 'Informe um IP valido da rede: ' ip
        netip=$(ipmask $mask $ip 2>/dev/null | cut -f2 -d' ')
fi

# verifica se o IP eh valido
if [ -z "$netip" ]; then
        echo "Mascara ou IP inválido"
        exit
fi

echo Procurando na rede $netip

# faz um ping no endereco de rede (broadcast), mostrando todos os hosts que 
responderam
for host in $(ping $netip -w 3 -i 5 -s 1 2>/dev/null | sed -ne 's/^9 octets 
from \([0-9\.]*\):.*/\1/p' | sort | uniq); do
        echo Host ativo: $host
done




----- Original Message -----
From: "Fernando Lemes da Silva" <address@hidden>
Date: Wed, 12 Nov 2003 16:43:43 -0300
To: <address@hidden>
Subject: Re: [shell-script] ip,mascara de rede!!!

> 
>    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????
> 
> 
> 
> 
> ---------------------------------------------------------------------
> Esta lista não admite a abordagem de outras liguagens de programação, como 
> perl, C etc. Quem insistir em não seguir esta regra será moderado sem prévio 
> aviso.
> ---------------------------------------------------------------------
> Sair da lista: address@hidden
> ---------------------------------------------------------------------
> Esta lista é moderada de acordo com o previsto em 
> http://www.listasdiscussao.cjb.net
> ---------------------------------------------------------------------
>  
> 
> Seu uso do Yahoo! Grupos é sujeito às regras descritas em: 
> http://br.yahoo.com/info/utos.html 
> 
> 



# ########################################################
# Mateus de Oliveira Caruccio <caruccio at operamail.com>
# Linux User #183552 - UIN #37364320
# Vim, vi e matei o Emacs !



-- 
___________________________________________________
OperaMail free e-mail - http://www.operamail.com
OperaMail Premium - 28MB, POP3, more! US$29.99/year

Powered by Outblaze


reply via email to

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