我想编写一个程序(在Windows 7上的Python 3.x中),它通过ssh在远程shell上执行多个命令.在查看了paramikos的exec_command()函数之后,我意识到它不适合我的用例(因为在执行命令后通道被关闭),因为命令依赖于环境变量(由先前的命令设置)而不能连接到一个exec_command()调用,因为它们将在程序中的不同时间执行.
因此,我想在同一个通道中执行命令.我研究的下一个选项是使用paramikos的invoke_shell()函数实现交互式shell:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=user, password=psw, port=22)
channel = ssh.invoke_shell()
out = channel.recv(9999)
channel.send('cd mivne_final\n')
channel.send('ls\n')
while not channel.recv_ready():
time.sleep(3)
out = channel.recv(9999)
print(out.decode("ascii"))
channel.send('cd ..\n')
channel.send('cd or_fail\n')
channel.send('ls\n')
while not channel.recv_ready():
time.sleep(3)
out = channel.recv(9999)
print(out.decode("ascii"))
channel.send('cd ..\n')
channel.send('cd simulator\n')
channel.send('ls\n')
while not channel.recv_ready():
time.sleep(3)
out = channel.recv(9999)
print(out.decode("ascii"))
ssh.close()
Run Code Online (Sandbox Code Playgroud)
这段代码存在一些问题:
我对这种"非决定论"感到困惑,非常感谢你的帮助.
我有一堆C# DLL,我试图使用pythonnet库用 python 包装它们。
我注意到 DLL 正在使用Application.StartupPath,在 python 的情况下是 python.exe 所在的位置(据我所知)。这对我来说是个问题,因为这意味着我需要将所有 DLL 复制到安装 python 的文件夹中(o/w DLL 引发异常)。如果我理解正确,则无法使用pythonnet库修改Application.StartupPath。此外,我没有任何访问 DLL 源代码的权限,但我知道他们正在使用Application.StartupPath(因为我使用了dotPeak)。从头开始构建 DLL 也是不可能的,因为构建非常复杂并且需要很多依赖项。
如果有人能帮助我了解如何避免将 DLL 文件夹内容复制到我的 python 解释器的路径,我会很高兴。