繁体   English   中英

从linux服务器在windows上执行文件

[英]Executing file on windows from linux server

是否可以从 linux 服务器在 windows 服务器上执行文件(.exe、.py、.bat 等)。 这些文件存储在 Linux 机器中。 如果可能,那么我们如何通过制作 python 或 shell 脚本来实现它。

您可以使用 paramiko 创建到远程 Linux 服务器的 ssh 连接,下载可执行文件并运行它。 我有一些 python 代码,它们应该对你很有用:

import os

import paramiko


def create_ssh_connection(username, hostname, port=22, rsa_key=None, password=None):
    if rsa_key:
        rsa_key = paramiko.RSAKey.from_private_key_file(rsa_key, password=password)
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    ssh.connect(
        hostname=hostname,
        username=username,
        pkey=rsa_key,
        password=password,
        port=port
    )

    return ssh


def main():
    # Create SSH connection
    ssh = create_ssh_connection("username", "hostname", password="password")
    # Create SFTP connection
    sftp = ssh.open_sftp()

    # Get file from remote server
    remote_path = "/home/username/example.py"
    local_path = "example.py"
    sftp.get(remote_path, local_path)

    # Execute file
    os.system(local_path)


    # Delete file, if you only want to run it once
    os.remove(local_path)


if __name__ == '__main__':
    main()

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM