LoginSignup
0
0

More than 3 years have passed since last update.

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

Last updated at Posted at 2019-08-23

いつも諦めさせられる、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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0