使用expect在远程计算机上运行python脚本

时间:2022-05-17 18:16:37

I have a python script on my local machine, which just prints "hello world". I want to run this script on a remote machine via ssh, using Expect. I am using Expect to automate entering the password. Entering the following commands to the command line runs the local script on the remote machine, however they require the user to enter the password:

我的本地机器上有一个python脚本,它只打印“hello world”。我想使用Expect通过ssh在远程计算机上运行此脚本。我使用Expect自动输入密码。在命令行中输入以下命令将运行远程计算机上的本地脚本,但是它们要求用户输入密码:

ssh abcd@111.222.333.44 python < hello .py

or

cat hello.py | ssh abcd@111.222.333.44 python -

The following .exp script automates entering the password, but runs a script which is already on the remote machine.

以下.exp脚本自动输入密码,但运行已在远程计算机上的脚本。

#!/usr/bin/expect
spawn ssh abcd@111.222.333.44 python < hello.py
set pass "1"
expect {
    password: { send "$pass\r"; exp_continue }
}

If hello.py file is not found on the remote machine, I get the following message: "bash: hello.py: No such file or directory". Can anyone help me please? Thanks in advance.

如果在远程计算机上找不到hello.py文件,我会收到以下消息:“bash:hello.py:没有这样的文件或目录”。有人可以帮我吗?提前致谢。

Edit: I do not want to first copy the script to the remote machine and then execute it. I tried and it works, but simply I do not want to run it this way.

编辑:我不想先将脚本复制到远程计算机然后执行它。我尝试了它的工作原理,但我只是不想以这种方式运行它。

1 个解决方案

#1


3  

< is not special in Expect/Tcl. spawn ssh abcd@111.222.333.44 python < hello.py is like you manually run ssh abcd@111.222.333.44 "python < hello.py" where the < is interpreted on the remote side.

<在expect tcl中并不特别。 spawn ssh abcd@111.222.333.44 python 就像你手动运行ssh>

You can write like this:

你可以像这样写:

spawn bash -c "ssh abcd@111.222.333.44 python < hello.py"

or

spawn bash -c "cat hello.py | ssh abcd@111.222.333.44 python"

#1


3  

< is not special in Expect/Tcl. spawn ssh abcd@111.222.333.44 python < hello.py is like you manually run ssh abcd@111.222.333.44 "python < hello.py" where the < is interpreted on the remote side.

<在expect tcl中并不特别。 spawn ssh abcd@111.222.333.44 python 就像你手动运行ssh>

You can write like this:

你可以像这样写:

spawn bash -c "ssh abcd@111.222.333.44 python < hello.py"

or

spawn bash -c "cat hello.py | ssh abcd@111.222.333.44 python"