sudoを介してサーバーにSSHで接続する - スクリプトで予期しないファイル終了エラーが発生する

sudoを介してサーバーにSSHで接続する - スクリプトで予期しないファイル終了エラーが発生する

iptables --flush100を超えるサーバーでコマンドを実行する必要があります。ルート資格情報はありませんが、私のアカウントへのsudoアクセス権があります。だから私はすべてのサーバーでこのコマンドを実行するために以下のスクリプトを考えました。

flashhostsファイルにサーバー名を追加しました。

[root@~]# cat testscript.sh

> for i in `cat /flushhosts`
> 
> do
> 
> sshpass -p 'Mypwd' ssh -t username@$i sudo /sbin/iptables --flush
> 
> cat << EOF || "Mypwd"
> 
> done

[root@~]# ./testscript.sh

./testscript.sh: line 6: syntax error: unexpected end of file

スクリプトに欠落しているコンテンツが見つかりません。

ベストアンサー1

別のアプローチを使用したい場合は、次のようにExpectを使用することをお勧めします。

小さな期待スクリプトの作成ex1.sh

#!/usr/bin/expect -f
set arg1 [lindex $argv 0]
spawn ssh username@$arg1
expect "password: "
send "Mypwd\r"
expect "$ "
send "sudo /sbin/iptables --flush\r"
expect "password "
send "Mypwd\r"
expect "$ "
send "exit\r"

その後、次のループで使用できます。

for i in $(</flushhosts); do ./ex1 $i; done

この場合、Expectをより柔軟に使用できます。

おすすめ記事