V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
keenking
V2EX  ›  问与答

Python 执行 Linux 命令的一个小问题,求大佬赐教

  •  
  •   keenking · 2018-08-22 09:33:58 +08:00 · 1527 次点击
    这是一个创建于 2046 天前的主题,其中的信息可能已经有所发展或是发生改变。

    python 连接 linux 服务器操作 输入命令后,linux 执行命令后需要按 enter 键才能继续执行 请问,enter 键的命令是什么? 下面是代码 ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname='21.221.168.141', port=22, username='tomcat', password='tomcat123') stdin, stdout, stderr = ssh.exec_command('cd /yyy/abc;sh shutdown-box.sh abc;sh launch-box.sh') log = stdout.read() print(log)

    解释下 comand: cd /yyy/abc (先进入 abc 文件夹) sh shutdown-box.sh abc (结束 abc 服务) sh launch-box.sh (启动 abc 服务,问题出现在这里,执行整个 launch 启动,需要人工按下“ enter ”键,但是我想在这个脚本让他执行到这里自动回车,请问有大佬知道需要怎么输入命令,会自动执行 enter 吗?)

    4 条回复    2018-08-22 11:49:13 +08:00
    jeremaihloo
        1
    jeremaihloo  
       2018-08-22 09:49:03 +08:00
    你这个格式,没人想看下去呀
    zhouquan03
        2
    zhouquan03  
       2018-08-22 09:53:10 +08:00
    enter 就是执行动作!
    bomb77
        3
    bomb77  
       2018-08-22 11:37:56 +08:00
    一般来说 exec_command 会直接执行命令不需要额外传输回车
    如果你这里一定要回车,可能和 launch-box.sh 脚本有关?
    或者尝试下命令后面加上'\r\n'
    baelish
        4
    baelish  
       2018-08-22 11:49:13 +08:00
    expect
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4414 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 05:32 · PVG 13:32 · LAX 22:32 · JFK 01:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.