2013-04-04 45 views
0

我有3個或更多的節點,我想要使用基於各個之間的RSSI之間的距離估計來執行本地化。如何遠程運行python腳本,但在本地使用結果列表?

要做到這一點,我試圖在接收到的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]包含由[0]從[0]接收的信號強度

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

有沒有一種方法,我可以通過使用paramiko(或其他)在SSH上共享產生的列表?

+0

這只是在每個主機上生成文件的代碼,您希望以某種方式發送給主機?如果是這樣,這個問題似乎沒有關係。 – abarnert 2013-04-04 20:54:19

+0

同時,是的,當然你可以使用'paramiko'通過SSH共享產生的列表。你是否想要['paramiko.SFTPClient'](http://www.lag.net/paramiko/docs/paramiko.SFTPClient-class.html)或其他什麼樣的代碼,或者你有不同的問題? – abarnert 2013-04-04 20:55:12

回答

0

有沒有一種方法,我可以通過使用paramiko(或其他)在SSH上共享產生的列表?

是的,你可以使用paramiko這個。 demo_sftp.py從源代碼分發應告訴你如何做到這一點,連同API docs。在各種主機上正確設置主機密鑰和授權密鑰,然後每臺遠程主機只需創建一個SFTPClient並在其上調用put即可。

是否這是最好設計完全取決於您的網絡設置和您的邏輯是什麼樣子。如果可行,最簡單的解決方案是將數據存儲在所有主機可以訪問的網絡文件系統(NFS,SMB,無論什麼)上。或者也許你想設置一個套接字服務器或0MQ或任何其他的東西,以便每個主機可以直接上傳它的數據,而不是首先將它存儲在一個文件中(這也意味着可以在接收數據時直接觸發目標,而不必設置文件系統手錶或其他)。或者......真的有很多選擇。

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

+0

put和get函數走哪個方向?如果我正在對遠程節點進行ssh,並且希望將文件從它複製/移動到本地節點,我可以使用put還是get? 例如是: sftp.put( 'rssi.txt', '/rssi.txt') 或 sftp.get( '/ rssi.txt', 'rssi.txt') 要複製rssi文件回我的本地節點? – 2013-04-05 13:40:29

+0

如果你有一個ssh到每個奴隸的主人,它將使用'get'。如果你有每個slave ssh給master,或者每個peer的ssh與其他peer有一個p2p網格,他們將使用'put'。正如[文檔](http://www.lag.net/paramiko/docs/)所說,「將遠程文件從SFTP服務器複製到本地主機」,而「put'複製」本地文件...到SFTP服務器「。 – abarnert 2013-04-05 18:29:09

相關問題