第六周作业
编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)
expect
[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
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
[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
#!/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

浙公网安备 33010602011771号