马哥教育-第十周作业

1、Ubuntu系统网络配置总结(包括主机名、网卡名称、网卡配置)

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

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

4、输入若干个数值存

 

1.ubuntu系统网络配置总结(包括主机名、网卡名称、网卡配置)

主机名:

查看:hostname

设置:hostnamectl set-hostname  name  该命令直接生效

文件:/etc/hostname

网卡名称:

vim /etc/default/grub

GRUB_CMDLINE_LINUX="net.ifnames=0"

 grub-mkconfig -o /boot/grub/grub.cfg

update-grub

网卡配置:

vim /etc/netplan/01-netcfg.yaml

 

 

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

expect:

[root@centos8 ~]# cat expect1
#!/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

shell:

[root@centos8 ~]# cat expect.sh
#!/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 "]#" { send "hostname -I\n" }
expect "]#" { send "exit\n" }
expect eof
EOF

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

#!/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]}
    [ ${nums[$i]} -lt $min ] && min=${nums[$i]}
done
echo "ALL number are ${nums[*]}"
echo Max is $max
echo Min is $min

 

4.输入若干个数值存

#!/bin/bash
declare -a nums
read -p "请输入生成随机数个数:" number
for (( i=0;i<$number;i++ ));do
     nums[$i]=$RANDOM
done
echo "before sort:${nums[*]}"
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 (( ${nums[$j]} > ${nums[$next]} ));then 
             tmp=${nums[$next]}
             nums[$next]=${nums[$j]}
             nums[$j]=$tmp
         fi
        done
done
echo "after sort:${nums[*]}"
echo "the Min integer is ${nums[0]},the max integer is ${nums[$(( n-1 ))]}"

 

posted @ 2021-06-15 14:56  berniee  阅读(74)  评论(0)    收藏  举报