Guest User

Untitled

a guest
Feb 9th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. 1.单台传输脚本
  2. cat /root/soft_shell/allscp.sh
  3.  
  4. #!/usr/bin/expect
  5. if {$argc < 2} {
  6. send_user "usage: $argv0 src_file username ip dest_file password\n"
  7. exit
  8. }
  9. ##set key [lindex $argv 0]
  10. set src_file [lindex $argv 0]
  11. set username [lindex $argv 1]
  12. set host_ip [lindex $argv 2]
  13. set dest_file [lindex $argv 3]
  14. set password [lindex $argv 4]
  15. ##spawn scp -i $key $src_file $username@$host_ip:$dest_file
  16. spawn scp $src_file $username@$host_ip:$dest_file
  17. expect {
  18. "(yes/no)?"
  19. {
  20. send "yes\n"
  21. expect "password:" {send "$password\n"}
  22. }
  23. "password:"
  24. {
  25. send "$password\n"
  26. }
  27. }
  28. expect "100%"
  29. expect eof
  30.  
  31. 2.多台传输脚本
  32. cat /root/soft_shell/mainscp.sh
  33.  
  34. #!/bin/bash
  35. host_list="server_list.conf"
  36. src_file=$1 #设置目标文件变量(此变量为执行命令时的第一个参数)
  37. dest_file=$2 #设置目标文件变量(此变量为执行命令时的第二个参数)
  38. cat $host_list | while read line
  39. do
  40. host_ip=`echo $line|awk '{print $1}'`
  41. username=`echo $line|awk '{print $2}'`
  42. password=`echo $line|awk '{print $3}'`
  43. #src_file=`echo $line|awk '{print $4}'`
  44. #dest_file=`echo $line|awk '{print $5}'`
  45. ##key=`echo $line|awk '{print $6}'`
  46. ./allscp.sh $key $src_file $username $host_ip $dest_file $password #调用拷贝文件shell
  47. ./dep.sh $username $host_ip $password #调用执行命令shell
  48. done
  49.  
  50. 3.服务器信息文件
  51. cat /root/soft_shell/server_list.conf
  52. 格式为:
  53. ip 用户名 密码 源文件 目标文件地址
  54.  
  55. 执行命令脚本
  56. #!/usr/bin/expect -f
  57. set timeout 300
  58. set username [lindex $argv 0]
  59. set host_ip [lindex $argv 1]
  60. set password [lindex $argv 2]
  61. spawn ssh $username@$host_ip
  62. expect {
  63. "yes/no" { send "yes\r";exp_continue }
  64. "password:" { send "$password\r" }
  65. }
  66. expect -re "\](\$|#) "
  67. send "cd /tmp && ./zabbix_install.sh\r"
  68. expect -re "\](\$|#) "
  69. send "exit\r"
  70.  
  71. ps:以上3个文件,相信大家都看出来了,都是放在同一文件夹下面的.我本地测试只用ssh密码,没有加上ssh key,如果要用上跟我们公司正式环境一样的安全方式(ssh密码+key,才能登录服务器),那么请自己修改脚本文件,我比较懒这里就不说得那么详细了.
Add Comment
Please, Sign In to add comment