shell脚本本地(linux)执行远程服务器命令

  • 前提条件
    配置ssh免密码登陆,如何配置请参照

    https://blog.csdn.net/liuxiaoming1109/article/details/89349470

  • 脚本

    #!/bin/bash
    echo "start
    ssh [email protected] << eeooff
    
    cd /home
    mkdir test1234
    
    echo "success"
    
    exit
    eeooff
    echo "end"
    

    说明:远程服务器执行的命令在【<< eeooff】至【eeooff】中间
    执行完命令【exit】退出服务器

  • 执行后终端输出

    start
    Pseudo-terminal will not be allocated because stdin is not a terminal.
    success
    end
    

    此时会出现 Pseudo-terminal will not be allocated because stdin is not a terminal.
    没有强迫症可以不用解决哈

    原因:字面意思是由于stdin不是终端,因此不会分配伪终端
    解决:在ssh后面添加-Tq参数

    ssh -Tq [email protected] << eeooff

    此时输出如下:

    start
    success
    end

你可能感兴趣的:(shell)