如何远程运行python脚本,但在本地使用结果列表?

时间:2013-04-04 20:41:53

标签: python ssh

我有3个或更多节点,并且我想使用基于RSSI之间的距离估计来执行本地化。

为此,我尝试在它接收的RSSI的每个节点上创建一个列表,然后在所有节点之间共享这些列表。我有一个python脚本,它将所有RSSI以及接收和发送节点捕获到列表的子列表中。

代码如下:

import subprocess

def matching_line(lines, keyword):
    """Returns the first matching line in a list of lines. See match()"""
    for line in lines:
        matching=match(line,keyword)
        if matching!=None:
            return matching
    return None

def match(line,keyword):
    """If the first part of line (modulo blanks) matches keyword,
    returns the end of that line. Otherwise returns None"""
    line=line.lstrip()
    length=len(keyword)
    if line[:length] == keyword:
        return line[length:]
    else:
        return None

neighbour = [] #list of local node and neighbour's addresses
scanned = {}    # dictionary containing iwlist scan results Address: RSSI
single_localisation = [[],[],[]] #list of iwlist scan results at a single node. Node's address, transmitting node's address, RSSI 
all_localisation = [[],[],[]] #list of iwlist scan results from all nodes. Receiving node's address, transmitting node's address, RSSI 

#Save batctl o to file - batctl o shows all the nodes nearby participating in the mesh
Proc=subprocess.Popen("batctl o > bato.txt", shell=true)
Proc.wait()

#Populate neighbour list with addresses of neighbouring nodes
with open("bat.txt") as fd:
    fd.readline() #skip column headings
    for line in fd:
        neighbour.append(line.split()[0])

#Add local node's Address to neighbour list for later comparison
neigbour.append( subprocess.check_output("ip link show wlan0 | grep link | awk '{print $2}'",shell=True).strip())

#Scan wlan2 and save to file
Proc=subprocess.Popen("iwlist wlan2 scan | awk '/ESSID/ {print $1} /level/ {print $3}' > rssi.txt", shell=true)
Proc.wait()

#Populate scanned list with all MAC addresses and RSSIs from file
cells=cells[1:]

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

#Test if MAC address in scanned list matches MAC address in neighbour list (and is therefore a meshed node)
for s in scanned:
    if s in neighbour:
    #if it does make an entry in localisation for it
        localisation[0].append( subprocess.check_output("ip link show wlan0 | grep link | awk '{print $2}'",shell=True).strip())
        localisation[1].append(s)
        localisation[2].append(scanned[s])

所以

  • 本地化[0]包含本地节点的MAC
  • 本地化[1]包含发送节点的MAC
  • 定位[2]包含从[1]到[0]
  • 接收的信号强度

我想以某种方式合并所有节点上的所有本地化列表,以创建每个节点具有的一个大型列表。

有没有办法可以使用paramiko(或替代方法)通过SSH共享生成的列表?

1 个答案:

答案 0 :(得分:0)

  

有没有办法可以使用paramiko(或替代方法)通过SSH共享生成的列表?

是的,您可以使用paramiko。来自源代码分发的demo_sftp.py应该与API docs一起向您展示如何执行此操作。在各种主机上正确设置主机密钥和授权密钥,然后每个远程主机只需创建一个SFTPClient并在其上调用put

这是否是最佳设计完全取决于您的网络设置方式以及逻辑外观。如果可行的话,最简单的解决方案是将数据存储在所有主机都可以访问的网络文件系统(NFS,SMB等)上。或者您可能想要设置套接字服务器或0MQ或其他任何东西,以便每个主机可以直接上传其数据,而不是首先将其存储在文件中(这也意味着目标可以在收到数据时直接触发,而不是必须设置文件系统监视或其他东西)。或者......真的,有很多选择。

您还应该考虑是否正在尝试构建客户端 - 服务器系统(每个人都上传到一个“主”,然后将更新发送到所有“从”主机)或p2p系统(每个人上传到其他所有人)。对于后者,使用简单的DHT设计可能会更好,而不是试图让每个人保持同步。