SSH shell script to automatically turn off the firewall settings login password DNS closed selinx

! # / bin / bash 
# 20,190,606 
# qq450433231 
# list.txt file format: IP Password New Password 

createsh () { 
echo "start creating the script auto.sh" 
CAT auto.sh >> << - EOF 
/ bin / bash! 
echo "start execution script" 
Release = \ $ (CAT / etc /-Release System | -R & lt Sed 'S /.* ([0-9] +) \ .. * / \. 1 /') 
DNS = \ $ ( 172.26.4.193 /etc/resolv.conf|wc -l grep) 
IF [\ $ DNS -eq. 1]; the then 
          echo "Configuring the DNS already exists" 
the else 
          echo "nameserver 172.26.4.193"> /etc/resolv.conf 
          echo " 172.26.4.194 nameserver ">> /etc/resolv.conf 
          echo" the DNS configuration has been updated " 
fi 
Case \ $ in Release 
	7) 
       systemctl disable firewalld. Service service 
       systemctl stop firewalld.service
	   echo "Firewall is turned off." 
       ;; 
    . 6) 
       -Service STOP iptables 
	   the chkconfig iptables OFF 
	   echo "Firewall off" 
	   ;; 
	*) 
       echo "Firewall not closed" 
       ;; 
Esac 
the setenforce 0 && Sed -i 'S / enforcing / Disabled / G' / etc / SELinux / config  
echo "SELinux is turned off" 
the chage -d 0 root 
echo "login next time the user must change password" 
echo "successful script execution" Exit 0 && 
EOF 
echo "create a script auto.sh complete" 
} 

createexp () { 
echo "begin creating an automated login script ssh.exp" 
CAT ssh.exp >> << - EOF 
#!/usr/bin/expect
set passwd [lindex \$argv 0]
set host [lindex \$argv 1]
set shell [lindex \$argv 2]
set newpasswd [lindex \$argv 3]
spawn scp $shell root@\$host:$shell
expect {
    "yes/no" { send "yes\r"; exp_continue}
    "password:" { send "\$passwd\r" }
}
spawn ssh  root@\$host
expect {
    "yes/no" { send "yes\r"; exp_continue}
    "password:" { send "\$passwd\r" }
}
expect "]*"
send "echo '\$newpasswd'|passwd --stdin root || exit 1 \r"
expect "]*"
send "sh $shell \r"
expect "]*"
send "\[ -f $shell \] && rm -f $shell \r"
expect "]*"
send "exit\r"
expect eof
EOF
echo "创建脚本ssh.exp完成"
[ `ID -u` -ne 0] && echo" Please use the root user $ 0 "&& exit 1 
The shell = / root / auto.sh
}

ipfile=/root/list.txt
[-F $ ipfile] || echo "under /root/list.txt path of the file does not exist list.txt file format: IP Login Password New Password" && Exit 1 
[-f /root/auto.sh] && RM -f /root/auto.sh && echo "auto.sh delete old files" 
[-f /root/ssh.exp] && RM -f /root/ssh.exp && echo "ssh.exp delete old files" 
createsh 
createexp 

the while the Read Line  
do 
    IP = `echo $ Line | awk '{Print $. 1}'` 
    PW = `echo $ Line | awk '{Print $ 2}'` 
	the newpw = `echo $ Line | awk '{Print $. 3}'` 
	IF [- $ ip z] || [the -z $ pw] || [the -z $ newpw]; the then 
			echo "list.txt file in $ ip $ pw $ newpw error file format: IP Login password new password" 
			Exit 1 
	elif [ the shell $ -f]; the then 
		of ping. 1 $ -C IP> / dev / null 2>. 1 & 
			if [ $? -eq 0 ] ;then
				echo "	Login host $ ip " 
			the else 
				echo" $ ip host can not access Please check your network connection " 
			Exit 1
			fi  
		echo "$ ip is copied to the host automatically run scripts auto.sh 0.log Log $" | TEE -a /root/$0.log	 
	the else 
		echo "auto.sh script file not found"  
		Exit 1 
	fi 
    / usr / bin / the Expect / root / pw ssh.exp $ $ $ ip shell $ newpw 
DONE <$ ipfile 

echo "delete local script file" 
[-f /root/ssh.exp] && RM -f /root/ssh.exp 
[-f / root / auto.sh] && RM -f /root/auto.sh 

echo "$ (DATE +" the Y% /% m /% D% H:% M.% S ") $ 0 end script execution" | tee -a / root /$0.log 
echo "show log" 
LS /root/*.log  
Exit 0


Guess you like

Origin blog.51cto.com/junhai/2406155