expect命令编写SSH自动输入密码与SCP传输文件

如下代码所示:
1、创建一个IP_list文件,将需要操作的主机都放进去
2、通过expect传输,并执行shell文件check_reconn.sh
注意:expect执行直接./[EXPECT.SH]文件即可,不能通过sh来执行。

#!/usr/bin/expect

set count 1
set password **********
set fd [open "/app/oracle_reconnect_check/ip_list683" r]
while { [gets $fd line] >= 0 } {
  #传输check_reconn.sh文件
  spawn scp /app/oracle_reconnect_check/check_reconn.sh appdeploy@$line:/tmp/
  set timeout 1
  expect "root@$line's password:"
  set timeout 1
  send "$password\r"
  #执行check_reconn.sh文件
  set timeout 1
  spawn ssh -l appdeploy -o StrictHostKeyChecking=no $line "sh /tmp/check_reconn.sh"
  set timeout 1
  expect "root@$line's password:"
  set timeout 1
  send "$password\r"
  set timeout 1
  puts "HOST $line is running task"
  
  puts "当前计数是:$count"
  incr count
}
close $fd
puts "file transer is done!"
sleep 300

#!/usr/bin/expect
set cnt 1
set fd1 [open "/app/oracle_reconnect_check/ip_list683" r]
while { [gets $fd1 line1] >= 0 } {
  #将结果文件传输回本机
  spawn scp appdeploy@$line1:/tmp/"$line1"_1521_inconformity.txt /app/oracle_reconnect_check/result/
  set timeout 1
  expect "root@$line1's password:"
  set timeout 1
  send "$password\r"
  #删除远程服务器上生成的结果文件
  sleep 2
  set timeout 1
  spawn ssh -l appdeploy -o StrictHostKeyChecking=no $line1 "rm /tmp/'$line1'_1521_inconformity.txt"
  set timeout 1
  expect "root@$line1's password:"
  set timeout 1
  send "$password\r"
  set timeout 1


  puts "当前计数是:$cnt"
  incr cnt

}
close $fd1

#send "exit\r"

expect eof 

你可能感兴趣的:(expect命令编写SSH自动输入密码与SCP传输文件)