0

I've tried everything on this page and a few other Google searches, but I can't seem to get this to work.

Note* I looked up help on printing to test out the output from outfile.write, but I still can't get it formatted properly in testing.

My code looks like:

def ipfile():
    global ip_targets
    ip_target_file = raw_input('Place target file location here: ')
    try: ip_holder = open(ip_target_file,'r')
    except IOError:
        os.system('clear')
        print('Either I can\'t read that file, or it\'s not a file')
        os.system('sleep 3')
        return
    ip_targets = ip_holder.readlines()

def inbound_connections():   
    i = 0
    uri_ip_src = ''
    uri_ip_dst = ''
    while i < (len(ip_targets)):
        uri_ip_src_repeater = ('ip.src%3D%3D' + ip_targets[i])
        uri_ip_src = uri_ip_src + '||' + uri_ip_src_repeater
        uri_ip_dst_repeater = ('ip.dst%3D%3D' + ip_targets[i])
        uri_ip_dst = uri_ip_dst + '||' + uri_ip_dst_repeater
        i += 1          
    url = '&expression=' + "(" + (uri_ip_src[2:]) + ")" + "%26(" + (uri_ip_dst[2:]) + ")"
    #Write output to file functions
    outfile=open("jacobi_queries.txt","a")
    outfile.write("Same -> Same Connections " + str(datetime.datetime.now())[:16])
    outfile.write("\n")
    outfile.write(call_moloch + "/sessions?" + timestamp + url + "' 2>/dev/null &")
    outfile.write("\n")
    outfile.close()
    

The code works (sorry I'm leaving some variables out that are from other functions, but assume they work) however, my output has line breaks and I can't seem to find where I need to transform the "uri_ip_src" and "uri_ip_dst" to make it output on one line. My output looks as such:

Same -> Same Connections 2018-06-05 05:30
nohup firefox 'localhost:8005/sessions?&date=6&expression=(ip.src%3D%3D10.0.2.15
||ip.src%3D%3D10.0.0.1
||ip.src%3D%3D127.0.0.1
)%26(ip.dst%3D%3D10.0.2.15
||ip.dst%3D%3D10.0.0.1
||ip.dst%3D%3D127.0.0.1
)' 2>/dev/null &
iacob
  • 20,084
  • 6
  • 92
  • 119
Bloodvault
  • 33
  • 1
  • 7
  • Oh, and forgot to mention my text file is a return separated list, and that needs to stay. – Bloodvault Jun 06 '18 at 16:36
  • 1
    I don't understand… where does `print` come into play here?? Please read https://stackoverflow.com/help/mcve and edit your question accordingly. –  Jun 06 '18 at 16:46
  • Scroll to the bottom of the code. Outfile.write is what I'm having issues with (which I assume works similarly to print) – Bloodvault Jun 06 '18 at 17:01
  • In Python 2, `print` is a statement, which makes it completely different from `file.write`. And none of the `write` calls you show produce any of the output you are having problems with, so this question really doesn't make any sense. – Blckknght Jun 06 '18 at 17:02
  • Sorry bout that, edited post – Bloodvault Jun 06 '18 at 17:05
  • there are indendation errors all over this code. this can't _possibly_ be the code that produced that output. –  Jun 06 '18 at 17:09
  • I only saw 1 indent error, but ok fixed. I haven't posted on this site much. Thanks for being understanding. – Bloodvault Jun 06 '18 at 17:17
  • 1
    I don't understand how this is supposed to work, you give me feedback to fix my post, but then it gets down voted so now it won't be seen even though it is fixed. I had missed a few things, but now my problem won't get addressed. What's the deal? – Bloodvault Jun 06 '18 at 17:50

1 Answers1

0

Your issue is:

ip_targets = ip_holder.readlines()

has a newline character at the end of every line. You want to remove this character from the end of each element before adding it to uri_ip_src_repeater. This should work:

ip_targets = [i.rstrip('\n') for i in ip_targets]

But you could also cleanly grab them from the source without having to strip the unwanted characters after:

ip_targets = ip_holder.read().splitlines() 
iacob
  • 20,084
  • 6
  • 92
  • 119
  • But the second part "ip_targets = [i.rstrip('\n') for i in ip_targets]" added that after "ip_targets = ip_holder.readlines()" worked like a charm. Thanks for the help! – Bloodvault Jun 06 '18 at 18:41
  • I'd love to, but thanks to being down voted into oblivion by people above I don't have the ability to up vote your answer. – Bloodvault Jun 08 '18 at 09:30