0

I am trying to grab 802.11 beacons and print them. Printing adds a line whereas stdout prefixes random spaces before/after the channel. Any ideas how to get the line flush to the left without a new line?

def beacon_sniff(pkt):
try:
    sta_oui = pkt.addr2[0:8] # Grab vendor OUI of BSSID
    if pkt.subtype == 8:
        if pkt.addr2 not in ap:
            if pkt.info != "\x00\x00\x00\x00\x00\x00\x00\x00": #Filter out hidden SSID's
                ap.append(pkt.addr2)
                #print '{0:<7} \t {1:<32} \t {2:<20} \t {3:<10} \t {4:<10}'.format(int(ord(pkt[5].info)), pkt.info, pkt.addr2, -(256-ord(pkt[0].notdecoded[-4:-3])), get_vendor(sta_oui))
                sys.stdout.write("{0:<7} \t {1:<32} \t {2:<20} \t {3:<10} \t {4:<10}".format(int(ord(pkt[5].info)), pkt.info, pkt.addr2, -(256-ord(pkt[0].notdecoded[-4:-3])), get_vendor(sta_oui)))
                sys.stdout.flush()
except:         
    pass

stdout:

6            TALKTALK-F148E8...
    6            BTHub3-Z3KP...
  1          BTWifi-X...

print:

6            TALKTALK-F148E8...

6            BTHub3-Z3KP...

1            BTWifi-X...
jwodder
  • 54,758
  • 12
  • 108
  • 124
Jordyc
  • 21
  • 3

2 Answers2

1

The "random" spaces are there because you're using \t in the output string. That character in most terminals will move to next multiple of 8.

This means that sometimes adding a single space will end up adding 7 more spaces.

6502
  • 112,025
  • 15
  • 165
  • 265
  • why would it be adding this at the beginning of the string? – Jordyc Jan 22 '15 at 23:03
  • You are not adding a newline `\n` at the end, therefore the output will wrap every 80 chars (or whatever is the horizontal size of the terminal). – 6502 Jan 22 '15 at 23:06
0

Go back to using the print function. To keep it from adding a new line at the end, put a comma at the end of your print statement.

This prints on two lines:

print "Hello"
print "world!"

But this prints it on the same line:

print "Hello",
print "world!"
mbomb007
  • 3,788
  • 3
  • 39
  • 68