【问题标题】:Executing file on windows from linux server从linux服务器在windows上执行文件
【发布时间】:2020-03-25 12:50:59
【问题描述】:

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

【问题讨论】:

    标签: python-3.x windows powershell shell execution


    【解决方案1】:

    您可以使用 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()
    
    

    【讨论】:

    • 谢谢,但我想将文件从 linux 执行到 windows。
    • 那么您的文件位于Linux服务器上,您想连接Windows服务器并在Windows服务器上执行文件?
    猜你喜欢
    • 2011-03-14
    • 2013-09-13
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多