是否可以从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()