1、编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。
expect脚本形式:
[root@bogon ~]# vim expect_ssh #!/usr/bin/expect spawn ssh zhangchuangfei@192.168.56.130 expect { "yes/no" { send "yes\n";exp_continue } "password" { send "111111\n" } } interact
赋予可执行权限,并且执行脚本登录主远程主机
[root@bogon ~]# chmod +x expect_ssh [root@bogon ~]# ./expect_ssh spawn ssh zhangchuangfei@192.168.56.130 The authenticity of host '192.168.56.130 (192.168.56.130)' can't be established. ECDSA key fingerprint is SHA256:nORYtq45pDQbNZASvO1d0UI1XACeKppzEkD8RvOp3ls. ECDSA key fingerprint is MD5:d4:47:32:8d:07:eb:a7:82:aa:6c:e9:a8:b0:2c:42:bd. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.56.130' (ECDSA) to the list of known hosts. zhangchuangfei@192.168.56.130's password: Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.15.0-112-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Sat Nov 6 11:32:40 CST 2021 System load: 0.11 Processes: 161 Usage of /: 9.9% of 18.41GB Users logged in: 1 Memory usage: 46% IP address for ens33: 192.168.56.130 Swap usage: 0% * Super-optimized for small spaces - read how we shrank the memory footprint of MicroK8s to make it the smallest full K8s around. https://ubuntu.com/blog/microk8s-memory-optimisation 172 packages can be updated. 128 updates are security updates. New release '20.04.3 LTS' available. Run 'do-release-upgrade' to upgrade to it. Last login: Sat Nov 6 11:05:25 2021 To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details. zhangchuangfei@ubuntu1804:~$
shell脚本形式:
[root@bogon ~]# vim ssh.sh #!/bin/bash #**************************************************** #Author zhangchuangfei #Email 798438093@qq.com #FileName ssh.sh #CreateTime 2021-11-06 11:12:45 #Description #**************************************************** ip=$1 user=$2 password=$3 expect <<EOF spawn ssh ${user}@${ip} set timeout 2 expect { "yes/no" { send "yes\n";exp_continue } "password" { send "${password}\n" } } expect "]$" { send "exit\n" } expect eof EOF
执行脚本
[root@bogon ~]# ./ssh.sh 192.168.56.130 zhangchuangfei 111111 spawn ssh zhangchuangfei@192.168.56.130 zhangchuangfei@192.168.56.130's password:
[root@bogon ~]#
2、生成10个随机数保存于数组中,并找出其最大值和最小值
脚本内容:
# cat max_min.sh #!/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"
执行脚本:
# ./max_min.sh All numbers are 9832 13212 31611 31322 13046 24536 161 30266 27792 7591 Max is 31611 Min is 161
3、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
脚本内容:
# cat maopao.sh #!/bin/bash values=(100 45 789 10 58) for ((i=1;i<${#values[*]};i++)) do for ((j=0;j<${#values[*]}-$i;j++)) do if [ ${values[j]} -gt ${values[$(($j+1))]} ] then tmp=${values[j]} values[j]=${values[$(($j+1))]} values[$(($j+1))]=$tmp fi done done echo ${values[*]}
执行脚本:
# ./maopao.sh 10 45 58 100 789
4、总结查看系统负载的几种命令,总结top命令的指标大概什么含义(不要求全部写出来)
常用的查看系统负载的几个命令有:
top、uptime、vmstat、sar等等
top命令的指标含义:
top - 21:01:47 up 33 days, 11:46, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 158 total, 1 running, 153 sleeping, 4 stopped, 0 zombie %Cpu(s): 0.1 us, 0.2 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 7699556 total, 5279680 free, 618304 used, 1801572 buff/cache KiB Swap: 8126460 total, 8126460 free, 0 used. 6376892 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 10122 root 20 0 0 0 0 S 0.7 0.0 0:00.26 kworker/3:0 1374 root 20 0 639668 49880 4932 S 0.3 0.6 16:27.76 salt-minion 1 root 20 0 125460 3884 2560 S 0.0 0.1 11:32.03 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.76 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:05.12 ksoftirqd/0 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H 7 root rt 0 0 0 0 S 0.0 0.0 0:04.50 migration/0 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh 9 root 20 0 0 0 0 S 0.0 0.0 10:38.72 rcu_sched 10 root rt 0 0 0 0 S 0.0 0.0 0:08.24 watchdog/0
第一行解释:
top - 21:01:47 up 33 days, 11:46, 1 user, load average: 0.00, 0.01, 0.05
21:01:47:系统当前时间
up 33 days, 11:46:系统开机到现在经过了33天
1 users:当前1用户在线
load average:0.00, 0.01, 0.05:系统1分钟、5分钟、15分钟的CPU负载信息.
备注:load average后面三个数值的含义是最近1分钟、最近5分钟、最近15分钟系统的负载值。这个值的意义是,单位时间段内CPU活动进程数。如果你的机器为单核,那么只要这几个值均<1,代表系统就没有负载压力,如果你的机器为N核,那么必须是这几个值均<N才可认为系统没有负载压力。
第二行解释:
Tasks: 158 total, 1 running, 153 sleeping, 4 stopped, 0 zombie
158 total:当前有158个任务
1 running:1个任务正在运行
153 sleeping:153个进程处于睡眠状态
0 stopped:停止的进程数
0 zombie:僵死的进程数
第三行解释:
%Cpu(s): 0.1 us, 0.2 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
0.1%us:用户态进程占用CPU时间百分比
0.2%sy:内核占用CPU时间百分比
0.0%ni:renice值为负的任务的用户态进程的CPU时间百分比。nice是优先级的意思
99.8%id:空闲CPU时间百分比
0.0%wa:等待I/O的CPU时间百分比
0.0%hi:CPU硬中断时间百分比
0.0%si:CPU软中断时间百分比
第四行解释:
KiB Mem : 7699556 total, 5279680 free, 618304 used, 1801572 buff/cache
7699556k total:物理内存总数
618304k used: 使用的物理内存
5279680k free:空闲的物理内存
1801572k cached:用作缓存的内存
第五行解释:
KiB Swap: 8126460 total, 8126460 free, 0 used. 6376892 avail Mem
8126460k total:交换空间的总量
0k used: 使用的交换空间
8126460k free:空闲的交换空间
6376892k cached:缓存的交换空间
最后一行:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
PID:进程ID
USER:进程的所有者
PR:进程的优先级
NI:nice值
VIRT:占用的虚拟内存
RES:占用的物理内存
SHR:使用的共享内存
S:进行状态 S:休眠 R运行 Z僵尸进程 N nice值为负
%CPU:占用的CPU
%MEM:占用内存
TIME+: 占用CPU的时间的累加值
COMMAND:启动命令
5、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
for脚本示例:
# cat for.sh #!/bin/bash for i in `seq 1 254`;do ping -c 1 192.168.0.${i} [ $? -eq 0 ] && echo "sucess!" || echo "fail!" done
while脚本示例:
# cat while.sh #!/bin/bash i=1 while [ $i -le 255 ] do ping -c 1 192.168.0.${i} [ $? -eq 0 ] && echo "sucess!" || echo "fail!" let i++ done
6、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间
# crontab -l 30 1 * * 1-5 /usr/bin/tar -Jcf /backup/etcbak-`date -d yesterday +%Y-%m-%d-%H`.tar.xz /etc