Ubuntu network configuration and shell script
1. Ubuntu system network configuration
1.1 Modify the host name
Use hostnamectl command to modify the host name
#测试环境
#uname -a
Linux dl-homework.linux.com 4.15.0-112-generic #113-Ubuntu SMP Thu Jul 9 23:41:39 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
#计算机名字永久生效
#hostnamectl set-hostname changename
root@dl-homework ~]#hostname
changename
Change the configuration file /etc/hostname to modify the host name
#vim /etc/hostname #更改配置文件,将计算机名称改成abc,重启生效
abc
#hostname abc # 计算机名称临时更改成abc
1.2 Change network card name
Change the network card device name ens33 back to eth0
#增加net.ifnames=0 biosdevname=0这两个参数
#vim /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"
#生成grub.cfg
#grub-mkconfig -o /boot/grub/grub.cfg
Sourcing file `/etc/default/grub'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.15.0-112-generic
Found initrd image: /boot/initrd.img-4.15.0-112-generic
done
#reboot
Ubuntu's network configuration file /etc/netplan/01-netcfg.yaml, adjust this file accordingly.
#vim /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: no
dhcp6: no
eth1:
dhcp4: no
dhcp6: no
eth2:
dhcp4: no
dhcp6: no
eth3:
dhcp4: no
dhcp6: no
bonds:
bond0:
interfaces:
- eth0
- eth2
parameters:
mode: active-backup
mii-monitor-interval: 100
#修改网卡配置文件后需执行命令生效:
#netplan apply
2. Use expect and shell script to realize remote login to the host
2.1 expect script method
Configure the remote server sshd to allow root remote login
#远程服务器环境
# uname -a
Linux localhost.localdomain 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
# ip add sh
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,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP group default qlen 1000
link/ether 00:0c:29:e5:86:86 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP group default qlen 1000
link/ether 00:0c:29:e5:86:86 brd ff:ff:ff:ff:ff:ff
4: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 00:0c:29:e5:86:86 brd ff:ff:ff:ff:ff:ff
inet 172.20.200.131/24 brd 172.20.200.255 scope global noprefixroute dynamic bond0
valid_lft 1665sec preferred_lft 1665sec
inet6 fe80::7c8f:bc5b:a7d:5eda/64 scope link noprefixroute
valid_lft forever preferred_lft forever
#vim /etc/ssh/sshd_config
PermitRootLogin yes #打开此选项
Need to check whether expect is installed in the local environment
#expect
Command 'expect' not found, but can be installed with:
snap install expect # version 5.45-7snap0, or
apt install expect
Install expect in local environment
#apt install expect
Reading package lists... Done
Building dependency tree
expect remote login script file
# vim ssh_expct
#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send $pass\n }
}
interact
Test remote login
#./ssh_expct 172.20.200.131 xsd xsd123
spawn ssh [email protected]
[email protected]'s password:
Last login: Tue Jan 26 02:59:46 2021 from 172.20.200.138
PRD System!!
_oo0oo_
088888880
88" . "88
(| -_- |)
0\ = /0
___/'---'\___
.' \\| |// '.
/ \\||| : |||// \
/_ ||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' |_/ |
\ .-\__ '-' __/-. /
___'. .' /--.--\ '. .'___
."" '< '.___\_<|>_/___.' >' "".
| | : '- \'.;'\ _ /';.'/ - ' : | |
\ \ '_. \_ __\ /__ _/ .-' / /
====='-.____'.___ \_____/___.-'____.-'=====
'=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 iii 永不宕机
2.2 Shell call expect mode
#vim shell_ssh.sh
#!/bin/bash
ip=$1
user=$2
pass=$3
sshpass -p $pass ssh -o StrictHostKeyChecking=no $user@$ip
Test remote login
#./shell_ssh.sh 172.20.200.131 xsd xsd123
Last login: Tue Jan 26 04:11:36 2021 from 172.20.200.138
PRD System!!
_oo0oo_
088888880
88" . "88
(| -_- |)
0\ = /0
___/'---'\___
.' \\| |// '.
/ \\||| : |||// \
/_ ||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' |_/ |
\ .-\__ '-' __/-. /
___'. .' /--.--\ '. .'___
."" '< '.___\_<|>_/___.' >' "".
| | : '- \'.;'\ _ /';.'/ - ' : | |
\ \ '_. \_ __\ /__ _/ .-' / /
====='-.____'.___ \_____/___.-'____.-'=====
'=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 iii 永不宕机
3. Write a script to generate 10 random numbers and save them in an array, and find the maximum and minimum values
#vim big_small.sh
#!/bin/bash
#
#********************************************************************
#Author: xinshidong
#QQ: 6736080
#Date: 2021-01-26
#FileName: big_small.sh
#URL:
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
declare -i min max
declare -a nums
for ((i=0;i<10;i++));do
num[$i]=$RANDOM
[ $i -eq 0 ] && min=${num[$i]} && max=${num[$i]} && continue
[ ${num[$i]} -gt $max ] && max=${num[$i]}
[ ${num[$i]} -lt $min ] && min=${num[$i]}
done
echo "All num are ${num[*]}"
echo "The max num is $max;the min num is $min."
# ./big_small.sh #执行脚本
All num are 22342 23327 32610 32026 17990 17874 26047 1707 28289 23487
The max num is 32610;the min num is 1707.
4. Write a script to input a number of values into the array, and use the bubble algorithm to sort in ascending or descending order
#vim num_sort.sh
#!/bin/bash
#
#********************************************************************
#Author: xinshidong
#QQ: 6736080
#Date: 2021-01-26
#FileName: num_sort.sh
#URL:
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
i=0
declare -a num
PS3="Please input nunbeers first,then chose the sort method or quit the script:"
select MENU in Ascending Descending Quit;do
case $REPLY in
1)
echo "The array are:${num[*]}"
j=${#num[*]}
for ((k=0;k<j;k++));do
for((m=0;m<j;m++));do
min=${num[k]}
if [ $min -lt ${num[m]} ];then
num[k]=${num[m]}
num[m]=$min
fi
done
done
echo "The Ascending sort:${num[*]}"
break
;;
2)
echo "The array are:${num[*]}"
j=${#num[*]}
for ((k=0;k<j;k++));do
for((m=0;m<j;m++));do
min=${num[k]}
if [ $min -gt ${num[m]} ];then
num[k]=${num[m]}
num[m]=$min
fi
done
done
echo "The Descending sort:${num[*]}"
break
;;
3)
break
;;
*)
if [[ $REPLY =~ ^[0-9]+$ ]];then
num[$i]=$REPLY
let i++
else
echo "Input is incorrect."
fi
esac
done
#测试升序排列数组
# ./num_sort.sh
1) Ascending
2) Descending
3) Quit
#向属组中填入数字
Please input nunbeers first,then chose the sort method or quit the script:345
Please input nunbeers first,then chose the sort method or quit the script:676
Please input nunbeers first,then chose the sort method or quit the script:999t
Input is incorrect.
Please input nunbeers first,then chose the sort method or quit the script:8887
Please input nunbeers first,then chose the sort method or quit the script:766
Please input nunbeers first,then chose the sort method or quit the script:4554
#升序排列
Please input nunbeers first,then chose the sort method or quit the script:1
The array are:345 676 8887 766 4554
The Ascending sort:345 676 766 4554 8887
#测试降序排列属组
# ./num_sort.sh
1) Ascending
2) Descending
3) Quit
#向属组中填入数字
Please input nunbeers first,then chose the sort method or quit the script:67766
Please input nunbeers first,then chose the sort method or quit the script:8989
Please input nunbeers first,then chose the sort method or quit the script:3434
Please input nunbeers first,then chose the sort method or quit the script:56
Please input nunbeers first,then chose the sort method or quit the script:767
#降序排列
Please input nunbeers first,then chose the sort method or quit the script:2
The array are:67766 8989 3434 56 767
The Descending sort:67766 8989 3434 767 56