第十周
1、Ubuntu系统网络配置总结(包括主机名、网卡名称、网卡配置)
#修改主机名
wang@ubuntu:~$ hostnamectl set-hostname ubuntu1804.magedu.org
wang@ubuntu:~$ hostname
ubuntu.bear.test
wang@ubuntu:~$ cat /etc/hostname
ubuntu.bear.test
#修改网卡名称
wang@ubuntu:~$ 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 fq_codel state UP group default qlen 1000
link/ether 00:0c:29:3b:e2:82 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.204/24 brd 10.0.0.255 scope global dynamic ens33
valid_lft 1333sec preferred_lft 1333sec
inet6 fe80::20c:29ff:fe3b:e282/64 scope link
valid_lft forever preferred_lft forever
wang@ubuntu:~$ sudo -i
[sudo] password for wang:
root@ubuntu:~#vim /etc/default/grub
##修改GRUB_CMDLINE_LINUX="net.ifnames=0"
#生效新的grub.cfg文件
root@ubuntu:~# grub-mkconfig -o /boot/grub/grub.config
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.3.0-18-generic
Found initrd image: /boot/initrd.img-5.3.0-18-generic
done
#重启生效
root@ubuntu:~# reboot
#配置网卡自动获取IP
wang@ubuntu:~$ cat /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes
修改网卡配置文件后需执行命令生效:
root@ubuntu1804:~#netplan apply
#配置网卡实现静态IP
root@ubuntu:/etc/netplan# cat 01-netcfg.yaml
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses:
- 10.0.0.204/24
- 192.168.101.100/24
gateway4: 192.168.101.9
nameservers:
search: [bear.test, bear.org]
addresses: [180.76.76.76, 8.8.8.8, 1.1.1.1]
#查看路由
root@ubuntu:/etc/netplan# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.101.9 0.0.0.0 UG 0 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.101.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
#查看DNS
root@ubuntu:/etc/netplan# systemd-resolve --status
Global
LLMNR setting: no
MulticastDNS setting: no
DNSOverTLS setting: no
DNSSEC setting: no
DNSSEC supported: no
DNSSEC NTA: 10.in-addr.arpa
16.172.in-addr.arpa
168.192.in-addr.arpa
17.172.in-addr.arpa
18.172.in-addr.arpa
19.172.in-addr.arpa
20.172.in-addr.arpa
21.172.in-addr.arpa
22.172.in-addr.arpa
23.172.in-addr.arpa
24.172.in-addr.arpa
25.172.in-addr.arpa
26.172.in-addr.arpa
27.172.in-addr.arpa
28.172.in-addr.arpa
29.172.in-addr.arpa
30.172.in-addr.arpa
31.172.in-addr.arpa
corp
d.f.ip6.arpa
home
internal
intranet
lan
local
private
test
Link 2 (eth0)
Current Scopes: DNS
DefaultRoute setting: yes
LLMNR setting: yes
MulticastDNS setting: no
DNSOverTLS setting: no
DNSSEC setting: no
DNSSEC supported: no
DNS Servers: 180.76.76.76
8.8.8.8
1.1.1.1
DNS Domain: bear.test
bear.org
2、编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)。
# expect 脚本
[02:16:35 root@bear data]#vim expectssh
#!/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
~
[02:29:02 root@bear data]#chmod +x expectssh
[02:29:14 root@bear data]#./expectssh 10.0.0.219 root welcome1
spawn ssh root@10.0.0.219
root@10.0.0.219's password:
Last login: Sat Feb 13 18:26:12 2021 from 10.0.0.1
[18:29:32 root@centos7 ~]#whoami
root
[18:29:46 root@centos7 ~]#hostname -I
10.0.0.219 10.0.0.207 192.168.122.1
#Shell 脚本
[02:37:03 root@bear data]#vim expectshe.sh
#!/bin/bash
#
#********************************************************************
#Author: stella tang
#Mail: 359564666@126.com
#Date: 2021-02-14
#FileName: expectshe.sh
#URL: https://jianshu.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
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 eof
EOF
[02:36:28 root@bear data]#bash expectshe.sh 10.0.0.219 root welcome1
spawn ssh root@10.0.0.219
root@10.0.0.219's password:
Last login: Sat Feb 13 18:29:32 2021 from 10.0.0.222
[18:36:49 root@centos7 ~]#
3、生成10个随机数保存于数组中,并找出其最大值和最小值
[02:41:47 root@bear data]#cat max-min.sh
#!/bin/bash
#
#********************************************************************
#Author: stella tang
#Mail: 359564666@126.com
#Date: 2021-02-14
#FileName: max-min.sh
#URL: https://jianshu.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
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
[02:41:35 root@bear data]#bash max-min.sh
All numbers are 16221 2135 6403 17330 5695 15218 15308 28964 5600 18018
Max is 28964
Min is 2135
4、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
暂时不做