加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

expect实现自动scp和ssh登录后执行命令,并向bash提供返回值

发布时间:2020-12-15 16:57:50 所属栏目:安全 来源:网络整理
导读:#!/bin/bash#全局变量:用户名,普通用户密码,root密码loginname="xxxx"userpwd="xxxx"rootpwd="xxxx"#读取iplist(逐行存储IP地址)cat/root/iplist|whilereadlinedoip=($line)#scp脚本及ssh登录执行/usr/bin/expectEOFsettimeout10spawnscp/root/check_and_
#!/bin/bash
#全局变量:用户名,普通用户密码,root密码
loginname="xxxx"
userpwd="xxxx"
rootpwd="xxxx"

#读取iplist(逐行存储IP地址)
cat/root/iplist|whilereadline
do
ip=($line)

#scp脚本及ssh登录执行
/usr/bin/expect<<EOF
settimeout10
spawnscp/root/check_and_fix.sh$loginname@$ip:/home/xxxx
expect{
"*yes/no"{send"yesr";exp_continue}
"*password:"{
send"$userpwdr"
expecteof
}
}
spawnssh$ip-l$loginname-p22
expect{
"*yes/no"{send"yesr";exp_continue}
"*password:"{
send"$userpwdr"
expect"~]$"
send"su-r"
expect"*assword:"
send"$rootpwdr"
expect"~]#"
send"md5sum/home/xxx/check_and_fix.shr"
expect{
#a4e3eb6a16f78129cf78d67d1c737ce9为上述文件正确的md5值,若正确则执行该脚本
"a4e3eb6a16f78129cf78d67d1c737ce9"{send"/home/xxxx/check_and_fix.shr"}
}
expect{
"存在漏洞并已修复"{exit0}
#"存在漏洞并已修复"为脚本执行输出结果,匹配后退出expect并向bash提供返回值0
}
expecteof
exit1#若md5值不正确,则匹配上一条expecteof后匹配exit1,退出expect并向bash返回1
}
}
EOF

#记录修复日志
if[$?-eq0]
then
echo"$ip漏洞修复完成">>/var/log/patch.log
else
echo"$ip漏洞修复失败">>/var/log/patch.log
fi
done

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读