【发布时间】:2020-03-25 12:50:59
【问题描述】:
是否可以从 linux 服务器在 windows 服务器上执行文件(.exe、.py、.bat 等)。这些文件存储在 Linux 机器中。如果可能的话,我们如何通过制作 python 或 shell 脚本来实现它。
【问题讨论】:
标签: python-3.x windows powershell shell execution
是否可以从 linux 服务器在 windows 服务器上执行文件(.exe、.py、.bat 等)。这些文件存储在 Linux 机器中。如果可能的话,我们如何通过制作 python 或 shell 脚本来实现它。
【问题讨论】:
标签: python-3.x windows powershell shell execution
您可以使用 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()
【讨论】: