我在脚本里用 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"
搜了半天没整明白
1
araraloren 2017-11-08 09:12:17 +08:00
|
2
crytis OP @araraloren 但是我要用 expect 输入密码
|
3
dongrenwen 2020-09-07 15:19:14 +08:00
ret=$(
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" ) cat <<EOF ${ret} EOF |
4
dongrenwen 2020-09-07 15:20:24 +08:00
```bash
ret=$( 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" ) cat <<EOF ${ret} EOF ``` |