您的位置:首页 > 运维架构 > Shell

Shell 脚本远程批量执行命令

2016-12-13 14:18 651 查看
#! /usr/bin/expect
set ip_array 192.168.11.    ######   网段前缀
set username root
set timeout 30
#foreach ip {81 82 83 84 86 87 88 89 91 92 93 94 96 97 98 99} {                ######IP
for {set ip 21} {$ip <= 22} {incr ip} {               ###### 大于等于11小于等于50,如果IP有规律可用
spawn ssh -p 22 $username@$ip_array$ip
expect {
"(yes/no)?" { send "yes\r"; exp_continue }
"Password:" { send "password\r" }
}
expect "*#"
send "cp /etc/login.defs /etc/login.defs.bak\r"
send "cp /etc/profile /etc/profile.bak\r"
send "cp /etc/pam.d/common-password /etc/pam.d/common-password.bak\r"
send "sed -i -e '/^PASS_MIN_DAYS/'d /etc/login.defs\r"
send "sed -i -e '/^PASS_WARN_AGE/'d /etc/login.defs\r"
send "sed -i -e '/^ca\:\:ctrlaltdel/'d /etc/inittab\r"
#		send "echo "#ca::ctrlaltdel:/sbin/shutdown -t3 -r now" >> /etc/inittab\r"
send "echo PASS_MAX_DAYS 90 >> /etc/login.defs\r"
send "echo PASS_MIN_DAYS 5 >> /etc/login.defs\r"
send "echo PASS_WARN_AGE 7 >> /etc/login.defs\r"
send "echo PASS_MIN_LEN 8 >> /etc/login.defs\r"
send "echo TMOUT=300 >> /etc/profile\r"
send "echo HISTFILESIZE=5 >> /etc/profile\r"
send "echo HISTSIZE=5 >> /etc/profile\r"
#       send "echo umask 027 >> /etc/profile\r"
send "echo password  required  pam_cracklib.so use_authtok minlen=8 ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1 >> /etc/pam.d/common-password\r"
send "echo auth required pam_tally2.so deny=5 onerr=fail no_magic_root unlock_time=300 >> /etc/pam.d/common-password\r"
send "echo  password    sufficient    pam_unix.so md5 shadow nullok try_first_pass use_authtok remember=5 >> /etc/pam.d/common-password\r"
expect "*#"
send "exit\r"
}
expect eof
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Shell expect