通过ssh和python从远程机器获取Eflow状态:不执行powershell命令

问题描述 投票:0回答:1

我在远程计算机上运行 EFlow,并且想要轮询状态以确保它正在运行。

如果我通过 SSH 连接到机器,就像运行命令一样简单

powershell Get-EflowVm

然而,事实证明,在 python 脚本中自动执行此操作更加困难。一般来说,通过 python 的 ssh 工作起来就像一个魅力,我只是无法让它运行这个命令。不知何故,特别是当运行此命令(而不是通过 python 和 ssh 运行其他 powershell 命令)时,它会停止,不执行该命令,并最终超时。

我尝试了几种不同的方法来进行此操作,下面是一些示例:

import subprocess

r = subprocess.Popen(f"sshpass -p <pw> ssh <user>@<host> ""powershell Get-EflowVm", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate(timeout=60)

或与帕里科:

import paramiko

host = <host>
user = <user>
pw = <pw>

client = paramiko.client.SSHClient()

client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client.connect(host, port=22, username=user, password=pw, timeout=60)

stdin, stdout, stderr = client.exec_command("powershell Get-EflowVm")

client.close()

同样,无论哪种情况,它都会挂起并且不提供任何响应。其他 powershell 命令可以工作,如果我手动 ssh 进入机器并执行 powershell 命令,我会得到很好的响应。

任何帮助将不胜感激。

python powershell ssh azure-iot-edge
1个回答
0
投票

我能够解决这个问题,但并不真正知道实践与我已经在做的事情相比有什么不同。不过,它确实有效。

import threading
import paramiko

class ssh:
    shell = None
    client = None
    transport = None

    def __init__(self, address, username, password):
        print("Connecting to server on ip {}".format(address))
        
        self.fulldata = ""
        self.running = False
        
        self.client = paramiko.client.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
        
        self.client.connect(address, username=username, password=password, look_for_keys=False)
        
        self.transport = paramiko.Transport((address, 22))
        
        self.transport.connect(username=username, password=password)

        thread = threading.Thread(target=self.process)
        thread.daemon = True
        thread.start()

    def close_connection(self):
        if(self.client != None):
            self.client.close()
            self.transport.close()
            self.running = False

    def open_shell(self):
        self.shell = self.client.invoke_shell()

    def send_shell(self, command):
        if(self.shell):
            self.shell.send(command + "\n")
        else:
            print("Shell not opened.")

    def process(self):
        self.running = True
        
        while self.running:
            # Print data when available
            if self.shell is not None and self.shell.recv_ready():
                alldata = self.shell.recv(1024).decode()
                while self.shell.recv_ready():
                    alldata += self.shell.recv(1024)
                
                self.fulldata = self.fulldata + str(alldata)
                

def get_eflow_status(config):
    sshUsername = config["username"]
    sshPassword = config["password"]
    sshServer = config["address"]

    connection = ssh(sshServer, sshUsername, sshPassword, debug_logger)
    connection.open_shell()
    connection.send_shell('powershell "Get-EflowVM | Select -ExpandProperty VmPowerState | Format-List"\r')
    time.sleep(15)
    ret = connection.fulldata
    connection.close_connection()
    if "Running" in ret:
        return True
    else:
        return False
© www.soinside.com 2019 - 2024. All rights reserved.