Shell批量複製或執行檔案(自動輸入密碼)

Michael_DD發表於2014-09-29
Shell批量複製或執行檔案

全部指令碼放在同一個目錄下:
1  根據ip密碼檔案,批量處理

 passwd.txt 格式

點選(此處)摺疊或開啟

  1. 192.168.9.111 password
  2. 192.168.9.112 password
  3. 192.168.9.113 password
  4. 192.168.9.114 password

2  登入指令碼login.exp

點選(此處)摺疊或開啟

  1. #!/usr/bin/expect -f
  2. set ip [lindex $argv 0 ]
  3. set passwd [lindex $argv 1 ]
  4. set command [lindex $argv 2]
  5. set timeout -1
  6. spawn ssh root@$ip
  7. expect {
  8. \"yes/no\" { send \"yes\\r\";exp_continue }
  9. \"password:\" { send \"$passwd\\r\" }
  10. }
  11. expect \"*#*\" { send \"$command\\r\" }
  12. expect \"#*\" { send \"exit\\r\" }
  13. expect eof

3  批量複製檔案scp.sh
[root@ passwdsh]# sh scp.sh filename

點選(此處)摺疊或開啟

  1. uto exec expect shell scripts
  2. if
  3.       [ ! -e /usr/bin/expect ];then
  4.       yum install expect -y
  5. fi
  6. #Judge passwd.txt exist
  7. if
  8.       [ ! -e ./passwd.txt ];then
  9.       echo -e \"The passwd.txt is not exist......Please touch ./passwd.txt ,Content Example:\\n192.168.1.11 passwd1\\n192.168.1.12 passwd2\"
  10.       sleep 2 &&exit 0
  11. fi
  12. #Auto Tuoch login.exp File
  13. cat>login.exp <<EOF
  14. #!/usr/bin/expect -f
  15. set ip [lindex \\$argv 0]
  16. set passwd [lindex \\$argv 1]
  17. set src_file [lindex \\$argv 2]
  18. set des_dir [lindex \\$argv 3]
  19. set timeout -1
  20. spawn scp -p \\$src_file root@\\$ip:\\$des_dir
  21. expect {
  22. \"yes/no\" { send \"yes\\r\"; exp_continue }
  23. \"password:\" { send \"\\$passwd\\r\" }
  24. }
  25. expect \"100%\"
  26. expect eof
  27. EOF
  28. ##Auto exec shell scripts
  29. if
  30.     [ \"$1\" == \"\" ];then
  31.     echo ========================================================
  32.     echo \"Please insert your are command ,Example {/bin/sh $0 /src /des } ,waiting exit ........... \"
  33.     sleep 2
  34.     exit 1
  35. fi
  36. for i in `awk \'{print $1}\' passwd.txt`
  37. do
  38.       j=`awk -v I=\"$i\" \'{if(I==$1)print $2}\' passwd.txt`
  39.       expect ./login.exp $i $j $1 $2
  40. done

4  批量執行指令碼ssh.sh
[root@ passwdsh]# sh ssh.sh filename

點選(此處)摺疊或開啟

  1. uto exec expect shell scripts
  2. if
  3.       [ ! -e /usr/bin/expect ];then
  4.       yum install expect -y
  5. fi
  6. #Judge passwd.txt exist
  7. if
  8.       [ ! -e ./passwd.txt ];then
  9.       echo -e \"The passwd.txt is not exist......Please touch ./passwd.txt ,Content Example:\\n192.168.1.11 passwd1\\n192.168.1.12 passwd2\"
  10.       sleep 2 &&exit 0
  11. fi
  12. #Auto Tuoch login.exp File
  13. cat>login.exp <<EOF
  14. #!/usr/bin/expect -f
  15. set ip [lindex \\$argv 0 ]
  16. set passwd [lindex \\$argv 1 ]
  17. set command [lindex \\$argv 2]
  18. set timeout -1
  19. spawn ssh root@\\$ip
  20. expect {
  21. \"yes/no\" { send \"yes\\r\";exp_continue }
  22. \"password:\" { send \"\\$passwd\\r\" }
  23. }
  24. expect \"*#*\" { send \"\\$command\\r\" }
  25. expect \"#*\" { send \"exit\\r\" }
  26. expect eof
  27. EOF
  28. ##Auto exec shell scripts
  29. CMD=\"$*\"
  30. if
  31.     [ \"$1\" == \"\" ];then
  32.     echo ========================================================
  33.     echo \"Please insert your command ,Example {/bin/sh $0 \'mkdir -p /tmp\'} ,waiting exit ........... \"
  34.     sleep 2
  35.     exit 1
  36. fi
  37. for i in `awk \'{print $1}\' passwd.txt`
  38. do
  39.     j=`awk -v I=\"$i\" \'{if(I==$1)print $2}\' passwd.txt`
  40.     expect ./login.exp $i $j \"$CMD\"
  41. done


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29500582/viewspace-1284955/,如需轉載,請註明出處,否則將追究法律責任。

相關文章