1

i need some help please.

i write a little testing script for my daily work.

in that script i will connect to a server console.

here is my expect part:

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

the output looks like this:

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

you can see the connection works.

but i have a problems.

after pressing 3 times enter i want to close the connection.

if i want do this normal i will press "alt gr + . enter" an the connection is close.

server login: Connection to console closed.

but this doesn't work in expect as you can see above :(

do you have any ideas?

  • Are you trying to send the ~- to SSH or to the console SSH is connect to? Because ~ sequences are special to SSH, and SSH will grab those by default. – thrig Oct 10 '17 at 19:18
  • trying to send the ~- to the SSH connection to the console in the script. i hope you understand what i mean – Viperdriver2000 Oct 10 '17 at 20:00
  • What happens if you use spawn ssh -e none root:ttyS'"$port"'@'"$console"' ? – thrig Oct 10 '17 at 20:25
  • You could also try send "~B" to send a break to the remote, or if you dont use -e none as suggested by thrig then double the tilde character ~ if you want it to be received by the remote instead of acted on by ssh, as in send "~~B". Also, you might ensure your locale is C, in case the tilde you see is not an ascii tilde but some unicode lookalike. export LC_ALL=C. You can replace the tilde by the hex equivalent \x7e, eg send "\x7eB"`. – meuh Oct 10 '17 at 20:58
  • @thrig doesn't change anything – Viperdriver2000 Oct 10 '17 at 23:39
1

okay i answer my self because it is better to read.

i tested a little...i have delete the "expect eof" it seem to be better.

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"

with this code i get that output:

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

seems like the connection is closed correctly. i think so. i have test a normal connect. and i don't get a message like this:

*
* * * 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 :

this is what i get if a connection is not close correctly.

so i think this case is closed. thanks for your help

0

You need to send exit\r to close the ssh connection instead of ending the script at the eof.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.