我对脚本编写非常陌生,它试图创建一个脚本来自动登录到远程服务器,而无需用户输入用户名和密码,因为脚本使用预设的用户名,服务器和密码进行代理跳转。
问题是登录成功后,当用户类型退出时,控制台上会显示此错误消息:
username@remote_server ~ $ exit
logout
Connection to remote_server closed.
expect: spawn id exp6 not open
while executing
"expect eof "
另一个问题是,当服务器不可访问时,我不确定如何正确退出脚本。我在下面的脚本中对我的尝试发表了评论。
下面是我的脚本:
#!/bin/bash
#log into remote server
clear
echo "Enter server name [ENTER]:" #prompt user for input
read INPUT
SERVER=`echo $INPUT`
clear
echo ""
echo "Logging into $SERVER, please wait ............"
echo ""
export SERVER #export global variable to expect script
#Take the gobal env variable from bash script above i.e. $SERVER and pass
it into the expect script below
/usr/bin/expect -c '
set password "password"
expect ":~$"
set timeout 10
log_user 0
spawn ssh -o StrictHostKeyChecking=no $env(SERVER)
expect "user_name@proxy_jump_server*:" #preset proxy jump server username and servername. User should not see this
send "$password\r"
expect {
timeout {send_user "server is unreachable"; exit}
eof { ; exit } # this is where I am not sure how to exit the code correctly if the server is unreachable
"* ~ $"
}
send "clear\r"
log_user 1
interact
expect eof '