このスクリプトを実行しようとしていますが、変更すると別のエラーが発生します。これがコードと出力です。助けてください。
デバッグ情報を含む投稿の最後の更新
#!/bin/bash
(( $# != 1 )) && { echo >&2 "Usage: $0 \"[COMMAND]\""; exit 1; }
servers_addresses=(10.10.10.10 )
for server_address in ${servers_addresses[@]}; do
expect <<EOF
spawn ssh -t root@$server_address "$*"
expect -timeout 2 "Are you sure you want to continue connecting (yes/no)?" { send "yes\n" }
expect "s password:" { send "Correct_Password\n" }
expect "s password:" { send "Wrong_Password_22222\n" }
expect "s password:" { send "Wrong_Password_33333\n" }
expect eof
EOF
done
そして出力は次のようになります:
goldberg188@Test-Server ~$ ./test.sh "Sudo cat /etc/hosts"
spawn ssh -t [email protected] Sudo cat /etc/hosts
[email protected]'s password:
# Do not remove the following line, or various programs
# that require network functionality will fail.
10.10.10.10 TEST-004 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
Connection to 10.10.10.10 closed.
expect: spawn id exp4 not open
while executing
"expect "s password:" { send "Wrong_Password_33333\n" }"
このように変更すると、出力は少し異なります
expect "s password:" { send "Wrong_Password_11111\n" }
expect "s password:" { send "Correct_Password\n" }
expect "s password:" { send "Wrong_Password_33333\n" }
goldberg188@Test-Server ~$ ./test.sh "Sudo cat /etc/hosts"
spawn ssh -t [email protected] Sudo cat /etc/hosts
[email protected]'s password:
# Do not remove the following line, or various programs
# that require network functionality will fail.
10.10.10.10 TEST-004 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
Connection to 10.10.10.10 closed.
expect: spawn id exp4 not open
while executing
"expect eof"
3行目に正しいパスワードが入力されていれば、エラーはまったくありません。これで問題なく動作します。
expect "s password:" { send "Wrong_Password_11111\n" }
expect "s password:" { send "Wrong_Password_22222\n" }
expect "s password:" { send "Correct_Password\n" }
goldberg188@Test-Server ~$ ./test.sh "Sudo cat /etc/hosts"
spawn ssh -t [email protected] Sudo cat /etc/hosts
[email protected]'s password:
# Do not remove the following line, or various programs
# that require network functionality will fail.
10.10.10.10 TEST-004 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
Connection to 10.10.10.10 closed.
編集を無視:2つのコマンドを一度に実行する方法を知っています。
更新:デバッグ情報-に変更
exp_internal 1
expect "s password:" { send "Wrong_Password_11111\n" }
expect "s password:" { send "Correct_Password\n" }
expect "s password:" { send "Wrong_Password_33333\n" }
出力:
goldberg188@Test-Server ~$ ./test.sh "Sudo cat /etc/Host"
spawn ssh -t [email protected] Sudo cat /etc/Host
[email protected]'s password:
expect: does "[email protected]'s password: " (spawn_id exp4) match glob pattern "s password:"? yes
expect: set expect_out(0,string) "s password:"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "[email protected]'s password:"
send: sending "Wrong_Password_11111\n" to { exp4 }
expect: does " " (spawn_id exp4) match glob pattern "s password:"? no
expect: does " \r\n" (spawn_id exp4) match glob pattern "s password:"? no
Permission denied, please try again.
[email protected]'s password:
expect: does " \r\nPermission denied, please try again.\r\r\[email protected]'s password: " (spawn_id exp4) match glob pattern "s password:"? yes
expect: set expect_out(0,string) "s password:"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " \r\nPermission denied, please try again.\r\r\[email protected]'s password:"
send: sending "Correct_Password\n" to { exp4 }
expect: does " " (spawn_id exp4) match glob pattern "s password:"? no
expect: does " \r\n" (spawn_id exp4) match glob pattern "s password:"? no
cat: /etc/Host: No such file or directory
Connection to 10.10.10.10 closed.
expect: does " \r\ncat: /etc/Host: No such file or directory\r\r\nConnection to 10.10.10.10 closed.\r\r\n" (spawn_id exp4) match glob pattern "s password:"? no
expect: read eof
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " \r\ncat: /etc/Host: No such file or directory\r\r\nConnection to 10.10.10.10 closed.\r\r\n"
expect: spawn id exp4 not open
while executing
"expect eof"
意図的に間違ったパスワードを送信していないと想定して、exp_continue
をループ構造として:
expect <<EOF
set passwds {foo bar baz}
set i 0
spawn ssh -t root@$server_address "$*"
expect {
"continue connecting (yes/no)?" { send "yes\r"; exp_continue }
" password: " { send "[lindex $passwds $i]\r"; incr i; exp_continue }
eof
}
EOF
クレジット: glenn jackman
申し訳ありませんが、Glennのコードにはいくつかの変更が必要ですが、ここに追加して機能させます。
これは、それを必要とする可能性のある他の人のための固定コードです。 IPをテキストファイルに入れて、servers_addresses内にcatできます。同様にservers_addresses =( 'cat ip_list.txt')
IPリストに次のようなIPを追加します10.10.10.1 10.10.10.2 10.10.10.3
必要に応じて、さらに例を示します。コマンドを実行します。
./test.sh "Sudo cat /etc/hosts"
複数のコマンドを実行する
./test.sh "Sudo cat /etc/hosts & /etc/init.d/network status"
検索と置換
./test.sh "sed -i -e 's/SEARCH_STRING/REPLACE_STRING/g' /tmp/FileNAME.txt"
完全なコード:
#!/bin/bash
(( $# != 1 )) && { echo >&2 "Usage: $0 \"[COMMAND]\""; exit 1; }
servers_addresses=( 10.10.10.10 192.168.10.1 )
for server_address in ${servers_addresses[@]}; do
expect <<EOF
set passwds { password1 password2 password3 }
set i 0
spawn ssh -t root@$server_address "$*"
expect {
"Are you sure you want to continue connecting (yes/no)?" { send "yes\r"; exp_continue }
"s password:" { send "[lindex \$passwds \$i]\r"; incr i; exp_continue }
eof
}
EOF
done