姬長信(Redy)

python – 如何通过行而不是变量中的字符进…


我有以下代码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()

数据现在包含该设备的iwlist wlan0扫描输出.

我想查看数据并获取地址及其相应的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))

匹配和匹配行是其中定义的函数,其中在文件中查找匹配字符的内容.

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

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