linux(6)
Q1:编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。
A1:Expect脚本:
#!/usr/bin/expect
spawn ssh root@192.168.31.122
expect {
"*(yes/no*" { send "yes\r";exp_continue }
"*password:*" { send "root123\r";exp_continue }
"*]#" { send "hostname -I\r" }
}
expect eof
Shell脚本:
#!/bin/bash
Address=192.168.31.122
User=root
Passwd=root123
expect <<EOF
spawn ssh $User@$Address
expect {
"*(yes/no*" { send "yes\r";exp_continue }
"*password:*" { send "root123\r";exp_continue }
"*]#" { send "hostname -I\r" }
}
expect eof
Q2:生成10个随机数保存于数组中,并找出其最大值和最小值
A2:#!/bin/bash
declare -i min max
declare -a nums
for ((i=0;i<10:i++));do
nums[$i]=$RANDOM
[ $i -eq 0 ] && min =${nums[$i]} && max=${nums[$i]}&& continue
[ ${nums[$i]} -gt $max ] && max=${nums[$i]}
[ ${nums[$i]} -lt $max ] && max=${nums[$i]}
done
echo "All numbers are ${nums[*]}"
echo Max is $max
echo Min is $min
Q3:输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
A3:(这题网上参考,不太明白)
[root@centos7 expects]# cat arry.sh
read -p "请输入数值个数:" COUNT
declare -a nums
for ((i=0;i<$COUNT;i++));do
num[$i]=$RANDOM
done
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 "排列之后:${num[*]}"
echo "the max integer is $num,the min integer is ${num[$((n-1))]}"
[root@centos7 expects]# ./arry.sh
请输入数值个数:10
原始数组:25138 26980 11797 4296 25516 25625 20062 15422 18409 25784
排列之后:26980 25784 25625 25516 25138 20062 18409 15422 11797 4296
the max integer is 26980,the min integer is 4296
[root@centos7 expects]# vim arry.sh
[root@centos7 expects]# ./arry.sh
请输入数值个数:10
原始数组:10799 2623 13598 26123 4088 32235 22436 290 30159 32077
排列之后:32235 32077 30159 26123 22436 13598 10799 4088 2623 290
the max integer is 32235,the min integer is 290
[root@centos7 expects]#
Q4:总结查看系统负载的几种命令,总结top命令的指标大概什么含义(不要求全部写出来)
A4:uptime:查看系统平均负载
mpstat:百分比显示CPU利用率的各项指标
top和htop:查看进程的实时状态
free:查看内存空间的使用状态
pmap:查看进程对应的内存映射,可以看到进程依赖的子模块占用的内存数量,可以以此判断OOM
vmstat:查看虚拟内存的信息,可以以用户定义的间隔不断刷新状态,能够看到内存与SWAP、磁盘之间的IO情况;
iostat:能够看到更丰富的IO性能状态,可以自定义刷新间隔判断哪块硬盘的IO比较繁忙;-x参数可以看到磁盘基于扇区的IO,队列长度,处理时间等
iotop:以top方式监控磁盘的I/O,实时监控,而且可以只显示正在执行读写的进程,提供很多非交互式参数;
iftop:显示网络带宽的使用情况,查看访问当前主机的流量的实时信息,实时连接等;
nload:只能以接口为单位查看实时吞吐量,看不到连接信息,只有速率信息
top命令的指标以及含义
Q5:编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
A5:
for
#!/bin/bash
net=192.168.234
for ip in {1..254};
do
{
ping -W1 -c 1 $net.$ip &> /dev/null && echo "ping &net.&ip is successed!" || echo "piong $net.$ip is failed!"
}&
done
wait
while
#!/bin/bash
net=192.168.234
i=1
while((i<255));
do
{
ping -W1 -C 1 $net.$i &< /dev/null && echo "ping $net.$i is success" || echo "ping $net.$i is fail"
}&
let i+=1
done
wait
~
Q6:每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
A6:
[21:58:04 root@test01 backup]#crontab -l
30 1 * * 1-5 /home/backup.sh
[21:58:22 root@test01 backup]#cat /home/backup.sh
#!/bin/bash
/usr/bin/tar -cJPf /backup/etcbak-`date -d yesterday +%F-%H`.tar.xz /etc/
[21:58:00 root@test01 backup]#ls
etcbak-2021-11-06-21.tar.xz
[21:58:28 root@test01 backup]#date +%F-%H
2021-11-07-21
浙公网安备 33010602011771号