第十周作业

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

root@ubuntu01:~# hostnamectl set-hostname ubuntu.test.com
root@ubuntu01:~# hostname
ubuntu.test.com
root@ubuntu01:~# echo $HOSTNAME
ubuntu01
root@ubuntu01:~# exit
exit
zhang@ubuntu01:~$ hostname
ubuntu.test.com
zhang@ubuntu01:~$ sudo -i
[sudo] password for zhang: 

修改网卡名:

root@ubuntu:~# sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#'
/etc/default/grub
root@ubuntu:~# grub-mkconfig -o /boot/grub/grub.cfg
root@ubuntu:~# reboot

修改网卡配置:

#配置自动获取:
root@ubuntu:~# cat /etc/netplan/00-installer-config.yaml 
network:
  version: 2
  renderer: networkd
  ethernets:
	eth0:
	  dhcp4: yes

#配置静态地址:
root@ubuntu:~# cat /etc/netplan/00-installer-config.yaml 
network:
  renderer: networkd
  ethernets:
    eth0:
      addresses:
      - 192.168.246.30/24
      gateway4: 192.168.246.1
      nameservers:
        search: [magedu.com,aliyun.com]
        addresses: [221.228.255.1,223.5.5.5]
  version: 2
root@ubuntu:~# ip addr
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:8b:2d:ce brd ff:ff:ff:ff:ff:ff
    inet 192.168.246.30/24 brd 192.168.246.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe8b:2dce/64 scope link 
       valid_lft forever preferred_lft forever

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

expect远程登陆

脚本固定登陆信息:

#!/usr/bin/expect
set ip 192.168.246.10
set password 123456
set user root
spawn ssh $user@$ip
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send "$password\n" }
}
interact

通过位置变量识别登陆信息:

#!/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远程登陆

脚本固定登陆信息:

#!/bin/bash
user=root
password=123456
ip=192.168.246.10
expect << EOF
set timeout 20
spawn ssh $user@$ip
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send "$password\n" }
}
expect "#" { send "ls -l /data\n" }
expect eof
EOF

通过位置变量识别登陆信息:

#!/bin/bash
ip=$1
user=$2
password=$3
expect << EOF
set timeout 20
spawn ssh $user@$ip
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send "$password\n" }
}
expect "#" { send "ls -l /data\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 numbers are ${nums[*]}"
echo Max is $max
echo Min is $min

[root@centos7-7 scripts]#bash array.sh 
ALL numbers are 13287 1727 29902 12899 16004 27162 31288 10043 18204 1810
Max is 31288
Min is 1727

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

#!/bin/bash
read -p "请输入数值个数:" COUNT
declare -a nums
for ((i=0;i<$COUNT;i++));do
    num[$i]=$RANDOM
done
echo "The initial array:"
echo ${num[@]}

declare -i n=$COUNT
for (( i=0; i<n-1; i++ ));do
    for (( j=0; j<n-1-i; j++ ));do
        let x=$j+1
        if (( ${num[$j]} < ${num[$x]} ));then
 #从大到小排列
           tmp=${num[$x]}
            num[$x]=${num[$j]}
            num[$j]=$tmp
        fi
    done
done
echo "After sort:"
echo ${num[*]}
echo "the max integer is $num,the min integer is ${num[$((n-1))]}"

[root@centos7-7 scripts]#bash array1.sh 
请输入数值个数:5
The initial array:
6990 13303 21741 11095 25247
After sort:
25247 21741 13303 11095 6990
the max integer is 25247,the min integer is 6990

posted @ 2021-01-26 14:50  gody2019  阅读(156)  评论(1)    收藏  举报