shell expect免密函数块sftp、scp、ssh
2024-10-30 09:01:26 阅读次数:11
expect,ssh,函数
一、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"
exp_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"
exp_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"
exp_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"
exp_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"
exp_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
版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/u_13236892/5870418,作者:哭泣的馒头,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。
上一篇:EOF:warning: delimited by end-of-file
下一篇:LeetCode刷题:反转链表 与 链表中的中间节点