Help us understand the problem. What is going on with this article?

sshのパスワードを自動入力するやーつ

いつも諦めさせられる、sshのtimeoutがない問題。これならなんとかなるはず!

#!/bin/sh

USER=root
DOMAIN=example.com
PW="123456789"

expect -c "
set timeout 5
spawn ssh -p 22 ${USER}@${DOMAIN}
expect {
    \"(yes/no)?\" {
        send \"yes\n\"
        exp_continue
    }
    \"password:\" {
        send \"${PW}\n\"
    }
}
expect \" #\"
send \"ps aux\n\"
expect \" #\"
send \"exit 0\n\"

キモは expect \" #\"# です。これはpromptの入力待ちになる直前の文字になってます。shellの種類、PS1変数によって変わるところ

参考までに、動いたサーバのlogin後promptはこれです

[root@test.example.com:~]# <------- # の後にspaceがあって、そこに仕掛けている
[root@test.example.com:~]# 入力するときはここから入力される

実行例

$ bash this.sh
spawn ssh -p 22 root@test.example.com
root@test.example.com's password:
[root@test.example.com:~]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.0  19352  1532 ?        Ss   Aug16   0:03 /sbin/init
...
省略
...
root      88134  0.0  0.0  93868  3992 ?        Ss   21:20   0:00 sshd: root@pts/0
root      88138  0.0  0.0 106428  1840 pts/0    Ss   21:20   0:00 -bash
root      88171  0.0  0.0 108324  1020 pts/0    R+   21:20   0:00 ps aux
[root@test.example.com:~]# exit 0
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away