阅读 460

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 <

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

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

原文:https://www.cnblogs.com/zhaoyaxuan/p/15145192.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐