【问题标题】:running multiple commands in the same shell on server using python [duplicate]使用python在服务器上的同一个shell中运行多个命令[重复]
【发布时间】:2021-03-28 12:48:50
【问题描述】:

我想用同一个 shell 运行多个命令。

paramiko 库的执行命令不是同一个shell。

命令:

Ssh cloud-user@node -i /path/to/bcmt_rsa
Cat /etc/ssh/sshd_config
Change permitRootLogin yes to no
Restart sshd server
Log to file. 

我是 python 新手。我尝试了很多方法,但都不起作用

【问题讨论】:

  • 请向我们展示您的研究和尝试,并解释为什么您的研究或尝试都没有帮助。
  • 我已经尝试 cmd = ["ssh cloud-user@nodename","cat /etc/ssh/sshd_config"] 用于 cmd:proc = subprocess. Popen(命令,shell = True)过程。沟通()
  • 为什么不直接创建一个 shell 脚本并运行它呢?
  • 如何制作shell脚本。可能请分享一些想法
  • 来吧,真的吗?这是一个批处理文件。使用谷歌...

标签: python ssh paramiko fabric openssh


【解决方案1】:

不清楚的问题,但我有例子可以说你想编辑文件然后阅读它 有变量

command="echo data >> path_to_file/filename.txt" command+="cat path_to_file/filename.txt"

然后执行 ssh normal 并读取 stdout 行,或者您可以在命令中使用 && 喜欢command="echo data >file_path.txt && cat file_path.txt"

【讨论】:

    【解决方案2】:

    你想一次运行多个命令吗?我认为这是不可能的。但是你可以分割你的终端,并且可以在终端的不同分区中运行不同的命令。为此,你可以使用终结者,一个很棒的 bash终端。

    另外python是单线程语言。所以你不能在一个脚本中同时运行多个函数。Python会逐个函数执行

    【讨论】:

      猜你喜欢
      • 2017-10-12
      • 1970-01-01
      • 2023-04-07
      • 2021-05-17
      • 1970-01-01
      • 2015-04-02
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多