第六周学习作业

1、编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。 expect 实现远程登录主机 [root@centos84 data]# cat expect_ssh #!/usr/bin/expect 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: 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: 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 shell-ssh.sh #!/bin/bash ip=$1 user=$2 password=$3 sshpass -p $password ssh -o StrictHostKeyChecking=no $user@$ip [root@centos79 data]# [root@centos79 data]# sh shell-ssh.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: 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: 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: 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: 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 shell.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 #!/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" [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 #!/bin/bash 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 #!/bin/bash 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 /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 #!/bin/bash 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 etcbak.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 etcbak.sh #!/bin/bash [ ! -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/etcbak.sh

提供优质的网站源码大全,小程序、APP、H5、支付、游戏、区块链、商城、直播、影音、小说、公众号等源码下载。
精品源码资源网 » 第六周学习作业
赞助VIP 享更多特权,立即登录下载海量资源
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡