Ubuntu网络配置及shell脚本
一、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
二、使用expect和shell脚本两种形式实现远程登录主机
2.1 expect脚本方式
配置远程服务器sshd允许root远程登录
#远程服务器环境
# 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
#expect
Command 'expect' not found, but can be installed with:
snap install expect # version 5.45-7snap0, or
apt install expect
本地环境安装expect
#apt install expect
Reading package lists... Done
Building dependency tree
expect远程登录脚本文件
# 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 xsd@172.20.200.131
xsd@172.20.200.131'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调用expect方式
#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 永不宕机
三、编写脚本生成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.
四、编写脚本输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序
#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
来源:oschina
链接:https://my.oschina.net/u/4347242/blog/4929994