python - 编写一个在 ubuntu 终端上输入命令的 python 脚本?

标签 python ubuntu spyder gnome-terminal os.system

我在 ubuntu 16.04 上使用 spyder。我想编写一个脚本,在同一个 linux 终端中执行多个命令。

首先我想打开一个终端,然后ssh到另一台电脑,然后输入我的密码,然后继续输入命令。

我试过 os.system(command) 但这不会为我打开一个新终端,也不会运行我想要的命令。

os.system("gnome-terminal -e 'bash -c\"ssh blah blah blah; exec bash\"'") 可以,但我在尝试输入密码时卡住了。

如何在此上下文中使用 Python 脚本输入命令?

最佳答案

如果您希望使用密码自动连接并登录到 ssh 服务器,您可以使用 sshpass 输入密码,但最好使用 SSH key 。 Github 有一个关于如何生成 key 的很好的教程 here .

如果您希望在通过 SSH 连接到您的服务器后执行命令,您可以使用:os.system("ssh user@host 'command1 && command2'")

关于python - 编写一个在 ubuntu 终端上输入命令的 python 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48651556/

相关文章:

python - gRPC 是否能够为调用添加最大重试次数?

python - 使用 Google 日历了解事件在哪几天进行?

python - 分配结果变量的函数向我显示此错误

ruby-on-rails - 为什么我的 Upstart Unicorn Rails 服务器出现错误 "rackup file (config.ru) not readable"

r - doRedis 在 Ubuntu Linux、R 和 RStudio 中出现奇怪的套接字连接错误

python - 导入错误 : No module named _sqlite3 (even after doing eveything)

python - 无法安装mysql-python 无法构建或使用wheel

python - IDNA 不往返

python - 将参数传递给 Spyder 以调试文件

python - 使用 np.load 后关闭文件(使用 Spyder)