How can I make a for-loop loop through lines instead of characters in a variable?
I have the following code to ssh to a node and find the RSSIs from other devices.
bot_ipv6 is a list of ipv6 addresses to ssh to and the script uses pexpect to ssh.
for address in bot_ipv6:
session=spawn('ssh -6 root@'+address+'%wlan0')
#session.logfile = stdout
session.expect('password:')
session.sendline("123456")
session.expect(prompt)
session.sendline("iwlist wlan0 scan")
session.expect(prompt)
data=session.before
session.close()
data now contains the output of iwlist wlan0 scan
at that device.
I want to go through the data and get just the addresses and their corresponding RSSIs. This code works when the command can be ran locally and output to file:
with open("rssi.txt") as fd:
for line in fd:
cell_line = match(line,"Cell ")
if cell_line != None:
cells.append([])
line = cell_line[-27:]
cells[-1].append(line.rstrip())
for cell in cells:
level.append(matching_line(cell,"Quality=").split()[2].split('=')[1])
address.append(matching_line(cell,"Address: "))
scanned=dict(zip(address, level))
Where match and matching line are functions defined else where that look in the file for matching characters to what am looking for.
My problem is that I don't know how to output the data to a file but if I try to go through the lineds in the output data as I do when using the file:
for line in data:
cell_line = match(line,"Cell ")
if cell_line != None:
cells.append([])
line = cell_line[-27:]
cells[-1].append(line.rstrip())
instead of lines, it loops through each character.
How can I either print the output to a file that I can open locally and scan through as I did before or loop through lines in data instead of single characters?
here is a sample of what data looks like (to get it I put a print data into the script and then just copied it from the output and pasted it here, hopefully no formatting is lost):
iwlist wlan0 scan
wlan0 Scan completed :
Cell 01 - Address: 02:CA:FF:EE:BA:BE
Channel:11
Frequency:2.462 GHz (Channel 11)
Quality=47/70 Signal level=-63 dBm
Encryption key:off
ESSID:"nenenenenene-batman"
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
9 Mb/s; 12 Mb/s; 18 Mb/s
Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
Mode:Ad-Hoc
Extra:tsf=00000000f8083cfe
Extra: Last beacon: 72ms ago
IE: Unknown: 00136E656E656E656E656E656E652D6261746D616E
IE: Unknown: 010882840B160C121824
IE: Unknown: 03010B
IE: Unknown: 06020000
IE: Unknown: 32043048606C
Cell 02 - Address: D8:5D:4C:AF:C3:14
Channel:11
Frequency:2.462 GHz (Channel 11)
Quality=21/70 Signal level=-89 dBm
Encryption key:on
ESSID:"phome"
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
24 Mb/s; 36 Mb/s; 54 Mb/s
Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 48 Mb/s
Mode:Master
Extra:tsf=000000ff16ee018d
Extra: Last beacon: 3824ms ago
IE: Unknown: 000570686F6D65
IE: Unknown: 010882848B962430486C
IE: Unknown: 03010B
IE: Unknown: 2A0104
IE: Unknown: 2F0104
IE: Unknown: 32040C121860
IE: Unknown: 2D1A7C181BFFFF000000000000000000000000000000000000000000
IE: Unknown: 3D160B001700000000000000000000000000000000000000
IE: Unknown: DD090010180203F4010000
IE: Unknown: DD1E00904C337C181BFFFF000000000000000000000000000000000000000000
IE: Unknown: DD1A00904C340B001700000000000000000000000000000000000000
Use str.splitlines
to split a string into lines:
for line in data.splitlines():
# Do things.