简体   繁体   English

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

[英]How can I run a python script remotely but use the resulting list locally?

I have 3 or more nodes, and I want to perform localisation using an estimation of the distances between each based upon the RSSIs between. 我有3个或更多节点,并且我想使用基于两个节点之间RSSI的每个节点之间的距离估计来执行定位。

To do this I am trying to create a list on each node of the RSSIs it receives then share these lists between all the nodes. 为此,我尝试在接收的RSSI的每个节点上创建一个列表,然后在所有节点之间共享这些列表。 I have a python script which captures all the RSSIs along with the receiving and transmitting node into sublists of a list. 我有一个python脚本,可将所有RSSI以及接收和发送节点捕获到列表的子列表中。

The code is as following: 代码如下:

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

So 所以

  • localisation[0] contains the local node's MAC localisation [0]包含本地节点的MAC
  • localisation[1] contains the transmitting node's MAC localisation [1]包含发送节点的MAC
  • localisation[2] contains the signal strength received from [1] by [0] localization [2]包含[0]从[1]接收的信号强度

I want to somehow merge all the localisation lists across all the nodes to create one large list which each node has. 我想以某种方式合并所有节点上的所有本地化列表,以创建每个节点具有的一个大列表。

Is there a way I could possibly share the produced list over SSH using paramiko (or alternative)? 有没有办法可以使用paramiko(或替代方法)通过SSH共享生成的列表?

Is there a way I could possibly share the produced list over SSH using paramiko (or alternative)? 有没有办法可以使用paramiko(或替代方法)通过SSH共享生成的列表?

Yes, you can use paramiko for this. 是的,您可以为此使用paramiko demo_sftp.py from the source distribution should show you how to do it, together with the API docs . 源代码发行版中的demo_sftp.py应该与API docs一起向您展示如何做到这一点。 Set up your host keys and authorized keys properly on the various hosts, then each remote host just needs to create an SFTPClient and call put on it. 在正常的各个主机设置您的主机密钥和授权密钥,那么每个远程主机只需要创建一个SFTPClient ,并呼吁put它。

Whether this is the best design depends entirely on how your network is set up and what your logic looks like. 这是否是最佳设计,完全取决于您的网络设置方式和逻辑外观。 The easiest solution, if it's feasible, would be to just store the data on a networked filesystem (NFS, SMB, whatever) that all of the hosts can access. 如果可行,最简单的解决方案是将数据存储在所有主机都可以访问的网络文件系统(NFS,SMB等)上。 Or maybe you want to set up a socket server or 0MQ or whatever so that each host can upload its data directly instead of storing it in a file in the first place (which also means that the target can be triggered directly on receipt of data, instead of having to set up a filesystem watch or something). 或者,也许您想设置一个套接字服务器或0MQ或其他任何东西,以便每个主机都可以直接上传其数据,而不是首先将其存储在文件中(这也意味着可以在收到数据后直接触发目标,而不需要设置文件系统监视之类的东西。 Or… really, there are many options. 或者……确实,有很多选择。

You should also consider whether you're trying to build a client-server system (where everyone uploads to one "master", which then sends updates to all the "slave" hosts) or a p2p system (where everyone uploads to everyone else). 您还应该考虑是否要构建客户端服务器系统(每个人都上载到一个“主”,然后将更新发送到所有“从属”主机)或p2p系统(每个人都上载到其他的人) 。 For the latter, you might be better off with a simple DHT design instead of trying to keep everyone in sync. 对于后者,使用简单的DHT设计可能会更好,而不是尝试使所有人保持同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM