我在脚本里用 expect ssh 到机器上执行命令,比如 ls,如何把这个结果拿回来在脚本里使用?
expect -c "
spawn ssh -f -o StrictHostKeyChecking=no [email protected] ls
expect {
\"*assword\" {set timeout 6000; send \"123456\`\n\"; exp_continue ; sleep 30; }
\"yes/no\" {send \"yes\n\"; exp_continue;}
}
interact"
搜了半天没整明白
ssh 应该可以获取执行的程序的返回值
try this: echo $(ssh -f -o StrictHostKeyChecking=no [email&ls)
但是我要用 expect 输入密码
ret=$(
expect -c "
spawn ssh -f -o StrictHostKeyChecking=no [email&ls
expect {
\"*assword\" {set timeout 6000; send \"123456\`\n\"; exp_continue ; sleep 30; }
\"yes/no\" {send \"yes\n\"; exp_continue;}
}
interact"
)
cat <
${ret}
EOF
```bash
ret=$(
expect -c "
spawn ssh -f -o StrictHostKeyChecking=no [email&ls
expect {
\"*assword\" {set timeout 6000; send \"123456\`\n\"; exp_continue ; sleep 30; }
\"yes/no\" {send \"yes\n\"; exp_continue;}
}
interact"
)
cat <
${ret}
EOF
```