0

*I want to print out ip addresses from textfile (solved)

****no ip address in the textfile and error message will be shown.** (solved)

I have attached my current codes at the bottom, can any one please help?**

** ****IP addresses in the textfile will look like this.**** **

192.168.12.1
192.168.12.28 

*****And the following is my current codes...*****

f=open('output.txt','r')
print "IP address is ", f.read()
f.close()
user8140256
  • 3
  • 1
  • 1
  • 5
  • 4
    Possible duplicate of [How do I print the content of a .txt file in Python?](https://stackoverflow.com/questions/18256363/how-do-i-print-the-content-of-a-txt-file-in-python) – jww Jun 10 '17 at 05:51

5 Answers5

1

Use file.readlines() inside a loop.

So, the Code will be:

f=open('output2.txt','r')
c=f.readlines()
for i in c :
     print ("IP address of attacker is ", i)
f.close()
Taku
  • 31,927
  • 11
  • 74
  • 85
Mr Sam
  • 981
  • 1
  • 8
  • 12
  • 1
    You shouldn’t use readlines to iterate with. Instead just use `f` ad the iterator – Taku Jun 10 '17 at 06:32
1

Get IP address from text file and check. See my code on git.

import socket
import re


f = open('ip_list.txt', 'r') #Text file with many ip address
o = f.read()
ip1 = re.findall( r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", o )
hosts = ip1
ports = [80]
for host in hosts:
    for port in ports:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.settimeout(1)
            result = s.connect_ex((host, port))
            if result == 0:
                    print("  [*] Port " + str(port) + " open!" + host)
            else: print("[+] CLOSE HOST " + host + ":" + str(port))
        except:
            pass
Mika Sundland
  • 18,120
  • 16
  • 38
  • 50
0
import sys
import os 
import time 
b='sudo tshark -i ens33 -Y "tcp contains "attack"" -T fields -e ip.src -a duration:20>output2.txt' 
a=os.popen(b) 
time.sleep(22)
with open(output2.txt,"r") as f:
    ip=f.read.split('\n')
for Ip in ip:
    print "IP address of attacker is ", Ip

You have to just split the contents of the file at every newline.

0

It is best to open the file in its own context with 'with'. This way it will be closed automatically after the last line has been reached. Then loop trough the lines and add your text before each line. Another upside of this solution is that you do not have to keep all IPs in memory. The IPs will be streamed one at a time.

This code will also print a message if no ip was found.

with open('output2.txt','r') as f:
    ip_not_found = True
    for line in f:
        ip_not_found = False
        print "IP address of attacker is {IP}".format(IP=line)
    if ip_not_found:
        print 'no ip address was found'
aifos324
  • 98
  • 1
  • 5
0
import ipaddress

ip_address_file = open('ip.txt', 'r')  # open text file with ip addresses
for i in ip_address_file:  # loop through the ip text file
    i = i.strip()  # read each line
    try:
        i = ipaddress.ip_address(str(i)) #validates either ip is ipv4 or 6

    except ValueError:  #catch error for invalid ip format
        print('invalid ip '.format(i))
        continue   # if line empty continue with loop
Tints
  • 11
  • 2