ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Shell批量复制或运行文件(自动输入密码)

Shell批量复制或运行文件(自动输入密码)

原创 Linux操作系统 作者:Michael_DD 时间:2014-09-29 16:16:37 0 删除 编辑
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博客 ” ,链接:https://blog.itpub.net/29500582/viewspace-1284955/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-02-22

  • 博文量
    326
  • 访问量
    2671974