Writing IP addresses to file, each IP on a different line using Python

Asked

Viewed 187 times

0

from scapy.all import *


pkts = rdpcap("lalalao.pcap")

for p in pkts:
##  print p.time


        if IP in p: #if packet has IP layer
                    src_ip = p[IP].src
                    dest_ip = p[IP].dst
                    print src_ip

                    f = open('IP_src.txt', 'a+')
                    for ip in src_ip:
                            f.writelines(ip)

                    f.close()

The above code correctly prints src_ip one per line:

216.58.202.229
216.58.202.229
192.168.1.3
216.58.202.229
216.58.202.229
192.168.1.3
216.58.202.229
216.58.202.229
192.168.1.3
216.58.202.229
192.168.1.3
216.58.202.229

But when saving to Ip_src.txt file, the file gets all messy , all on the same line. How to save src_ip in the file one per line? When I pass this file to another program read, is it any problem for src_ip to be one per line? There is a difference between python 2 and python 3 at this point?

  • f. write('{} n'.format(ip)), try this Ed

1 answer

2


Failed to insert line break " n", you can use .write("\n") for example:

from scapy.all import *


pkts = rdpcap("lalalao.pcap")

for p in pkts:
    ##  print p.time


    if IP in p: #if packet has IP layer
        src_ip = p[IP].src
        dest_ip = p[IP].dst
        print src_ip

        f = open('IP_src.txt', 'w')
        for ip in src_ip:
            f.write(ip + "\n")

        f.close()
  • ,When I pass this file to another program read, it gives some problem src_ip be one per line?

  • writelines() does not work?

  • the code here did not work... Ips were saved vertically: http://imgur.com/a/97bGW

  • You probably won’t have trouble reading another program, in fact the only problem that can occur is from Ncode, but I believe that tbm won’t happen. Regarding the code, try to do f.writelines(ip) and soon after f.write("\n")

  • If he still keeps breaking all the ip per character, you will need to put f. write(" n") only when he finishes the for ip in src_ip:

  • only worked with: "If he keeps breaking all the ip per character, you’ll need to put the f. write(" n") only when it finishes the for ip in src_ip".

  • thanks. How to make the file "zeroed"/clean every time the program runs?

  • a yes, I was left wondering if it was coming the ip character array or an ips array

  • to clean the file and overwrite you need to set w in reading f = open('IP_src.txt', 'w')

  • The answer is wrong. 'Writelines" expects a string - if you pass a string, it is an appropriate call to write several lines that are already prepared (with " n" at the end) in a list. Since Python interacts strings by pulling one character at a time, this error may go unnoticed - but you will be making much more calls to the system internally than necessary. f.writelines(ip) for f.write(ip + "\n") (ready, the line break is already inserted along with the ip) - And, when tidying this, take advantage and arrange the identation.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.