[英]Running local PowerShell script on remote SSH server
I am wondering if it is possible to run a local PowerShell script on the remote server that I SSH into.我想知道是否可以在我 SSH 进入的远程服务器上运行本地 PowerShell 脚本。 This preferably has to be done over a script like Paramiko where I can just run the Paramiko Python script and it does everything – SSH and run the local PowerShell script all at once.这最好通过像 Paramiko 这样的脚本来完成,我可以在其中运行 Paramiko Python 脚本,它可以完成所有操作 - SSH 并一次运行本地 PowerShell 脚本。
I've searched around and there doesn't seem to be any conclusive answers.我四处搜寻,似乎没有任何确凿的答案。
I've tried Python Paramiko and it does work.我试过 Python Paramiko,它确实有效。 However, all I can do is SSH into the remote server and copy the PowerShell script files over using SFTP to run the the script.但是,我所能做的就是将 SSH 复制到远程服务器并使用 SFTP 复制 PowerShell 脚本文件来运行脚本。 Is it possible to not use SFTP at all?是否可以根本不使用 SFTP? Where I can just run the script locally without sending it over?我可以在哪里只在本地运行脚本而不发送它?
Thanks.谢谢。
Codes that I tried:我尝试过的代码:
cmd4 = "powershell; echo './script.ps1' | powershell -File C:\\Users\\Desktop\\script.ps1"
cmd5 = "C:\\Users\\Desktop\\script.ps1"
stdin, stdout, stderr = ssh.exec_command(cmd4)
stdin.write(cmd5 + '\n')
stdin.flush()
time.sleep(5)
lines = stdout.readlines()
print (stdout.readlines())
I've tried printing the stdout
but it is empty.我试过打印stdout
,但它是空的。
Also tried this:也试过这个:
cmd5 = "C:\\Users\\Desktop\\script.ps1"
cmd6 = "powershell; echo './script.ps1;' | powershell -noprofile -noninteractive -command { $input | iex }"
stdin, stdout, stderr = ssh.exec_command(cmd6)
stdin.write(cmd5 + '\n')
stdin.flush()
time.sleep(5)
lines = stdout.readlines()
print (stdout.readlines())
Run powershell
on the server and feed the script to its input.在服务器上运行powershell
并将脚本提供给它的输入。
Just combine these two together:只需将这两者结合在一起:
A trivial (untested) example:一个简单(未经测试)的例子:
with open("C:\\Users\\Desktop\\script.ps1", "r") as f:
script = f.read() + "\n"
cmd = "powershell -noprofile -noninteractive -"
stdin, stdout, stderr = ssh.exec_command(cmd)
stdout.channel.set_combine_stderr(True)
stdin.write(script)
stdin.flush()
stdin.close()
lines = stdout.readlines()
print(lines)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.