Sign in to follow this  
nilsonlino

Manipular Arquivo

Recommended Posts

Bom dia

Tenho um arquivo com varias linhas no seguinte formato

TCP 0.0.0.0:2569 0.0.0.0:0 LISTENING

Fiz um netstat na maquina e redirecionei para um arquivo, só que eu gostaria de comparar apenas as informações que estão na terceira coluna

TCP 0.0.0.0:2569 0.0.0.0:0 LISTENING

Alguém pode me ajudar como posso fazer isso.

Sou novato em python

Vlw galera

Share this post


Link to post
Share on other sites

Eu não sei o que você quer dizer com "comparar as informações" mas para ler um arquivo do tipo netstat separando os campos em cada linha é assim:

Supondo a seguinte saída do netstat:

tcp        0      0 127.0.0.1:5433              127.0.0.1:54294             ESTABLISHED
tcp        0   8760 66.240.211.91:80            62.183.20.244:61471         ESTABLISHED
tcp        0      0 127.0.0.1:5433              127.0.0.1:54100             ESTABLISHED
tcp        0      0 127.0.0.1:5433              127.0.0.1:39539             ESTABLISHED
tcp        0      0 66.240.211.91:80            122.152.128.48:55535        TIME_WAIT
tcp        0      0 127.0.0.1:5433              127.0.0.1:38813             ESTABLISHED
tcp        0      0 127.0.0.1:5433              127.0.0.1:38812             ESTABLISHED
tcp        0      0 127.0.0.1:5433              127.0.0.1:38814             ESTABLISHED
tcp        0      0 127.0.0.1:5432              127.0.0.1:43163             ESTABLISHED
tcp        0      0 127.0.0.1:5433              127.0.0.1:38811             ESTABLISHED
tcp        0      0 127.0.0.1:5433              127.0.0.1:38810             ESTABLISHED
tcp        0      0 127.0.0.1:43163             127.0.0.1:5432              ESTABLISHED
tcp        0      0 127.0.0.1:39539             127.0.0.1:5433              ESTABLISHED
tcp        0      0 127.0.0.1:5432              127.0.0.1:34489             ESTABLISHED
tcp        0      0 127.0.0.1:38811             127.0.0.1:5433              ESTABLISHED
tcp        0      0 127.0.0.1:38810             127.0.0.1:5433              ESTABLISHED
tcp        0      0 127.0.0.1:38814             127.0.0.1:5433              ESTABLISHED
tcp        0      0 127.0.0.1:38813             127.0.0.1:5433              ESTABLISHED
tcp        0      0 127.0.0.1:38812             127.0.0.1:5433              ESTABLISHED
tcp        0      0 127.0.0.1:33744             127.0.0.1:5432              ESTABLISHED
tcp        0      0 127.0.0.1:34489             127.0.0.1:5432              ESTABLISHED
tcp        0      0 127.0.0.1:5432              127.0.0.1:33744             ESTABLISHED
tcp        0      0 127.0.0.1:5433              127.0.0.1:51165             ESTABLISHED
Este program lê cada campo de cada linha:
f = open('netstat.txt', 'r')
for linha in f:
    campos = linha.split()
    Proto = campos[0]
    Recv_Q = campos[1]
    Send_Q = campos[2] 
    Local_Address = campos[3]               
    Foreign_Address = campos[4]             
    State = campos[5]
    print campos

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this