2

我有以下代码可以通过 ssh 连接到节点并从其他设备中查找 RSSI。

bot_ipv6 是 ssh 到的 ipv6 地址列表,脚本使用 pexpect 到 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 现在包含该iwlist wlan0 scan设备的输出。

我想浏览数据并仅获取地址及其相应的 RSSI。当命令可以在本地运行并输出到文件时,此代码有效:

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))

其中 match 和 matching line 是在其他地方定义的函数,它在文件中查找匹配字符以查找正在查找的内容。

我的问题是我不知道如何将数据输出到文件,但是如果我尝试像使用文件时那样遍历输出数据中的行:

for line in data:
        cell_line = match(line,"Cell ")
        if cell_line != None:
            cells.append([])
            line = cell_line[-27:]
        cells[-1].append(line.rstrip())

它不是行,而是遍历每个字符。

如何将输出打印到可以在本地打开并像以前一样扫描的文件,或者循环遍历数据中的行而不是单个字符?

这是数据的示例(为了得到它,我将打印数据放入脚本中,然后从输出中复制并粘贴到此处,希望不会丢失格式):

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
4

1 回答 1

13

用于str.splitlines将字符串拆分为行:

for line in data.splitlines():
    # Do things.
于 2013-05-06T17:21:03.763 回答