期待されるスクリプトに特殊文字 "~-enter" を送信します。

期待されるスクリプトに特殊文字

私は助けが必要です。

私は日常業務のために小さなテストスクリプトを書いた。

このスクリプトではサーバーコンソールに接続します。

私が期待する部分は次のとおりです。

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: ~.

接続が機能していることがわかります。

ところで質問があります。

Enterを3回押した後、接続を終了したいと思います。

これを正常に行うには、「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 :

接続が正しく閉じられないと、この現象が発生します。

だから事件は終結したようです。ご協力ありがとうございます

おすすめ記事