linux 的 ending 脚本示例

示例一、
要求:
1)文件数量不对报错;
2)文件不存报错;
3)文件行数差异报错;
4)用户存在显示用户存在,但是不改变此用户密码;
5)当用户不存在建立用户并设定相应密码。

这里写图片描述
这里写图片描述

这里写图片描述
这里写图片描述
这里写图片描述

示例二、
要求:
1)显示所有开机的主机(这里以172.25.254.66为例);
2)保存显示的主机 I P 到 /mnt/host 的文件中。

这里写图片描述
这里写图片描述

区别:\r 和 \n

这里写图片描述

示例三、
要求:
1)显示除了1-10的数字;
2)不显示数字 4 。

这里写图片描述

基本的运算

这里写图片描述
这里写图片描述

这里写图片描述
这里写图片描述

示例四、
要求:
1)10秒倒计时;
2)1分10秒倒计时。

这里写图片描述
这里写图片描述

方法一、
这里写图片描述

sleep 1 #每秒刷新一次
[ “ S E C "=" 0 " a " MIN” = “0” ]&& exit 0 #秒和分钟都为0时退出
SEC=59 #这种方法的优点是可以自行设定周期s

这里写图片描述

方法二、
这里写图片描述

A= [ p/60] #商为分钟
B= [ p%60] #求余秒

这里写图片描述

示例五、
要求:
1)制作计算器,方法一:只能计算实数;
2)方法二:对浮点数也可以。

这里写图片描述
这里写图片描述

这里写图片描述
这里写图片描述

示例六、
要求:
1)登陆指定IP主机(这里以172.25.254.66为例);
2)并在所登陆主机建立用户。

这里写图片描述
这里写图片描述

这里写图片描述
这里写图片描述

示例七、
要求:
1)脚本执行后会备份数据库中的所有库到/mnt/mysql 目录中;
2)备份文件名称为“库名称.sql“,当文件存在报错并询问动作;
3)输入“S“跳过备份,当输入“B“时备份“库名称.sql”文件为“库名称_backup.sql“,当输入“O“时,覆盖原文件。

这里写图片描述
这里写图片描述

这里写图片描述
这里写图片描述

示例八、
要求:
1)脚本执行后部署好论坛;
2)设定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

这里写图片描述
这里写图片描述
这里写图片描述

示例九、
要求:
1)执行脚本后自动登陆 172.25.254.66 密码 westos ;
2)并保持登陆。

这里写图片描述
这里写图片描述

示例十、
要求:
1)测试教室中开启的所有主机;
2)抓取所有主机的值机名称 和 IP 的对应列表 ;
3)把列表保存在 /mnt/ip_host.list 文件中。

#!/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 {1..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

这里写图片描述

猜你喜欢

转载自blog.csdn.net/janenancy/article/details/80816598