簡體   English   中英

如何遠程運行python腳本,但在本地使用結果列表?

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

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

所以

  • localisation [0]包含本地節點的MAC
  • localisation [1]包含發送節點的MAC
  • localization [2]包含[0]從[1]接收的信號強度

我想以某種方式合並所有節點上的所有本地化列表,以創建每個節點具有的一個大列表。

有沒有辦法可以使用paramiko(或替代方法)通過SSH共享生成的列表?

有沒有辦法可以使用paramiko(或替代方法)通過SSH共享生成的列表?

是的,您可以為此使用paramiko 源代碼發行版中的demo_sftp.py應該與API docs一起向您展示如何做到這一點。 在正常的各個主機設置您的主機密鑰和授權密鑰,那么每個遠程主機只需要創建一個SFTPClient ,並呼吁put它。

這是否是最佳設計,完全取決於您的網絡設置方式和邏輯外觀。 如果可行,最簡單的解決方案是將數據存儲在所有主機都可以訪問的網絡文件系統(NFS,SMB等)上。 或者,也許您想設置一個套接字服務器或0MQ或其他任何東西,以便每個主機都可以直接上傳其數據,而不是首先將其存儲在文件中(這也意味着可以在收到數據后直接觸發目標,而不需要設置文件系統監視之類的東西。 或者……確實,有很多選擇。

您還應該考慮是否要構建客戶端服務器系統(每個人都上載到一個“主”,然后將更新發送到所有“從屬”主機)或p2p系統(每個人都上載到其他的人) 。 對於后者,使用簡單的DHT設計可能會更好,而不是嘗試使所有人保持同步。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM