
		newtonJr.
		
		(usa CentOS)
		
		Enviado em 29/10/2009 - 11:21h 
		Olá comunidade. 
Fiz um script que processa uma tabela de roteamento de um servidor. 
Eis uma tabela de exemplo:
user@servidor:~/user > route -n
Tabela de Roteamento IP do Kernel
Destino         Roteador        MßscaraGen.    Opþ§es MÚtrica Ref   Uso Iface
10.114.32.154   192.164.34.140  255.255.255.255 UGH   6      0        0 eth0
10.114.32.153   192.164.34.140  255.255.255.255 UGH   6      0        0 eth0
152.13.49.21    192.164.34.140  255.255.255.255 UGH   2      0        0 eth0
10.114.32.26    192.164.34.140  255.255.255.255 UGH   6      0        0 eth0
152.13.174.121  192.164.34.110  255.255.255.255 UGH   2      0        0 eth0
10.114.32.25    192.164.34.140  255.255.255.255 UGH   6      0        0 eth0
152.13.49.25    192.164.34.110  255.255.255.255 UGH   2      0        0 eth0
152.13.102.5    192.164.34.110  255.255.255.255 UGH   3      0        0 eth0
152.13.43.14    192.164.34.110  255.255.255.255 UGH   2      0        0 eth0
152.13.93.34    192.164.34.110  255.255.255.255 UGH   4      0        0 eth0
152.13.46.5     192.164.34.110  255.255.255.255 UGH   2      0        0 eth0
152.13.54.5     192.164.34.140  255.255.255.255 UGH   2      0        0 eth0
152.13.44.1     192.164.34.140  255.255.255.255 UGH   2      0        0 eth0
152.13.49.30    192.164.34.140  255.255.255.255 UGH   4      0        0 eth0
152.13.101.0    192.164.34.110  255.255.255.252 UG    4      0        0 eth0
152.13.175.64   192.164.34.110  255.255.255.252 UG    5      0        0 eth0
152.13.43.12    192.164.34.110  255.255.255.252 UG    2      0        0 eth0
152.13.175.12   192.164.34.110  255.255.255.252 UG    6      0        0 eth0
152.13.43.28    192.164.34.110  255.255.255.252 UG    6      0        0 eth0
152.13.54.0     192.164.34.140  255.255.255.252 UG    4      0        0 eth0
152.13.54.4     192.164.34.140  255.255.255.252 UG    2      0        0 eth0
152.13.93.0     192.164.34.110  255.255.255.252 UG    4      0        0 eth0
152.13.174.120  192.164.34.110  255.255.255.252 UG    2      0        0 eth0
152.13.96.0     192.164.34.110  255.255.255.252 UG    3      0        0 eth0
152.13.46.8     192.164.34.110  255.255.255.252 UG    3      0        0 eth0
152.13.46.4     192.164.34.110  255.255.255.252 UG    2      0        0 eth0
152.13.104.4    192.164.34.110  255.255.255.252 UG    6      0        0 eth0
152.13.174.4    192.164.34.110  255.255.255.252 UG    6      0        0 eth0
152.13.104.0    192.164.34.110  255.255.255.252 UG    6      0        0 eth0
152.13.53.4     192.164.34.140  255.255.255.252 UG    2      0        0 eth0
152.13.49.20    192.164.34.140  255.255.255.252 UG    2      0        0 eth0
152.13.49.24    192.164.34.110  255.255.255.252 UG    2      0        0 eth0
152.13.103.4    192.164.34.110  255.255.255.252 UG    4      0        0 eth0
152.13.103.0    192.164.34.110  255.255.255.252 UG    5      0        0 eth0
152.13.41.24    192.164.34.110  255.255.255.252 UG    5      0        0 eth0
152.13.41.0     192.164.34.110  255.255.255.252 UG    6      0        0 eth0
152.13.95.24    192.164.34.110  255.255.255.252 UG    5      0        0 eth0
152.13.95.20    192.164.34.110  255.255.255.252 UG    4      0        0 eth0
152.13.95.4     192.164.34.110  255.255.255.252 UG    4      0        0 eth0
152.13.98.20    192.164.34.110  255.255.255.252 UG    6      0        0 eth0
152.13.1.4      192.164.34.110  255.255.255.252 UG    6      0        0 eth0
152.13.98.0     192.164.34.110  255.255.255.252 UG    4      0        0 eth0
152.13.44.0     192.164.34.140  255.255.255.252 UG    2      0        0 eth0
152.13.103.0    192.164.34.110  255.255.255.0   UG    6      0        0 eth0
172.19.104.0    192.164.34.110  255.255.255.0   UG    7      0        0 eth0
172.19.174.0    192.164.34.110  255.255.255.0   UG    4      0        0 eth0
192.168.49.0    192.164.34.140  255.255.255.0   UG    3      0        0 eth0
10.114.32.0     192.164.34.140  255.255.255.0   UG    6      0        0 eth0
172.19.103.0    192.164.34.110  255.255.255.0   UG    5      0        0 eth0
10.129.1.0      192.164.34.140  255.255.255.0   UG    5      0        0 eth0
172.20.104.0    192.164.34.110  255.255.255.0   UG    7      0        0 eth0
192.164.34.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.164.43.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
12.89.0.0       0.0.0.0         255.255.252.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
0.0.0.0         12.89.0.1       0.0.0.0         UG    0      0        0 eth1
0.0.0.0         192.164.34.180  0.0.0.0         UG    1      0        0 eth0
user@servidor:~/user > 
Fiz um script que lê essa tabela e gera 02 arquivos com a lista de IPs destino e IPs dos roteadores.
Eis o script:
#!/bin/bash
route -n > tabela-roteamento.txt
touch lista-ips-destino.txt
touch lista-ips-roteadores.txt
echo "" > lista-ips-destino.txt 
echo "" >lista-ips-roteadores.txt
cat tabela-roteamento.txt | while read linha; do 
   ipdestino=`echo $linha | awk {'print $1'}`
   iproteador=`echo $linha | awk {'print $2'}`
   mascara=`echo $linha | awk {'print $3'}`
   echo "$ipdestino" >> lista-ips-destino.txt
   echo "$iproteador" >> lista-ips-roteadores.txt
done
Preciso melhorar esse script em alguns aspectos:
1) Preciso ignorar as duas primeiras linhas; só quero IPs e mais nada. Como faz isso em shell?
2) Depois que o arquivo é formado, gostaria de classificar o arquivo pelos IPs, tipo menores primeiros e maiores no fim do arquivo. Como fazer ?
3) Preciso criar um arquivo de IPs destino no formato IP/numero de bits da mascara. Isso, porque vou ter que utilizar esse formato no comando nmap pra testar se essas máquinas estão ativas na rede.
Exemplo: 192.168.45.0/24 ou 192.168.44.11/32. Para isso precisaria converter a mascara em numero de bits, tipo 255.255.255.0 -> 24, 255.255.255.255 -> 32, etc. Como posso fazer?
4) Pela tabela acima dá pra vê que existem apenas 03 roteadores. Como posso obter uma lista de roteadores com IPs não repetidos? 
1 - 192.168.43.110
2 - 192.168.43.140
3 - 10.98.0.1
4 - 192.168.43.180
5) Como ignorar os IPs "0.0.0.0" ?
Agradeço a atenção de todos e estou no aguardo on-line. Att.