显示正在运行的SSH命令的正确方法

时间:2019-02-06 18:46:58

标签: bash networking ssh

我试图通过SSH将命令数组传递给NID设备,然后将输出存储到变量中。我想不出一种有效的方法来显示输出中正在运行的命令。

我可以通过循环阵列并进行7个单独的SSH会话来使其工作。这很慢。

dfj = dfS1.join(dfS2, on='date_field_name', how='outer', rsuffix='_1')

预期:

n_info=$(sshpass -p "-PW-" ssh -q -o StrictHostKeyChecking=false admin@$nid_ip << EOF
        ${c_array[0]}
        ${c_array[1]}
        ${c_array[2]}
        ${c_array[3]}
        ${c_array[4]}
        ${c_array[5]}
        ${c_array[6]} 
        exit
EOF
)
        echo "$i"
        echo "$n_info"| sed "s/ACCEDIAN:>//g"

实际:

[show log]

log text 

log text 

log text 

[show config]

config text

config text

config text

命令不是静态的**

1 个答案:

答案 0 :(得分:0)

最简单/最简单的解决方案是在nid上运行echo命令。 假设标题不包含shell特殊字符:

sshpass -p "-PW-" ssh -q -o StrictHostKeyChecking=false admin@$nid_ip << EOF
    echo '[${h_array[0]}]'; ${c_array[0]}
    echo '[${h_array[1]}]'; ${c_array[1]}
    echo '[${h_array[2]}]'; ${c_array[2]}
    echo '[${h_array[3]}]'; ${c_array[3]}
    echo '[${h_array[4]}]'; ${c_array[4]}
    echo '[${h_array[5]}]'; ${c_array[5]}
    echo '[${h_array[6]}]'; ${c_array[6]} 
    exit
EOF | sed "s/ACCEDIAN:>//g"

如果将openssh用作ssh客户端,则它具有使用单个控件连接的选项。像这样:

REMOTE="admin@$nid"

CP_DIR="/tmp/sshctl/$$"
CP="$CP_DIR/"%L-%r@%h:%p"
mkdir -p "$CP_DIR"

sshpass -p "-PW-" \
ssh -q -nNf -o ControlMaster=yes -o ControlPath="${CP}" \
    -q -o StrictHostKeyChecking=false "$REMOTE"

# !!! check you have a connection !!!
# it may work to wrap "sshpass ..." with "if ! sshpass ...; then do_error; fi"

(
    for i in {0..6}; do
        echo "[$h_array[$i]}]"
        ssh -o ControlPath="${CP}" \
            -q -o StrictHostKeyChecking=false "$REMOTE" "${c_array[$i]}"
    done
    ssh -O exit -o ControlPath="$CP" "$REMOTE"
) | sed "s/ACCEDIAN:>//g"

rm -r "$CP_DIR"