1、编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。
expect 实现远程登录主机
[root@centos84 data]# cat expect_ssh
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
interact
[root@centos84 data]#
[root@centos84 data]# ./expect_ssh 172.18.66.46 root abc@123
spawn ssh root@172.18.66.46
The authenticity of host '172.18.66.46 (172.18.66.46)' can't be established.
ECDSA key fingerprint is SHA256:zPJJYaIo1Yfj013LXx6XmL/QF3/gwzvIdhM5iHscwSk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.18.66.46' (ECDSA) to the list of known hosts.
root@172.18.66.46's password:
Last login: Wed Dec 22 02:47:47 2021 from 172.18.66.45
[root@centos79 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:b3:f4:32 brd ff:ff:ff:ff:ff:ff
inet 172.18.66.46/24 brd 172.18.66.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::e132:5b27:135f:b310/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@centos79 ~]#
shell实现远程登录主机
[root@centos79 data]# cat
ip=$1
user=$2
password=$3
sshpass -p $password ssh -o StrictHostKeyChecking=no $user@$ip
[root@centos79 data]#
[root@centos79 data]# sh 172.18.66.47 root abc@123
Warning: Permanently added '172.18.66.47' (ECDSA) to the list of known hosts.
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Wed Dec 22 03:16:46 2021 from 172.18.66.45
[root@centos84 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:68:87:02 brd ff:ff:ff:ff:ff:ff
inet 172.18.66.47/24 brd 172.18.66.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe68:8702/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:3b:f6:74 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:3b:f6:74 brd ff:ff:ff:ff:ff:ff
需要咨询一下教练: 使用这个方式 登录到远程主机后,操作不了任何东西,一会就自动退回到原主机
#!/bin/bash
ip=$1
user=$2
password=$3
expect <<EOF
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
expect eof
EOF
[root@centos79 data]# sh 172.18.66.47 root abc@123
spawn ssh root@172.18.66.47
root@172.18.66.47's password:
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Wed Dec 22 04:02:50 2021 from 172.18.66.46
[root@centos84 ~]#
## 这个位置就卡住了 ,过一会就自动退回到原主机,不知道这是为什么?
[root@centos79 data]#
2、生成10个随机数保存于数组中,并找出其最大值和最小值。
[root@centos84 ~]# 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[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"
[root@centos84 ~]#
[root@centos84 ~]# sh max_min.sh
All numbers are 30626 21667 13864 1251 1589 3151 21699 3660 9779 27371
Max is 30626
Min is 1251
[root@centos84 ~]#
-------------------------------------------------
[root@centos84 ~]# sh max_min.sh
All numbers are 13006 29886 26973 22700 9368 32520 31518 2388 9692 8072
Max is 32520
Min is 2388
[root@centos84 ~]# cat max_min.sh
declare -i min max
declare -a nums
for ((i=0;i<10;i++));do
nums[$i]=$RANDOM
if [ $i -eq 0 ];then
min=${nums[0]}
max=${nums[0]}
continue
fi
if [ ${nums[$i]} -gt $max ];then
max=${nums[$i]}
continue
fi
if [ ${nums[$i]} -lt $min ];then
min=${nums[$i]}
fi
done
echo "All numbers are ${nums[*]}"
echo "Max is $max"
echo "Min is $min"
[root@centos84 ~]#
3、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
[root@centos84 ~]# sh list_max_min.sh
10个随机数:543 29432 2546 28487 23486 21951 19830 20433 30270 6601
整理后的数组是(升序):543 2546 6601 19830 20433 21951 23486 28487 29432 30270
整理后的数组是(降序):30270 29432 28487 23486 21951 20433 19830 6601 2546 543
[root@centos84 ~]# cat list_max_min.sh
declare -a list
for ((n=0;n<10;n++));do
list[$n]=$RANDOM
done
echo "10个随机数:${list[*]}"
let=${#list[*]}
for ((j=0;j<$let;j++ ));do
for (( i=0;i<$let-1;i++));do
if [ ${list[$i]} -gt ${list[$i+1]} ];then
x=${list[$i]}
list[$i]=${list[$i+1]}
list[$i+1]=$x
# echo ${list[$i]} "-------" ${list[$i+1]}
fi
done
done
echo "整理后的数组是(升序):${list[*]}"
for ((j=0;j<$let;j++ ));do
for (( i=0;i<$let-1;i++));do
if [ ${list[$i]} -lt ${list[$i+1]} ];then
x=${list[$i]}
list[$i]=${list[$i+1]}
list[$i+1]=$x
# echo ${list[$i]} "-------" ${list[$i+1]}
fi
done
done
echo "整理后的数组是(降序):${list[*]}"
[root@centos84 ~]#
4、总结查看系统负载的几种命令,总结top命令的指标大概什么含义(不要求全部写出来)
pstree -p 查看进程树 显示PID 最小化安装需要安装psmisc
ps -ef 显示进程当前状态的快照 ps aux 比-ef显示丰富一些
uptime 一次性输出当前系统负载情况
18:35:19 当前时间
up 11:52, 系统开机到现在的时间
3 users, 当前远程到系统的账户数量
load average: 0.00, 0.00,0.00 每1、5、15分钟的平均负
top 动态显示系统负载情况
系统负载
18:35:19 当前时间
up 11:52, 系统开机到现在的时间
3 users, 当前远程到系统的账户数量
load average: 0.00, 0.00,0.00 每1、5、15分钟的平均负
进程
Tasks: 179 total, 系统中进程总数
1 running, 正在运行的进程数量
178 sleeping, 睡眠中的进程数量
0 stopped, 停止的进程数量
0 zombie 僵死进程数量
CPU使用情况
%Cpu(s): 0.3 us, 用户模式占用cpu的使用率
0.3 sy, 系统模式占用cpu的使用率
0.0 ni, 优先级发生改变的用户进程的占用cpu使用率
99.3 id, cpu空闲率
0.0 wa, 等待输入/输出的进程的占用cpu的使用率
0.0 hi, 强制中断请求占用cpu的使用率
0.0 si, 软中断请求占用cpu的使用率
0.0 st 虚拟机占用cpu的使用率
内存使用情况
MiB Mem : 782.2 total, 物理内存的总值
140.8 free, 空闲的内存值
336.0 used, 已经使用的内存值
305.4 buff/cache 缓冲区的内存值
SWAP分区的使用情况
MiB Swap: 2048.0 total, SWAP分区的总值
1992.5 free, SWAP分区的空闲值
55.5 used. SWAP分区的使用值
323.3 avail Mem SWAP分区理论上使用值
快捷键排序:
P:以占据的CPU百分比,%CPU
M:占据内存百分比,%MEM
T:累积占据CPU时长,TIME+
首部信息显示:
uptime信息:l命令
tasks及cpu信息:t命令
cpu分别显示:1 (数字)
memory信息:m命令
退出命令:q
修改刷新时间间隔:s
终止指定进程:k
保存文件:W
top命令栏位信息
us:用户空间
sy:内核空间
ni:调整nice时间
id:空闲
wa:等待IO时间
hi:硬中断
si:软中断(模式切换)
st:虚拟机偷走的时间
5、编写脚本,使用for和while分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"
for循环检测主机IP是否存活
[root@centos84 ~]# cat for_check_ip.sh
NETID=192.168.0.
for HOSTID in {1..254};do
{
if /bin/ping -c1 -w1 $NETID$HOSTID &> /dev/null ; then
echo "$NETID$HOSTID is success!"
else
echo "$NETID$HOSTID is fail!"
fi
} &
done
[root@centos84 ~]# sh for_check_ip.sh | wc -l
254
[root@centos84 ~]# sh for_check_ip.sh
192.168.0.1 is success!
192.168.0.2 is success!
192.168.0.128 is success!
192.168.0.129 is success!
192.168.0.130 is success!
[root@centos84 ~]# 192.168.0.65 is fail!
192.168.0.73 is fail!
192.168.0.14 is fail!
192.168.0.48 is fail!
192.168.0.10 is fail!
192.168.0.23 is fail!
192.168.0.36 is fail!
……
while 检测主机IP是否存活
[root@centos84 ~]# cat while_check_ip.sh
NETID=192.168.0.
declare -i HOSTID=1
while [ $HOSTID -lt 255 ] ; do
/bin/ping -c1 -w1 $NETID$HOSTID &> /dev/null
if [ $? -eq 0 ];then
echo "$NETID$HOSTID is success!"
else
echo "$NETID$HOSTID is fail!"
fi
let HOSTID++
done
[root@centos84 ~]# sh while_check_ip.sh
192.168.0.1 is success!
192.168.0.2 is success!
192.168.0.3 is fail!
192.168.0.4 is fail!
192.168.0.5 is fail!
192.168.0.6 is fail!
192.168.0.7 is fail!
192.168.0.8 is fail!
192.168.0.9 is fail!
…
192.168.0.128 is success!
192.168.0.129 is success!
192.168.0.130 is success!
…
6、每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间。
[root@centos84 ~]# sh
[root@centos84 ~]# ls -l /backup/
total 3964
-rw-r--r--. 1 root root 4057276 Dec 22 11:42 etcbck-2021-12-21-11.tar.xz
[root@centos84 ~]# cat
[ ! -d /backup ] && mkdir -p /backup
/usr/bin/tar -Jcvf /backup/etcbck-`date -d '-1 day' +'%F-%H'`.tar.xz /etc &> /dev/null
[root@centos84 ~]# crontab -l
30 1 * * 1-5 sh /root/
















