第六周作业
编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)
expect
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | [root@ansible srete] # cat telnet #!/usr/bin/expect set user user set passwd passwd spawn telnet [lindex $argv 0] expect { "Username" { send "$user\n" ;exp_continue } "Password" { send "$passwd\n" } } interact<br> alias telnet= '/root/srete/telnet' [root@ansible srete] # telnet 10.1.81.254 spawn telnet 10.1.81.254 Trying 10.1.81.254... Connected to 10.1.81.254. Escape character is '^]' . Warning: Telnet is not a secure protocol, and it is recommended to use Stelnet. Login authentication Username:admin admin Password: Info: The max number of VTY users is 10, and the number of current VTY users on line is 1. The current login time is 2022-04-12 09:08:43+00:00. <OFFICE_8F> |
shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | [root@ansible srete] # cat telnet.sh #!/bin/bash # #******************************************************************** #Author: 陈濛 #Date: 2022-04-12 #FileName: telnet.sh #Description: Create from cm #Copyright (C): 2022 All rights reserved #******************************************************************** user=*** passwd =*** expect <<! spawn telnet $1 expect { "Username" { send "$user\n" ;exp_continue } "Password" { send "$passwd\n" } } expect eof ! [root@ansible srete] # sh telnet.sh 10.1.81.254 spawn telnet 10.1.81.254 Trying 10.1.81.254... Connected to 10.1.81.254. Escape character is '^]' . Warning: Telnet is not a secure protocol, and it is recommended to use Stelnet. Login authentication Username:admin admin Password: Info: The max number of VTY users is 10, and the number of current VTY users on line is 1. The current login time is 2022-04-12 09:41:35+00:00. <OFFICE_8F> |
生成10个随机数保存于数组中,并找出其最大值和最小值(此题直接抄的)
#!/bin/bash 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
输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序(这题也是直接抄的)
read -p "请输入数值个数:" COUNT declare -a nums for ((i=0;i<$COUNT;i++));do num[$i]=$RANDOM done echo "The initial array:" echo ${num[@]} declare -i n=$COUNT for (( i=0; i<n-1; i++ ));do for (( j=0; j<n-1-i; j++ ));do let x=$j+1 if (( ${num[$j]} < ${num[$x]} ));then #从大到小排列 tmp=${num[$x]} num[$x]=${num[$j]} num[$j]=$tmp fi done done echo "After sort:" echo ${num[*]} echo "the max integer is $num,the min integer is ${num[$((n-1))]}"
总结查看系统负载的几种命令,总结top命令的指标大概什么含义(不要求全部写出来)
uptime和w:负载查询
mpstat:显示CPU相关统计
top和htop: 查看进程实时状态
free:内存空间
vmstat:查看虚拟内存信息
iostat:统计cpu和设备IO信息
iotop:监视磁盘IO
iftop:显示网络带宽使用情况
nload:查看网络实时吞吐量
iptraf-ng:网络监视工具
dstat:系统资源统计
glances:综合监视工具
lsof:查看进程打开文件
top命令可以查看系统运行时间,平均负载,任务运行状态,如running,sleeping,stopped,zomblel;可以查看cpu利用率,内存利用率;可以查看进程PID,用户,运行时间,命令以及进程使用的cpu和内存占比
编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
[root@ansible srete]# cat scanip.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #!/bin/bash # #******************************************************************** #Author: chenmeng #QQ: 903575406 #Date: 2022-04-16 #FileName: scanip.sh #Description: A test script #Copyright (C): 2022 All rights reserved #******************************************************************** net=$1 red= "\e[1;31m" end= "\e[0;m" scanip(){ for i in {1..254}; do ip=$net.$i { ping -c2 -w2 $ip &> /dev/null && echo "$ip up" || echo "$ip down" }& shift done wait } scanip up=`scanip | awk -F ' ' '{print $2}' | uniq -c | head -1 | awk -F ' +' '{print $2}' ` down=`scanip | awk -F ' ' '{print $2}' | uniq -c | tail -1 | awk -F ' +' '{print $2}' ` echo -e "$red 处于UP状态的IP数量为 $up$end" echo -e "$red 处于DOWN状态的UP数量为$down$end" |
[root@ansible ~]# cat scanip_w.sh #!/bin/bash # #******************************************************************** #Author: chenmeng #QQ: 903575406 #Date: 2022-04-18 #FileName: scanip_w.sh #Description: A test script #Copyright (C): 2022 All rights reserved #******************************************************************** net=$1 i=1 while [ $i -le 254 ];do ip=$net.$i { ping -c2 -w2 $ip &> /dev/null && echo "$ip up" || echo "$ip down" }& let i++ done wait
每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
编写备份脚本
[root@ansible srete]# cat backupetc.sh #!/bin/bash # #******************************************************************** #Author: chenmeng #QQ: 903575406 #Date: 2022-04-18 #FileName: backupetc.sh #Description: A test script #Copyright (C): 2022 All rights reserved #******************************************************************** dir=ectbak-`date -d "-1 day" +%F-%T` tar -Jcvf /backup/$dir.tar.xz /etc/
创建计划任务
[root@ansible srete]# crontab -l 30 1 * * 1-5 /root/srete/backupetc.sh
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现