今まで知らなかったのだが、expectでは以下のようにログファイルを指定する事でscriptコマンドのようにターミナルログを取得することが可能なようだ。
#!/bin/bash
expect -c "
trap {
set rows [stty rows]
set cols [stty columns]
stty rows \$rows columns \$cols < \$spawn_out(slave,name)
} WINCH
set host hostname
set user username
set pass Password
set log \"/path/to/log\"
spawn ssh \${user}@\${host}
log_file \${log}
expect \"s password:\"
send \"\${pass}\n\"
interact
"
Shebangをexpectにして実行する場合には以下。
#!/usr/bin/env expect
trap {
set rows [stty rows]
set cols [stty columns]
stty rows $rows columns $cols < $spawn_out(slave,name)
} WINCH
set host hostname
set user username
set pass Password
set log "/path/to/log"
spawn ssh ${user}@${host}
log_file ${log}
expect "s password:"
send "${pass}\n"
interact
blacknon@BS-PUB-UBUNTU-01:~$ ./expect.sh
spawn ssh blacknon@172.20.100.118
blacknon@172.20.100.118's password:
Last login: Sat Dec 16 22:55:04 2017 from 172.20.100.120
[blacknon@BS-PUB-CENT7-01 ~]$ ls -la
合計 16
drwx------. 2 blacknon blacknon 79 12月 15 09:22 .
drwxr-xr-x. 3 root root 21 12月 15 09:21 ..
-rw-------. 1 blacknon blacknon 272 12月 16 22:55 .bash_history
-rw-r--r--. 1 blacknon blacknon 18 12月 7 2016 .bash_logout
-rw-r--r--. 1 blacknon blacknon 193 12月 7 2016 .bash_profile
-rw-r--r--. 1 blacknon blacknon 231 12月 7 2016 .bashrc
[blacknon@BS-PUB-CENT7-01 ~]$
[blacknon@BS-PUB-CENT7-01 ~]$
[blacknon@BS-PUB-CENT7-01 ~]$
[blacknon@BS-PUB-CENT7-01 ~]$
[blacknon@BS-PUB-CENT7-01 ~]$ exit
ログアウト
Connection to 172.20.100.118 closed.
blacknon@BS-PUB-UBUNTU-01:~$
blacknon@BS-PUB-UBUNTU-01:~$ cat test_expect.log
blacknon@172.20.100.118's password:
Last login: Sat Dec 16 22:55:04 2017 from 172.20.100.120
[blacknon@BS-PUB-CENT7-01 ~]$ ls -la
合計 16
drwx------. 2 blacknon blacknon 79 12月 15 09:22 .
drwxr-xr-x. 3 root root 21 12月 15 09:21 ..
-rw-------. 1 blacknon blacknon 272 12月 16 22:55 .bash_history
-rw-r--r--. 1 blacknon blacknon 18 12月 7 2016 .bash_logout
-rw-r--r--. 1 blacknon blacknon 193 12月 7 2016 .bash_profile
-rw-r--r--. 1 blacknon blacknon 231 12月 7 2016 .bashrc
[blacknon@BS-PUB-CENT7-01 ~]$
[blacknon@BS-PUB-CENT7-01 ~]$
[blacknon@BS-PUB-CENT7-01 ~]$
[blacknon@BS-PUB-CENT7-01 ~]$
[blacknon@BS-PUB-CENT7-01 ~]$ exit
ログアウト
Connection to 172.20.100.118 closed.