一、
[root@desktop27 mnt]# vim useradd_create.sh
[root@desktop27 mnt]# cat useradd_create.sh
#!/bin/bash
Auto_Connect()
{
/usr/bin/expect <<EOF | grep -E "authenticity|ECDSA|connecting|Warning|spawn|password" -v
set timeout 5
spawn ssh root@172.25.254.$NUM "$1"
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "westos\r" }
}
expect eof
EOF
}
for NUM in {50..52}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null &&{
Max_Line=`awk 'BEGIN{N=0}{N++}END{print N}' $1`
for Line_Num in `seq 1 $Max_Line`
do
USERNAME=$(sed -n "${Line_Num}p" $1)
PASSWORD=$(sed -n "${Line_Num}p" $2)
User_Check=`Auto_Connect "useradd $USERNAME"`
[ -n "User_Check" ]&&{
echo $User_Check
}||{
Auto_Connect "echo $PASSWORD | passwd --stdin $USERNAME"
}
done
}|| echo 172.25.254.$NUM is down
done
[root@desktop27 mnt]#
第一次执行,成功后无显示,第二次再执行时,显示用户已经存在
二、
[root@desktop27 mnt]# vim db_dump.sh
[root@desktop27 mnt]# cat db_dump.sh
#!/bin/bash
DATABASE=`mysql -uroot -EN -e "show databases;" | grep -E "^\*|schema$" -v`
mkdir -p /mnt/mysqldump
for DATABASE_NAME in $DATABASE
do
[ ! -e "/mnt/mysqldump/${DATABASE_NAME}.sql" ]&&{
mysqldump -uroot $DATABASE_NAME > /mnt/mysqldump/${DATABASE_NAME}.sql
echo -e "\033[32m${DATABASE_NAME}.sql is backup!!\033[0m"
}||{
read -p "
[S]kip [B]ackup [O]verwrite
Please input action: " ACTION
ACTION=`echo $ACTION|tr 'A-Z' 'a-z'`
case $ACTION in
s)
;;
b)
mysqldump -uroot $DATABASE_NAME > /mnt/mysqldump/${DATABASE_NAME}_backup.sql
echo -e "\033[34m${DATABASE_NAME}_backup.sql is backup!!\033[0m"
;;
o)
mysqldump -uroot $DATABASE_NAME > /mnt/mysqldump/${DATABASE_NAME}.sql
echo -e "\033[31m${DATABASE_NAME}.sql is overwrite!!\033[0m"
;;
exit)
echo -e "\033[37mbye\033[0m"
exit 0
;;
*)
echo error
esac
}
done
[root@desktop27 mnt]#
二、
脚本要求:
执行 ./auto_ssh.sh 172.25.254.51 westos
172.25.254.51 为IP
westos 为密码
执行脚本后,自动登陆 172.25.254.51,并保持登陆
[root@desktop27 mnt]# vim auto_ssh.sh
[root@desktop27 mnt]# cat auto_ssh.sh
#!/bin/bash
/usr/bin/expect << EOF
spawn ssh root@$1
expect {
"yes/no" { send "yes\r";exp_continue }
"password" { send "$2\r" }
}
interact
EOF
[root@desktop27 mnt]# chmod +x /mnt/auto_ssh.sh
[root@desktop27 mnt]# ./auto_ssh.sh 172.25.254.51 westos
spawn ssh root@172.25.254.51
root@172.25.254.51's password: [root@desktop27 mnt]#
[root@desktop27 mnt]# vim auto_ssh.sh
[root@desktop27 mnt]# cat auto_ssh.sh
#!/bin/bash
/usr/bin/expect << EOF
spawn ssh root@$1
expect {
"yes/no" { send "yes\r";exp_continue }
"password" { send "$2\r" }
}
expect eof
EOF
[root@desktop27 mnt]# ./auto_ssh.sh 172.25.254.51 westos
spawn ssh root@172.25.254.51
root@172.25.254.51's password:
Last login: Tue Jun 26 12:06:02 2018 from 172.25.254.127
[root@server27 ~]# [root@desktop27 mnt]#
三、
批处理脚本要求:
检测同一局域网下开启的所有主机,并抓取所有主机的主机名称和IP的对应列表,把列表保存在/mnt/ip_host.list文件中
[root@desktop27 mnt]# vim host.sh
[root@desktop27 mnt]# cat host.sh
#!/bin/bash
Auto_Connect()
{
/usr/bin/expect << EOF
set timeout 5
spawn ssh root@172.25.254.$IP_NUM hostname
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "westos\r" }
}
expect eof
EOF
}
for IP_NUM in {45..50}
do
ping -c1 -w1 172.25.254.$IP_NUM &> /dev/null &&{
Host_Name=`Auto_Connect | grep -E "authenticity|fingerprint|connecting|password|spawn|Warning" -v`
}
echo $Host_Name 172.25.254.$IP_NUM | sed 's/\r//g' >> /mnt/ip_host.list
done
[root@desktop27 mnt]# sh host.sh
[root@desktop27 mnt]# cat ip_host.list
##主机 46——48 不存在
172.25.254.45
172.25.254.46
172.25.254.47
172.25.254.48
server27.example.com 172.25.254.49
foundation50.ilt.example.com 172.25.254.50
[root@desktop27 mnt]#
四、
服务自动部署示例
脚本要求:
执行脚本 lamp.sh
脚本执行后部署好论坛,并设定 apache 的网络接口为 8080
真机中执行脚本
#!/bin/bash
Auto_Discuz()
{
/usr/bin/expect << EOF
set timeout 30
spawn ssh root@$1
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "westos\r" }
}
expect "]#" { send "yum install httpd -y\r" }
expect "]#" { send "yum install mariadb-server -y\r"}
expect "]#" { send "yum install php-mysql.x86_64 -y\r"}
expect "]#" { send "systemctl start httpd\r" }
expect "]#" { send "systemctl start mariadb\r" }
expect eof
EOF
}
Auto_Connect()
{
/usr/bin/expect << EOF
set timeout 30
spawn ssh root@$1
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "westos\r" }
}
expect "]#" { send "cd /var/www/html/\r" }
expect "]#" { send "unzip /var/www/html/Discuz_X3.2_SC_UTF8.zip >> /dev/null \r" }
expect "]#" { send "chmod 777 /var/www/html/upload/ -R\r" }
expect "]#" { send "systemctl restart httpd\r" }
expect eof
EOF
}
Auto_Httpd()
{
/usr/bin/expect << EOF
set timeout 30
spawn ssh root@$1
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "westos\r" }
}
expect "]#" { send "sed "/^Listen/cListen 8080" -i /etc/httpd/conf/httpd.conf\r" }
expect "]#" { send "yum restart httpd -y\r" }
expect eof
EOF
}
yum install expect -y
Auto_Discuz $1
scp /home/kiosk/Downloads/Discuz_X3.2_SC_UTF8.zip root@$1:/var/www/html
Auto_Connect $1
firefox -new-tab $1/upload/install
Auto_Httpd $1