基于expect开发免密码分发多节点文件,ssh多节点操作

直接上程序

#!/usr/bin/expect -f

set filepath "/home/text_file.txt"
set remotepath "/home/"
set password "123456"

foreach ip {
10.9.233.190
10.9.233.191
10.9.233.192
10.9.233.193
10.9.233.194
} {
#puts $ip;
spawn scp -r $filepath root@$ip:$remotepath
expect {
    "*yes/no" { send "yes\r"; exp_continue }
    "*password:" { send "$password\r" }
}
interact   
}

注意:

该脚本通过expect实现人机交互,expect需要tcl语言支持

脚本开头不是以往的/bin/bash,而是expect

注意空格的影响

set varname varvalue  通过set给变量赋值

put 将内容打印出来,做调试用

加强版:批量文件分发多节点

#!/bin/bash
   
src_dir=/home/patchdrs/*
dest_dir=/home/
password="123456"

find $src_dir | while read line  
do
filepath=$line
#echo $filepath
for ip in 192.168.1.1 192.168.1.2 192.168.1.2
do
expect << EOF
spawn scp -r $filepath root@$ip:$dest_dir
expect {
    "*yes/no" { send "yes\r"; exp_continue }
    "*password:" { send "$password\r" }
}
expect eof
EOF
done
done

ssh多节点操作linux命令

#脚本内容
#!/bin/bash
   
baseDir=`cd \`dirname $0\`; pwd` 
ipFile=$baseDir/ip.conf
password="E65!9*wkQ"
   
while read line   
do
ip=$line
expect <<-EOF
   set timeout 3                      
   spawn ssh root@$ip
   expect {
	 "*yes/no" { send "yes\r"; exp_continue }
     "*password:" { send "$password\r" }
   }
   expect "*#"
   send "df -h\r" 
   expect "*#"
   send "exit\r"
   interact       
   expect eof
EOF
done < $ipFile
--------------------------------------------------
#ipconf文件
10.37.47.93
10.37.47.94
10.37.47.95
10.37.47.96
over,这是最后一行,不要随意添加无关行,ip写在上面即可 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值