[Shell] Linux主机密码批量回收 脚本
来源:互联网 发布:网络巡更系统 编辑:程序博客网 时间:2024/04/28 08:01
需求:每月末定时回收主机密码,但是规定我堡垒机与各主机不能做公钥,只能明文。。。本来准备用python的paramiko模块做的,但是还是没弄好,就先用shell吧。。。有的地方写的比较挫。。
scp_shell.sh
#!/usr/bin/expect
#author: yifangyou
#create time:2011-05-17
set scphost "[lindex $argv 0]"
set port "[lindex $argv 1]"
set scpuser "[lindex $argv 2]"
set scppw "[lindex $argv 3]"
#要执行的shell命令
set cmd "[lindex $argv 4]"
spawn ssh -p $port $scpuser@$scphost "$cmd"
set timeout 100
expect {
#respose: "root@1.2.3.4's password:"
"*assword*" {
set timeout 30
send "$scppw\r"
}
#the first connect will respose "Are you sure you want to continue connecting (yes/no)? yes"
"*yes*" {
set timeout 100
send "yes\r"
set timeout 100
expect "*assword*"
set timeout 100
send "$scppw\r"
}
busy {send_user "\n";exit 1;}
failed {send_user "\n";exit 2;}
timeout {send_user "\n";exit 3;}
}
#Permission denied not try again
expect {
"*denied*" {
send_user "\n"
exit 4
}
busy {send_user "\n";exit 5;}
failed {send_user "\n";exit 6;}
timeout {send_user "\n";exit 7;}
}
exit 0
passroot.sh
#!/bin/bash
tmp_usr=root
dir=/usr/sh/shell/linux/password/root_pass_smredhat_20170901
dir1=scp_shell.sh
data1=$(date +%Y%m%d)
dir2="/usr/sh/shell/linux/log/pass_root_smredhat_$data1.log"
echo "Begin! $(date +%Y%m%d-%T)" >> $dir2
more "$dir" | while read line
do
randroot=`cat /dev/urandom | tr -dc "a-eA-E0-5_\~\#\%\&\."| fold -w 10 | head -n 1` #生成随机数密码
tmp_ip=`echo $line | awk '{print $1}'`
tmp_cmd="echo '"$randroot"' | passwd --stdin root && echo yes"
tmp_pwd=`echo $line | awk '{print $3}'`
echo -e "$tmp_ip\troot\t$randroot" >> /usr/sh/shell/linux/password/root_pass_smredhat_$data1
/usr/bin/expect $dir1 $tmp_ip 22 $tmp_usr "$tmp_pwd" "$tmp_cmd" >> $dir2 #传参,ip、port、user、old-passwd、cmd
done
echo "End! $(date +%Y%m%d-%T)" >> $dir2
tol_num=`more $dir2 | awk '{print $1}' | grep yes | wc -l`
echo "$tol_num" >> $dir2
- [Shell] Linux主机密码批量回收 脚本
- 利用shell脚本批量获取主机信息
- Linux 批量拷贝数据脚本 + ssh 无密码登陆远程LINUX主机
- python 远程批量更改linux主机密码
- Linux多台主机批量命令脚本
- Linux多台主机批量命令脚本
- Linux#Shell#密码生成脚本
- 用shell脚本批量连续更改FreeBSD主机ip
- 通过linux的shell脚本批量创建用户名,用户名所属目录,设置对应密码
- 通过linux的shell脚本批量创建用户名,用户名所属目录,设置对应密码
- Linux常用的主机监控shell脚本
- linux常用的主机shell脚本
- shell脚本——linux主机监控
- linux批量修改文件后缀名shell脚本
- linux shell + expect:批量scp脚本工具
- linux批量修改文件后缀名shell脚本
- linux批量修改文件字符集shell 脚本
- linux shell 批量mac ping脚本
- microsoft visual c++ 14.0 is required问题解决办法
- PHP中注解(Annotation)的定义与使用
- STM32中断向量表的位置,重定向
- matplotlib核心剖析
- 错误:在证书存储区找不到清单签名证书
- [Shell] Linux主机密码批量回收 脚本
- mysql update、delete left join 操作
- leetcode zigzag-convert
- Apache Shrio官方介绍文档翻译
- JAVA并发编程(一)—基本概念和术语
- activiti部署、执行,完成简单示例
- tcp传输机制
- PHP 错误处理
- Java导出CSV文件