如何远程运行python脚本,但在本地使用生成的列表?

如何远程运行python脚本,但在本地使用生成的列表?,python,ssh,Python,Ssh,我有3个或3个以上的节点,我想根据每个节点之间的RSSI估计每个节点之间的距离来执行定位 为此,我尝试在它接收的RSSIs的每个节点上创建一个列表,然后在所有节点之间共享这些列表。我有一个python脚本,它将所有RSSI以及接收和发送节点捕获到列表的子列表中 代码如下: import subprocess def matching_line(lines, keyword): """Returns the first matching line in a list of lines.

我有3个或3个以上的节点,我想根据每个节点之间的RSSI估计每个节点之间的距离来执行定位

为此,我尝试在它接收的RSSIs的每个节点上创建一个列表,然后在所有节点之间共享这些列表。我有一个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]从[1]接收到的信号强度
我想以某种方式合并所有节点上的所有本地化列表,以创建一个每个节点都具有的大型列表

有没有一种方法可以使用paramiko(或其他方法)通过SSH共享生成的列表

有没有一种方法可以使用paramiko(或其他方法)通过SSH共享生成的列表

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

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


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

这就是在每个主机上生成文件的代码,您希望以某种方式将其发送到主机吗?如果是这样,它似乎与问题无关。同时,是的,您当然可以使用
paramiko
通过SSH共享生成的列表。您是想要示例代码还是其他什么,还是有不同的问题?put和get函数朝哪个方向发展?如果我正在ssh连接到远程节点,并希望将文件从远程节点复制/移动到本地节点,我是否使用put或get?例如:sftp.put('/rssi.txt','/rssi.txt')或sftp.get('/rssi.txt',rssi.txt')将把rssi文件复制回我的本地节点?如果您有一个主机,ssh连接到每个从机,它将使用
get
。如果每个从ssh连接到主ssh,或者每个对等ssh连接到其他对等ssh的p2p网格,则它们将使用
put
。例如,
get
将“远程文件…从SFTP服务器复制到本地主机”,而
put
将“本地文件…复制到SFTP服务器”。