简体   繁体   English

在远程机器上运行python脚本功能

[英]Run python script function in remote machine

I have script in remote device and I want to run specific function in python script in remote device 我在远程设备中有脚本,我想在远程设备中的python脚本中运行特定功能

remote device has below script: 远程设备具有以下脚本:

#connect.py

class ConnectDevice:
    def __init__(self, ip):
        connect.Device(ip)
    def get_devicestate(self):
        state = show.Device_State  
        return state

configured password less connection from source machine to remote machine 从源计算机到远程计算机的配置的无密码连接

function get_devicestate return up or down. 函数get_devicestate返回上或下。

How to get get_devicestate output from source machine. 如何从源计算机获取get_devicestate输出。 source machine has below script: 源计算机具有以下脚本:

import os
import sys
import time
import getpass
import errno
import json
import subprocess
import threading
from subprocess import call
from subprocess import check_output
call(["ssh", "1.1.1.1", "\"python Connect.py\""])#This is just example how to run script from source to remote. Need help how to run function get_devicestate and get value. 

At a first glance , it seems that connect.py has got more code than you have pasted in your question. 乍看之下,似乎connect.py拥有比您粘贴在问题中更多的代码。 Anyways, assuming connect.py does not require any input parameters to run, simply use subprocess's check_output method to get the stdout message and store it in a variable for further use. 无论如何,假设connect.py不需要运行任何输入参数,只需使用子进程的check_output方法获取stdout消息并将其存储在变量中以备将来使用。

from subprocess import check_output
out = check_output(["ssh", "1.1.1.1", "\"python Connect.py\""])

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

相关问题 如何使用 ssh 在远程机器上运行 python 脚本 - How run python script in remote machine with ssh 如何在远程机器上运行本地python脚本 - how to run local python script on remote machine Python脚本来SSH远程计算机并在cron作业中运行此脚本 - Python script to ssh remote machine and run this script in cron job 如何在多台远程机器上运行python脚本? 不安装脚本? - How to run python script on multiple remote machine? without installing the script? 在没有sudo特权的情况下在远程计算机上运行python脚本 - Run a python script on a remote machine without sudo previlage 在不使用SCP的情况下在远程计算机上运行python脚本 - Run python script in remote machine without using SCP 有没有办法在远程机器上运行python脚本而不发送它? - Is there any way to run a python script on remote machine without sending it? 在存储在远程服务器上的本地计算机上运行 python 脚本 - run python script on local machine stored on remote server 通过 ssh 从远程 linux 机器运行本地 python 脚本 - run local python script from remote linux machine over ssh 如何通过Powershell从远程计算机异步运行Python脚本? - How to Run Python Script from Remote Machine via Powershell Asynchronously?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM