1、编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。

(1)expect形式:

#!/usr/bin/expect

spawn ssh 192.168.52.111

expect {

       "yes/no" { send "yes\n";exp_continue }

       "password" { send "123456\n" }

}

interact

[root@localhost ~]# chmod +x expect1.exp

[root@localhost ~]#

[root@localhost ~]# ./expect1.exp  

spawn ssh 192.168.52.111

root@192.168.52.111's password:  

Last login: Sat Apr  9 13:16:12 2022 from 192.168.52.139

[root@localhost ~]#  

(2)shell形式

#!/bin/bash

ip=$1

user=$2

password=$3

expect <<EOF

set timeout 20

spawn ssh $user@$ip

expect {

       "yes/no" { send "yes\n";exp_continue }

       "password" { send "$password\n" }

}

expect eof

EOF

[root@localhost ~]# chmod +x expect1.sh

[root@localhost ~]# ./expect1.sh 192.168.52.111 root 123456

spawn ssh root@192.168.52.111

root@192.168.52.111's password:  

Last login: Sat Apr  9 13:17:09 2022 from 192.168.52.139

[root@localhost ~]#

2、生成10个随机数保存于数组中,并找出其最大值和最小值

#!/bin/bash

#********************************************************************#

#date:2022年 04月 10日 星期日 01:25:37 CST

#FileName:arr_10num_compare.sh

#URL: http://www.magedu.com

#Description: The test script

#Copyright (C): 2022 All rights reserved

#********************************************************************#

declare -a nums

for((i=0;i<10;i++));do

       nums[$i]=$RANDOM

       [ "$i" -eq 0 ] && { max=${nums[$i]};min=${nums[$i]}; }

       [ ${nums[$i]} -gt $max ] && max=${nums[$i]}

       [ ${nums[$i]} -lt $max ] && min=${nums[$i]}

done

echo "The array: ${nums[*]}"

echo "max: $max"

echo "min: $min"

3、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序

#!/bin/bash

#********************************************************************#

#date:2022年 04月 09日 星期六 23:36:14 CST

#FileName:paixu_maopao.sh

#URL: http://www.magedu.com

#Description: The test script

#Copyright (C): 2022 All rights reserved

#********************************************************************#

declare -a nums

i=0

while :;do

        read -p "请输入参与排序的数值:"  INPUT

        num[$i]=$INPUT

        let i+=1

        if [ "$i" -gt 1 ];then

                read -p "1)继续输入;2)开始比较"  INPUT

                case $INPUT in

                1)

                        continue

                        ;;

                2)

                        break

                        ;;

                esac

        fi

done

echo "The initial array: ${num[*]}"

i=0

j=0

n=${#num[*]}

for((i=0;i<n-1;i++));do

        for((j=0;j<n-1-i;j++));do

                if [ "${num[$j]}" -gt "${num[$[$j+1]]}" ];then

                        temp=${num[$j]}

                        num[$j]=${num[$[$j+1]]}

                        num[$[$j+1]]=$temp

                fi

        done

done

echo "The array of After sort: ${num[*]}"

4、总结查看系统负载的几种命令,总结top命令的指标大概什么含义(不要求全部写出来)

查看系统负载的命令:w,uptime,top,htop

当前时间,系统已启动的时间,当前上线人数,系统平均负载(1、 5、 15分钟的平均负载,一般不会超过1,超过5时建议警报)

任务情况:任务数,运行态,睡眠态,停止态,僵死态

CPU情况:us:用户空间,sy:内核空间(系统空间),ni:调整nice时间,id:空闲,wa:等待IO时间,hi:硬中断,si:软中断(模式切换),st:虚拟机偷走的时间

内存情况:总内存,空闲内存,已使用内存,缓存

swap空间:总空间,空闲swap空间,已使用swap空间,可使用内存

5、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"

(1)for形式

#!/bin/bash

#********************************************************************#

#date:2022年 04月 10日 星期日 01:43:10 CST

#FileName:scanhost.sh

#URL: http://www.magedu.com

#Description: The test script

#Copyright (C): 2022 All rights reserved

#********************************************************************#

NET=192.168.52

for i in {1..254};do

{ ping -c1 -W1 ${NET}.${i} &> /dev/null && { echo ${NET}.${i} :success;} || { echo ${NET}.${i} :failed;} }&

done

wait

(2)while形式

#!/bin/bash

#********************************************************************#

#date:2022年 04月 10日 星期日 01:43:10 CST

#FileName:scanhost.sh

#URL: http://www.magedu.com

#Description: The test script

#Copyright (C): 2022 All rights reserved

#********************************************************************#

NET=192.168.52

i=1

while((i<255));do

{ ping -c1 -W1 ${NET}.${i} &> /dev/null && { echo ${NET}.${i} :success;} || { echo ${NET}.${i} :failed;} }&

let i+=1

done

wait

6、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间

#!/bin/bash

#********************************************************************#

#date:2022年 04月 09日 星期六 10:52:58 CST

#FileName:backup.sh

#URL: http://www.magedu.com

#Description: The test script

#Copyright (C): 2022 All rights reserved

#********************************************************************#

if [ -d "/backup" ];then

        tar -Jcf /backup/etcbak-`date +%F-%H -d "-1day"`.tar.xz /etc

else

        echo "/backup目录不存在,创建目录" && mkdir /backup

        tar -Jcf /backup/etcbak-`date +%F-%H -d "-1day"`.tar.xz /etc

fi

配置/etc/crontab

SHELL=/bin/bash

#PATH=/sbin:/bin:/usr/sbin:/usr/bin

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

MAILTO=root


# For details see man 4 crontabs


# Example of job definition:

# .---------------- minute (0 - 59)

# |  .------------- hour (0 - 23)

# |  |  .---------- day of month (1 - 31)

# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...

# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# |  |  |  |  |

# *  *  *  *  * user-name  command to be executed

30 1 * * 1-5 root sh /root/backup.sh