Linux学习-第十周

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

(1) 修改主机名
1) 临时修改,重启主机后生效
hostname 主机名
2) 永久生效
hostnamectl set-hostname 主机名
或者
直接修改/etc/hostname文件
(2) 修改网卡名称
1) 修改配置文件为下面形式
[root@Ubuntu16 ~]#vi /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0"
或者sed修改
[root@Ubuntu16 ~]#sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#' /etc/default/grub
2) 生效新的grub.cfg文件
[root@Ubuntu16 ~]#grub-mkconfig -o /boot/grub/grub.cfg
或者
[root@Ubuntu16 ~]#update-grub
[root@Ubuntu16 ~]#grep net.ifnames /boot/grub/grub.cfg
linux /vmlinuz-4.4.0-186-generic root=UUID=c5ca1e75-fc41-4e63-a49f-294364753cc4 ro net.ifnames=0
linux /vmlinuz-4.4.0-186-generic root=UUID=c5ca1e75-fc41-4e63-a49f-294364753cc4 ro net.ifnames=0
linux /vmlinuz-4.4.0-186-generic root=UUID=c5ca1e75-fc41-4e63-a49f-294364753cc4 ro recovery nomodeset dis_ucode_ldr net.ifnames=0
3) 重启生效
[root@Ubuntu16 ~]# reboot
(3) 网卡配置
Ubuntu从17.10开始,已放弃在/etc/network/interfaces里配置IP地址,即使配置也不会生效,而是改成netplan方式,配置写在/etc/netplan/01-netcfg.yaml或者类似名称的yaml文件里
1) 配置自动获取IP
[root@Ubuntu18 ~]# cat /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: yes
修改网卡配置文件后需执行命令生效:
[root@Ubuntu18 ~]#netplan apply
2) 配置静态IP
[root@Ubuntu18 ~]#vim /etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
addresses: [192.168.8.10/24,10.0.0.10/8]
gateway4: 192.168.8.1
nameservers:
search: [magedu.com, magedu.org]
addresses: [180.76.76.76, 8.8.8.8, 1.1.1.1]
配置好后,查看方式如下:
a) 查看ip和gateway
[root@Ubuntu18 ~]#ip addr
[root@Ubuntu18 ~]#route -n
b) 查看DNS
[root@Ubuntu18 ~]#systemd-resolve --status



















































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

(1) expect脚本形式
[root@CentOS8 script]#cat expect4
#!/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" }
}
interac
[root@CentOS8 script]#./expect4 10.0.0.71 root root123
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Jan 26 17:15:54 2021 from 10.0.0.1
[root@CentOS7-1 ~]#
(2) shell脚本形式
[root@CentOS8 script]#vim expect5.sh

















    #!/bin/bash
    ip=$1
    user=$2
    password=$3
expect <<EOF
spawn ssh $user@$ip          

expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
    }
    expect "]#" { send  "echo abc | mail -s just_test root\n" }
 expect "]#" { send "exit\n" }
 expect eof
EOF

[root@CentOS8 script]#./expect5.sh 10.0.0.71 root root123

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

[root@CentOS8 script]#cat sort.sh

    #!/bin/bash
    declare -i min max
    declare -a number
    for ((i=0;i<10;i++));do
number[$i]=$RANDOM
        [ $i -eq 0 ] &&  min=${number[$i]} &&  max=${number[$i]}  &&  continue
        [ ${number[$i]} -gt $max ] && max=${number[$i]}
        [ ${number[$i]} -lt $min ] && min=${number[$i]}
    done
    echo ${number[]}
    echo max=$max  min=$min*

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

(1) 升序排列

[root@CentOS8 script]#vim bubble_sort.sh

#!/bin/bash
##############################################
#File Name: bubble_sort.sh
#Version: V1.0
#Author: LiRui
#Created Time: 2021-02-01 09:23:53
#Description: The test script
##############################################
n=$1                                                                                                                                                                  
declare -a number
for ((i=0;i<$n;i++));do
number[$i]=$RANDOM
done
echo ${number[@]}
echo " ************************"

if [ $n -gt 1 ];then
    for ((j=$[$n-1];j>0;j--));do
        for ((k=0;k<$j;k++));do
            if [ ${number[$k]} -lt ${number[$k+1]} ];then
                temp=${number[$k]}
                number[$k]=${number[$k+1]}
                number[$k+1]=$temp
            fi
        done
        echo ${number[@]}
    done
elif [ $n -eq 1 ];then
    echo ${number[@]}    
fi

[root@CentOS8 script]#bash bubble_sort.sh 4
31629 30118 5066 27319


31629 30118 27319 5066
31629 30118 27319 5066
31629 30118 27319 5066
[root@CentOS8 script]#bash bubble_sort.sh 6
623 445 11470 11797 1367 15913




623 11470 11797 1367 15913 445
11470 11797 1367 15913 623 445
11797 11470 15913 1367 623 445
11797 15913 11470 1367 623 445
15913 11797 11470 1367 623 445



(2) 降序排列
[root@CentOS8 script]#vim bubble3_sort.sh

#!/bin/bash
n=$1
declare -a number
for ((i=0;i<$n;i++));do
number[$i]=$RANDOM
done
echo ${number[@]}
echo " ************************"

if [ $n -gt 1 ];then
    for ((j=$[$n-1];j>0;j--));do
        for ((k=0;k<$j;k++));do
            if [ ${number[$k]} -gt ${number[$k+1]} ];then                                                                                                             
                temp=${number[$k]}
                number[$k]=${number[$k+1]}
                number[$k+1]=$temp
            fi
        done
        echo ${number[@]}
    done
elif [ $n -eq 1 ];then
    echo ${number[@]}    
fi

[root@CentOS8 script]#bash bubble3_sort.sh 4
15524 21396 8625 29902


15524 8625 21396 29902
8625 15524 21396 29902
8625 15524 21396 29902
[root@CentOS8 script]#bash bubble3_sort.sh 7
6985 26599 10050 26870 5789 8515 14775




6985 10050 26599 5789 8515 14775 26870
6985 10050 5789 8515 14775 26599 26870
6985 5789 8515 10050 14775 26599 26870
5789 6985 8515 10050 14775 26599 26870
5789 6985 8515 10050 14775 26599 26870
5789 6985 8515 10050 14775 26599 26870




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章