あるLinuxサーバーから、Expectコマンドを利用して複数台ある他のLinuxサーバーのIPアドレスをwhileで読み込ませて、順番にSCPコマンド自動化スクリプトを作成しておりますが、パスワード入力のプロンプトでどうしても処理がストップしてしまいます。どうもsendコマンドによるパスワード受け渡しがうまくできていないようです。
現象:以下パスワード入力プロンプトで止まってしまう
spawn scp test.txt ohm@192.168.38.194:/tmp
ohm@192.168.38.194's password:
フルコードは以下になります。どなたかご教授、アドバイス頂けますでしょうか?
#!/bin/bash ip_file='systems.txt' USER='ohm' PASS='#xxxxxxxx' TARGET_DIR='test.txt' CR_DIR='/tmp/' while read line || [ -n "${line}" ]; do expect -c " set timeout -1 spawn scp ${TARGET_DIR} ${USER}@${line}:${CR_DIR} expect { \" Are you sure you want to continue connecting (yes/no)? \" { send \"yes\r\" expect \"${USER}@${line}'s password: \" send \"${PASS}\r\" } exp_continue } interact " done < ${ip_file
修正箇所
コードを以下の様に変更した結果、パスワードプロンプトで停止せずスクリプトの実行は終了たが、scpの処理が実行されない。 expect { \" Are you sure you want to continue connecting (yes/no)? \" { send \"yes\r\" expect \"${USER}@${line}'s password:\" -re \"${USER}@${line}'s password:\" send \"${PASS}\r\" } -re \"${USER}@${line}'s password:\" { send \"${PASS}\r\" } interact "
scp -vオプションを付けて実行した結果になります。scpコマンドは実行されている事が確認。
[root@host]# bash scp_auto.sh
spawn scp -v test.txt ohm@22.222.22.222:/tmp
Executing: program /usr/bin/ssh host 22.222.22.222, user far, command scp -v -t /tmp
OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: Connecting to 22.222.22.222 [22.222.22.222] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2
debug1: match: OpenSSH_7.2 pat OpenSSH* compat 0x04000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-sha1 none
debug1: kex: client->server aes128-ctr hmac-sha1 none
debug1: kex: curve25519-sha256@libssh.org need=20 dh_need=20
debug1: kex: curve25519-sha256@libssh.org need=20 dh_need=20
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: RSA c2:ae:1e:70:c4:82:7e:21:49:36:5d:4a:29:6a:2d:a9
debug1: Host '22.222.22.222' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:18
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Next authentication method: keyboard-interactive
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: password
ohm@22.222.22.222's password: [root@host]#
修正後:
while read line || [ -n "${line}" ];
do
expect -c "
set timeout 5
spawn scp -v ${TARGET_DIR} ${USER}@${line}:${CR_DIR}
expect {
"${USER}@${line}'s password:"
send "${PASS}\n"
}
-re "[Pp]assword:" {
send "${PASS}\n"
}
interact
"
done < ${ip_file}
結果:
debug1: Next authentication method: password
ohm@22.222.22.222's password: usage: send [args] string
while executing
"send"
invoked from within
"expect {
"ohm@22.222.22.222's password:"
send "#!8102L#>t\n"
}"
[root@host]
修正版:成功!!
expect -c "
set timeout 5
spawn scp -v ${TARGET_DIR} ${USER}@${line}:${CR_DIR}
expect "${USER}@${line}'s password:" {
send "${PASS}\n"
}
expect "${USER}@${line}'s password:" {
send "${PASS}\n"
}
"
結果:
Sending file modes: C0755 0 test.txt
Sink: C0755 0 test.txt
test.txt 100% 0 0.0KB/s 00:00
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 2688, received 4328 bytes, in 0.1 seconds
Bytes per second: sent 25747.9, received 41457.1
debug1: Exit status 0

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2018/03/16 03:18
2018/03/16 04:37
2018/03/16 05:04
2018/03/16 05:10
2018/03/16 06:36
2018/03/16 06:43
2018/03/16 06:51
2018/03/16 06:53
2018/03/16 07:31