shell进阶和计划任务

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

[20:52:46 root@CentOS8 ~]\ [#cat expect.sh 
#!/usr/bin/expect
spawn ssh 10.0.0.109
expect {
	"yes/no" { send "yes\n" ; exp_continue }
	"password" { send "123456\n" }
}
interact
[20:54:16 root@CentOS8 ~]\ [#chmod +x expect.sh
[20:54:39 root@CentOS8 ~]\ [#./expect.sh
spawn ssh 10.0.0.109
root@10.0.0.109‘s password:
Last login: Sat Aug 14 20:52:32 2021 from 10.0.0.105
[20:54:57 root@CentOS7 ~]\ [#exit
logout
Connection to 10.0.0.109 closed.

shell脚本实现远程登录:

[21:34:05 root@CentOS8 ~]\ [#cat shell_expect.sh 
#!/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
[21:34:21 root@CentOS8 ~]\ [#chmod +x shell_expect.sh 
[21:34:29 root@CentOS8 ~]\ [#./shell_expect.sh 10.0.0.109 root 123456
spawn ssh root@10.0.0.10
root@10.0.0.109‘s password: 
Last login: Sat Aug 14 21:30:58 2021 from 10.0.0.105

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

[21:48:12 root@CentOS8 data]\ [#cat min_max.sh
#!/bin/bash
#
###################################
#Auntor:                 Zhaoyaxuan
#QQ:                      907620409
#Email:            907620409@qq.com
#Date:           2021-08-14 21:40:39
#Description:                script
###################################
declare -i min max
declare -a nums
for ((i=0;i<10;i++));do
	nums[$i]=$RANDOM
	[ $i -eq 0 ] && min=${nums[0]} && max=${nums[0]} && continue
	[ ${nums[$i]} -gt $max ] && max=${nums[$i]} && continue
	[ ${nums[$i]} -lt $min ] && min=${nums[$i]}
done
echo "ALL Numbers are ${nums[*]}"
echo Max is $max
echo Min is $min
[21:48:19 root@CentOS8 data]\ [#chmod +x min_max.sh
[21:48:30 root@CentOS8 data]\ [#bash min_max.sh
ALL Numbers are 17785 4631 17727 19062 14535 15120 6926 20604 7355 24110
Max is 24110
Min is 4631

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

[15:24:00 root@CentOS8 ~]\ [#cat number.sh
#!/bin/bash
#
###################################
#Auntor:                 Zhaoyaxuan
#QQ:                      907620409
#Email:            907620409@qq.com
#Date:           2021-08-15 15:21:30
#Description:                script
###################################
declare -a number
for (( i=0; i<10; i++ ));do
 	 number[$i]=$RANDOM
done
echo "before sort:"
echo ${number[@]}
declare -i n=10
for (( i=0; i<n-1; i++ ));do
	for (( j=0; j<n-1-i; j++ ));do
	let next=$j+1
	if (( ${number[$j]} < ${number[$next]} ));then
	tmp=${number[$next]}
	number[$next]=${number[$j]}
	number[$j]=$tmp
	fi
	done
done
echo "after sort:"
echo ${number[*]}
echo "the max integer is ${number[0]},the min integer is ${number[$(( n-1 ))]}"
[15:24:10 root@CentOS8 ~]\ [#chmod +x number.sh
[15:24:13 root@CentOS8 ~]\ [#bash  number.sh
before sort:
984 30551 31434 32264 19109 16360 11345 12374 7813 14151
after sort:
32264 31434 30551 19109 16360 14151 12374 11345 7813 984
the max integer is 32264,the min integer is 984

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

cat /ect/cpuinfo 查看cpu信息
uptime 查看负载
w 查看负载
mpstat 显示CPU相关统计
vmstat 虚拟内存信息
iostat 统计CPU和设备IO信息
top 提供动态的实时进程状态
htop 增强版的top命令。
总计top命令:
running 运行态   ready 就绪态    stopped 停止态(暂停于内存,但不会被调度,出发手动启动)
zombie 僵尸态(结束进程,父进程结束前,子进程不关闭,杀死父进程可以关闭僵尸态的子进程)
睡眠态分两种,可终端:interruptable,不可中断:uninterruptable
top命令栏:
	us:用户空间   sy:内核空间   ni:调整nice时间   id:空闲   wa:等待IO时间  hi:硬中断
	si:软中断(模式切换)  st:虚拟机偷走的时间

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

[13:55:52 root@CentOS8 data]\ [#cat ping_test.sh 
#!/bin/bash
#
###################################
#Auntor:                 Zhaoyaxuan
#QQ:                      907620409
#Email:            907620409@qq.com
#Date:           2021-08-15 13:49:43
#Description:                script
###################################
NET=192.168.0
for ID in {1..254};do
	{
		ping -c1 -W1 $NET.$ID &> /dev/null && echo $NET.$ID is success || echo $NET.$ID is fail
	}
done
[13:56:22 root@CentOS8 data]\ [#chmod +x ping_test.sh 
[13:56:36 root@CentOS8 data]\ [#bash ping_test.sh 
192.168.0.1 is fail
192.168.0.2 is success
192.168.0.3 is fail
192.168.0.4 is fail
......

while循环编写脚本

[14:13:54 root@CentOS8 data]\ [#cat while_ping_test.sh
#!/bin/bash
#
###################################
#Auntor:                 Zhaoyaxuan
#QQ:                      907620409
#Email:            907620409@qq.com
#Date:           2021-08-15 14:09:58
#Description:                script
###################################
NET=192.168.0
declare -i i=1
while [ $i -le 254 ];do
	ping -c1 -W1 $NET.$i &> /dev/null
	if [ $? -eq 0 ];then
		echo $NET.$i is success
	else
		echo $NET.$i is fail
	fi
	let i++
done
[14:14:01 root@CentOS8 data]\ [#chmod +x while_ping_test.sh 
[14:14:11 root@CentOS8 data]\ [#bash while_ping_test.sh 
192.168.0.1 is fail
192.168.0.2 is success
192.168.0.3 is fail
192.168.0.4 is fail
......

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

[14:40:16 root@CentOS8 ~]\ [#cat /data/back.sh 
#!/bin/bash
#
###################################
#Auntor:                 Zhaoyaxuan
#QQ:                      907620409
#Email:            907620409@qq.com
#Date:           2021-08-15 14:27:39
#Description:                script
###################################
[ -d /backup ] || mkdir /backup
tar -Jcpvf /backup/ectbak-`date -d ‘-1 day‘ +‘%F-%H‘`.tar.xz /etc
[14:40:31 root@CentOS8 ~]\ [#chmod +x /data/back.sh 
[14:40:41 root@CentOS8 ~]\ [#crontab -l
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
30 1 * * 1-5 /data/back.sh

shell进阶和计划任务

上一篇:Photoshop 照片加上纹理背景及调色


下一篇:shell的文件锁操作