Usando netstat para verificar conexões e portas

Publicado por marcio mendes mendes (última atualização em 01/12/2015)

[ Hits: 7.688 ]

Homepage: https://www.conectadohost.com

Download conexao.sh




Olá, esse script foi desenvolvido para verificar quantas conexões há em seu servidor, mostrando o ip, total de conexões, porta e total de conexões por portas.

O saída do script é similar a:

[+] Número de Conexões por IP:
        21 127.0.0.1
        12 192.168.1.1
        6 192.168.1.2
        2 192.168.1.3
        1 192.168.1.4
        1 192.168.1.5
        1 192.168.1.6
        1 192.168.1.7
        1 192.168.1.8

[+] Número de Conexões por porta:
        22 80
        15 110
        4 3306
        1 39806
        1 39809
        1 39813
        1 39815
        1 39828
        1 39799
        1 39803
        1 59635

[+] TOTAL: 63

Para executar digite:

$ conexao.sh

Obs.: os IPs 192.168.1.1, 192.168.1.2 etc são apenas um exemplo. Quando executar o script ele vai mostra os IPs reais que estão conectados ao seu servidor.

  



Esconder código-fonte

#!/usr/bin/perl
# attackers 2, with ipv6 support.
# output connection information from netstat
#

use strict;

sub run {
        my @netstat = `netstat -pant`;

        my $portcheck = $ARGV[0];
        if ( $portcheck =~ /^[0-9]+$/ ) {
                my %ports;
                my %ips;
                foreach (@netstat) {
                        my ($port, $ip);
                        if ( /^tcp\s+\d\s+\d\s+[0-9\.|0-9A-Za-z\.:]+:${portcheck}\s+([0-9\.|0-9A-Za-z\.:]+):/ ) {
                                chomp;
                                $ip = $1;
                                if ( $ip !~ /^::$/ ) {
                                        $ips{$ip}++;
                                }
                        }
                }
                my $count;
                print "[+] Highest connections on port $portcheck\n";
                foreach my $number ( sort {$ips{$b} <=> $ips{$a}} keys %ips ) {
                        if ( $count <= 10 ) {
                                if ($number) {
                                        print "\t$ips{$number} $number\n";
                                        $count++;
                                }
                        }
                }
                my $total;
                foreach my $key ( keys %ips ) {
                        if ($key) {
                                $total += $ips{$key};
                        }
                }
                print "\n[+] TOTAL: $total\n";
        } else {
                my %ports;
                my %ips;
                foreach (@netstat) {
                        my ($port, $ip);
                        if ( /^tcp\s+\d\s+\d\s+[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:([0-9]+)\s+([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}):[0-9]+\s+/ ) {
                                chomp;
                                $port = $1;
                                $ip = $2;
                        } elsif ( /^tcp\s+\d\s+\d\s+[0-9\.|0-9A-Za-z\.:]+:([0-9]+)\s+([0-9\.|0-9A-Za-z\.:]+):[0-9]+\s+/ ) {
                                chomp;
                                $port = $1;
                                $ip = $2;
                        }
                        $ports{$port}++;
 $ips{$ip}++;
                }

                my $count;
                print "[+] Número de Conexões por IP:\n";
                foreach my $number ( sort {$ips{$b} <=> $ips{$a}} keys %ips ) {
                        if ( $count <= 10 ) {
                                if ($number) {
                                        print "\t$ips{$number} $number\n";
                                        $count++;
                                }
                        }
                }

                $count = 0;
                print "\n[+] Número de Conexões por porta:\n";
                foreach my $number ( sort {$ports{$b} <=> $ports{$a}} keys %ports ) {
                        if ( $count <= 10 ) {
                                if ($number) {
                                        print "\t$ports{$number} $number\n";
                                        $count++;
                                }
                        }
                }

                my $total;
                foreach my $key ( keys %ports ) {
                        if ($key) {
                                $total += $ports{$key};
                        }
                }

                print "\n[+] TOTAL: $total\n";
        }
}

run();

Scripts recomendados

Script completo de firewall

Simple AdAway para Android

ADMINISTRANDO USUARIOS COM XDIALOG

Mantenha seu relógio ajustado 2.0

instalando o unbound dns no debian via script


  

Comentários
[1] Comentário enviado por removido em 14/12/2015 - 17:54h

Muito bom!
Estou tentando resolver um erro:

bash: ./conexao.sh: /usr/bin/perl^M: bad interpreter: Arquivo ou diretório não encontrado


----------------------------------------------------------------------------------------------------------------
# apt-get purge systemd

Encryption works. Properly implemented strong crypto systems are one of the few things that you can rely on. Unfortunately, endpoint security is so terrifically weak that NSA can frequently find ways around it. — Edward Snowden

[2] Comentário enviado por Mc.Eagle em 24/03/2019 - 16:20h

Aqui está a resposta amigo: http://www.dicas-l.com.br/arquivo/conversao_de_formatos_dos_to_unix_e_vice-versa.php#.XJfYU7XQi-9


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts