我需要一些帮助。
我为我的日常工作编写了一个小测试脚本。
在该脚本中我将连接到服务器控制台。
这是我的期望部分:
export TMPEXPECT=$(mktemp /home/user/tmp/expect.XXXXXX)
chmod 700 $TMPEXPECT
echo "Verbinde auf console..."
cat <<< '#!/usr/bin/expect -f
spawn ssh root:ttyS'"$port"'@'"$console"'
expect { "*gnore*" { send "i\r\r"} }
expect { "\n" {send "\r"} }
expect { "\n" {send "\r"} }
sleep 2
expect { "\n" {send "\r"} }
expect { "\n" {send "~.\r"} }
expect eof
' > $TMPEXPECT
$TMPEXPECT
输出如下所示:
spawn ssh root:ttyS33@console
A non-empty Data Buffering File was found. Choose which action
should be performed ( (I)gnore, (D)isplay, (E)rase or (S)how and erase ) : I
Welcome to Special Dedicated-Server Linux (x86_64) Version 5.5.1 - Kernel 4.4.77-9.1.x86_64 (ttyS0).
server login:
Welcome to Special Dedicated-Server Linux (x86_64) Version 5.5.1 - Kernel 4.4.77-9.1.x86_64 (ttyS0).
server login:
Welcome to Special Dedicated-Server Linux (x86_64) Version 5.5.1 - Kernel 4.4.77-9.1.x86_64 (ttyS0).
server login: ~.
你可以看到连接有效。
但我有一个问题。
按 3 次 Enter 后我想关闭连接。
如果我想正常执行此操作,我将按“alt gr + .enter”连接关闭。
server login: Connection to console closed.
但这在预期中不起作用,正如您在上面看到的:(
你有什么想法?
答案1
好吧,我回答自己,因为阅读更好。
我测试了一下...我删除了“expect eof”,它似乎更好。
cat <<< '#!/usr/bin/expect -f
spawn ssh -e none root:ttyS'"$port"'@'"$console"'
expect {
"*regular*" { send "1\r"}
"default" { send "i\r"}
}
expect {
"*gnore*" { send "i\r"}
}
expect {
"*\n*" {send "\r"}
}
sleep 5
expect {
"*\n*" {send "\r"}
}
sleep 2
expect {
"*\n*" {send "\r"}
}
sleep 2
' > $TMPEXPECT
$TMPEXPECT
echo ""
echo ""
echo "this is a test after the expect and expect eof part"
通过这段代码我得到了输出:
spawn ssh -e none root:ttyS33@console
A non-empty Data Buffering File was found. Choose which action
should be performed ( (I)gnore, (D)isplay, (E)rase or (S)how and erase ) : I
Welcome to Special Dedicated-Server Linux (x86_64) Version 5.6.0 - Kernel 4.4.89-9.1.x86_64 (ttyS0).
server login:
Welcome to Special Dedicated-Server Linux (x86_64) Version 5.6.0 - Kernel 4.4.89-9.1.x86_64 (ttyS0).
server login:
this is a test after the expect and expect eof part
似乎连接已正确关闭。我想是这样。我已经测试了正常连接。我没有收到这样的消息:
*
* * * ttyS33 is being used by (root) !!!
*
1 - Initiate a regular session
2 - Initiate a sniff session
3 - Send messages to another user
4 - Kill session(s)
5 - Quit
Enter your option :
这是如果连接未正确关闭时我得到的结果。
所以我认为这个案子已经结束了。感谢您的帮助
答案2
您需要发送exit\r
以关闭 ssh 连接,而不是在 eof 处结束脚本。