shell expect免密函数块sftp、scp、ssh

时间:2022-11-19 14:58:57

一、expect编写函数

1、变量设置

#!/bin/bash
################remote infomation############
remoteuser=zhangsan
remotepass=123456
remoteport=22
remoteip=ip.txt
remoteconfigdir=/tmp
isrestart=yes
remotebindir=/tmp
##################script dir#####################
localpath=$(cd `dirname $0`;pwd)
cd ${localpath}

2、sftp函数

#################function sftp##################
function sftpconfig() {
/usr/bin/expect<<EOF
set timeout 20
spawn sftp -P ${remoteport} ${remoteuser}@${1}
expect {
"yes/no" {
send "yes\r"
expect_continue
}
"assword" { #此处不写password是因为有的操作系统是小p,有的操作系统是大P
send "${remotepass}\r"
}
}
expect "sftp>"
send "cd ${remoteconfigdir}\r"
expect "sftp>"
send "lcd ${temp}\r"
expect "sftp>"
send "put -r *\r"
expect "sftp>"
send "bye\r"
EOF
#EOF前面后面不能有空格不然就报错
echo ""
sleep 1
}

示例:
sftpconfig 10.10.10.10

3、scp函数

####################scpconfig####################
#scp只能一次传一个文件,没法用*批量传
function scpconfig() {
/usr/bin/expect<<EOF
set timeout 20
spawn scp -P ${remoteport} ${2} ${remoteuser}@${1}:${remoteconfigdir}
expect {
"yes/no" {
send "yes\r"
expect_continue
}
"assword" { #此处不写password是因为有的操作系统是小p,有的操作系统是大P
send "${remotepass}\r"
}
}
expect eof
EOF
}

示例:
scpconfig 10.10.10.10 a.conf

4、ssh函数

####################sshstart####################
function sshstart() {
/usr/bin/expect<<EOF
set timeout 20
spawn ssh -p ${remoteport} ${remoteuser}@${1}
expect {
"yes/no" {
send "yes\r"
expect_continue
}
"assword" { #此处不写password是因为有的操作系统是小p,有的操作系统是大P
send "${remotepass}\r"
}
}
expect "*"
send "cd ${remotebindir} && sh stop.sh && sleep 2 && sh start.sh\r"
expect "*"
send "exit\r"
expect eof
EOF
sleep 1
}

示例:
sshstart 10.10.10.10

二、脚本配置文件传输并重启

有一个需求是需要将配置文件批量传输到远程机器并重启服务

1、规划
script/
ip.txt #存放远程机器ip
config/ #存放配置文件,如果需要将配置文件ip改成服务器对应,则将ip设置成localip
temp/ #拷贝config/目录的配置文件,并将localip替换成远程主机具体ip
main.sh #脚本

2、ip.txt
cat ip.txt
192.168.10.100
192.168.10.101
192.168.10.102

3、config
cat a.conf
ip:localip

4、main.sh
#!/bin/bash
################remote infomation############
remoteuser=zhangsan
remotepass=123456
remoteport=22
remoteip=ip.txt
remoteconfigdir=/tmp
isrestart=yes
remotebindir=/tmp
##################script dir#####################
localpath=$(cd `dirname $0`;pwd)
cd ${localpath}
####################scpconfig####################
function scpconfig() {
/usr/bin/expect<<EOF
set timeout 20
spawn scp -P ${remoteport} $2 ${remoteuser}@$1:${remoteconfigdir}
expect {
"yes/no" {
send "yes\r"
expect_continue
}
"assword" { #此处不写password是因为有的操作系统是小p,有的操作系统是大P
send "${remotepass}\r"
}
}
expect eof
EOF
}
####################sshstart####################
function sshstart() {
/usr/bin/expect<<EOF
set timeout 20
spawn ssh -p ${remoteport} ${remoteuser}@$1
expect {
"yes/no" {
send "yes\r"
expect_continue
}
"assword" { #此处不写password是因为有的操作系统是小p,有的操作系统是大P
send "${remotepass}\r"
}
}
expect "*"
send "cd ${remotebindir} && sh stop.sh && sleep 2 && sh start.sh\r"
expect "*"
send "exit\r"
expect eof
EOF
sleep 1
}
#######################main#####################
cat ${remoteip}|while read line
do
find ${localpath}/temp -type f -delete
cp config/* temp/
##如果需要ip替换
sed -i 's#localip#'${line}'#' temp/a.conf
for i in $(ls ${localpath}/temp/*)
do
scpconfig ${line} ${i}
done
if [ ${isrestart} == 'yes' ]
then
sshstart ${line}
fi
done