Ubuntu 네트워크 구성 및 셸 스크립트
1. Ubuntu 시스템 네트워크 구성
1.1 호스트 이름 수정
hostnamectl 명령을 사용하여 호스트 이름 수정
#测试环境
#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
구성 파일 / etc / hostname을 변경하여 호스트 이름을 수정합니다.
#vim /etc/hostname #更改配置文件,将计算机名称改成abc,重启生效
abc
#hostname abc # 计算机名称临时更改成abc
1.2 네트워크 카드 이름 변경
네트워크 카드 장치 이름 ens33을 다시 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의 네트워크 구성 파일 /etc/netplan/01-netcfg.yaml, 그에 따라이 파일을 조정하십시오.
#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. expect 및 셸 스크립트를 사용하여 호스트에 대한 원격 로그인을 실현합니다.
2.1 예상 스크립트 방법
루트 원격 로그인을 허용하도록 원격 서버 sshd 구성
#远程服务器环境
# 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 #打开此选项
예상이 로컬 환경에 설치되어 있는지 확인해야 함
#expect
Command 'expect' not found, but can be installed with:
snap install expect # version 5.45-7snap0, or
apt install expect
로컬 환경에서 예상 설치
#apt install expect
Reading package lists... Done
Building dependency tree
원격 로그인 스크립트 파일이 필요합니다.
# 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
원격 로그인 테스트
#./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 쉘 호출 예상 모드
#vim shell_ssh.sh
#!/bin/bash
ip=$1
user=$2
pass=$3
sshpass -p $pass ssh -o StrictHostKeyChecking=no $user@$ip
원격 로그인 테스트
#./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. 10 개의 난수를 생성하고 배열에 저장하는 스크립트를 작성하고 최대 값과 최소값을 찾습니다.
#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. 배열에 여러 값을 입력하는 스크립트를 작성하고 버블 알고리즘을 사용하여 오름차순 또는 내림차순으로 정렬
#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