linux_6

1、编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。

#使用expect远程登录
[root@centos8 ~]#dnf -y install expect
[root@centos8 ~]#yum info expect
Last metadata expiration check: 0:01:31 ago on Fri 01 Oct 2021 01:14:22 AM CST.
Installed Packages
Name         : expect
Version      : 5.45.4
...
[root@centos8 ~]#vim remote_login_expect.sh
#!/usr/bin/expect
spawn ssh 10.0.0.128
expect {
    "*yes/no" { send "yes\n";exp_continue }
    "password" { send "password\n";exp_continue }
    "*]#" { send "hostname -I\r" }
}
interact

#执行验证
[root@centos8 ~]#chmod +x remote_login_expect.sh
[root@centos8 ~]#./login.sh
spawn ssh 10.0.0.128
root@10.0.0.128's password: 
Last login: Fri Oct  1 01:47:30 2021 from 10.0.0.131
[root@centos7 ~]#hostname -I
10.0.0.128

#shell脚本调用expect

#!/bin/bash
ip=10.0.0.128
user=root
passwd=486.
expect <<EOF
spawn ssh $user@$ip
expect {
        "*yes/no*"  { send "yes\n";exp_continue }
        "password" { send "$passwd\n";exp_continue}
        "*]#" { send "hostname -I\r" }
}
expect eof
EOF

#执行脚本验证
[root@centos8 ~]#./login.sh 
spawn ssh root@10.0.0.128
root@10.0.0.128's password: 
Last login: Fri Oct  1 02:15:03 2021 from 10.0.0.131
[root@centos7 ~]#hostname -I
10.0.0.128 
[root@centos7 ~]#

2、生成10个随机数保存于数组中,并找出其最大值和最小值

[root@centos7 ~]#cat max_min.sh 
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 $min ] && min=${nums[$i]} 
done 
echo array are ${nums[*]}
echo max is $max 
echo min is $min

#执行验证
[root@centos7 ~]#bash max_min.sh 
array are 13080 933 1678 2268 22196 31395 4216 19184 31842 11791
max is 3184

3、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序

#!/bin/bash
declare -a num
read -p "请输入生成随机数个数:"  number
for (( i=0;i<$number;i++ ));do
    num[$i]=$RANDOM
done
echo "before sort:${num[*]}"
declare -i n=$number
for (( i=0;i<n-1;i++ ));do
    for ((j=0;j<n-1;j++));do
        let next=$j+1
    if (( ${num[$j]} > ${num[$next]} ));then
          tmp=${num[$next]}
      num[$next]=${num[$j]}
      num[$j]=$tmp
    fi  
    done
done
echo "after sort:${num[*]}"

4、总结查看系统负载的几种命令,总结top命令的指标大概什么含义(不要求全部写出来)

uptime、mpstat、top、htop
top 提供动态的实时进程状态
PID:进程id
USER:用户
PR:权重,优先级
VIRT:虚拟内存
RES:常驻内存
SHR:共享内存
S:进程状态,R表示运行
%CPU:cpu的占用率
%MEM:内存的占用率
TIME+:执行时间
COMMAND:进程的名称/路径

5、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"

#!/bin/bash
net="192.168.0"
multi_ping() {
  ping -c2 -i0.1 -W1 $1 &> /dev/null
  if [ $? -wq 0 ];then
    echo "success!"
  else
    echo "fail!"
  fi
}
for i in {0..255}
do
  multi_ping $net.$i &
done
wait

6、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间

[root@centos8 ~]#cat bak_etc.sh
#!/bin/bash
[ -d /backup ] || mkdir /backup
bak_file="/backup/etcbak-`date -d "-1 days" +%Y-%m-%d-%H`.tar.xz"
tar -cxvf /$bak_file /etc

[root@centos8 ~]#crontab -e
30 1 * * 1-5 /root/bak_etc.sh
posted @ 2021-10-19 19:09  火火7412  阅读(28)  评论(0编辑  收藏  举报