直接上程序
#!/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写在上面即可