我有一个用例,我需要在通过ssh连接到主机后执行命令。执行命令后,我需要执行Ctrl-D和Ctrl-M,以便我可以发出其他命令。
我尝试使用EOF,但它完全关闭了会话。
expect << EOF
spawn ssh -o StrictHostKeyChecking=no LocalCOMUser@$nodeIp -p $ssh_port
expect {
"password:" {}
timeout { send_user "Timed out in ssh connection" ;exit 1}
}
send "p@ssword\r"
expect {
">" {}
timeout { send_user "Timed out in ssh connection" ;exit 1}
}
set timeout 120
send "mml\r"
expect {
"<" {}
timeout { send_user "Timed out in ssh connection" ;exit 1}
}
send "$command1\r"
expect {
"<" {}
}
send "exit;\r"
expect {
">" {}
timeout { send_user "Timed out in ssh connection" ;exit 1}
}
send "exit\r"
EOF
需要一个以Ctrl-d方式执行操作的命令。
您可以通过发送^ D /␄字符来模拟按Ctrl-D:
send "\x04"