1、Ubuntu系統(tǒng)網(wǎng)絡(luò)配置總結(jié)(包括主機名、網(wǎng)卡名稱赶舆、網(wǎng)卡配置)
主機名修改
#1 命令修改
root@ubuntu:~# cat /etc/hostname
ubuntu
root@ubuntu:~# hostnamectl set-hostname ubuntu1804
root@ubuntu:~# cat /etc/hostname
ubuntu1804
#2 直接修改hostname文件
root@ubuntu:~# vim /etc/hostname
ubuntu1804.test
root@ubuntu:~# cat /etc/hostname
ubuntu1804.test
網(wǎng)卡名稱的修改
#手工修改
root@ubuntu:~# vi /etc/default/grub
#在文件中把下面一行做修改
GRUB_CMDLINE_LINUX=""
#修改后
GRUB_CMDLINE_LINUX="net.ifnames=0"
#sed命令修改
root@ubuntu:~# sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#' /etc/default/grub
#修改后使配置生效
#方法1 生成新的grub.cfg 文件
root@ubuntu:~# grub-mkconfig -o /boot/grub/grub.cfg
#方法2 命令生效
root@ubuntu:~# update-grub
#方法3 重啟生效
root@ubuntu:~# reboot
網(wǎng)卡的配置
#方法1 自動獲取IP
root@ubuntu1804:~# 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:
ens33:
dhcp4: yes
#方法2 配置靜態(tài)IP
root@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:
ens33:
addresses: [10.0.0.10/24]
gateway4: 10.0.0.2
nameservers:
addresses: [202.101.172.35,202.101.172.47]
##修改網(wǎng)卡配置文件后需執(zhí)行命令生效
root@ubuntu:~# netplan apply
2、編寫腳本實現(xiàn)登陸遠程主機祭饭。(使用expect和shell腳本兩種形式)芜茵。
#方法1
[root@centos7 ~]# vim expect_login.sh
#!/usr/bin/expect
set ip 10.0.0.8
set user root
set password 123456
set timeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n";exp_continue }
"password" { send "$password\n" }
}
interact
[root@centos7 ~]# chmod +x expect_login.sh
[root@centos7 ~]# ./expect_login.sh
spawn ssh root@10.0.0.8
The authenticity of host '10.0.0.8 (10.0.0.8)' can't be established.
ECDSA key fingerprint is SHA256:suSC6K0UIGayYlZBfnpZClLdT+8n0b/a+vgPRbUoeEA.
ECDSA key fingerprint is MD5:b3:90:95:86:fa:e8:fa:e5:ed:b0:7f:2c:17:1a:2b:f2.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.8' (ECDSA) to the list of known hosts.
root@10.0.0.8's password:
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Fri Jan 29 03:34:22 2021 from 10.0.0.1
[root@centos8 ~]#
#方法2
[root@centos7 ~]# vim shell_login.sh
#!/bin/bash
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
3、生成10個隨機數(shù)保存于數(shù)組中倡蝙,并找出其最大值和最小值
[root@centos7 ~]# vim max_min.sh
#!/bin/bash
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
[root@centos7 ~]# chmod +x max_min.sh
[root@centos7 ~]# ./max_min.sh
All numbers are 6045 22477 28851 3375 14140 19251 6128 11183 1775 17015
Max is 28851
Min is 1775
4九串、輸入若干個數(shù)值存入數(shù)組中,采用冒泡算法進行升序或降序排序
[root@centos7 ~]# vim maopao.sh
#!/bin/bash
i=0
flag=0
wrong=0
#保存數(shù)組值
while true;do
read -p "請輸入數(shù)字(輸入quit退出):" num
if [[ "$num" == ^"" ]];then
echo "請輸入數(shù)字 你已經(jīng)輸錯了$wrong 次"
let wrong++
fi
if [ $wrong -eq 5 ];then
echo "你居然能輸錯 $wrong 次 我受不了你了"
break
elif [[ $num == quit ]];then
break
elif [[ $num =~ ^[-]{0,1}[[:digit:]]{0,}[.]{0,1}[[:digit:]]{0,}[[:digit:]]$ ]];then #篩選出 整數(shù) 小數(shù) 負數(shù)
echo $num
array[$i]=$num
let i++
else
echo "請輸入數(shù)字 你已經(jīng)輸錯了$wrong 次"
let wrong++
fi
done
#比較兩個數(shù)大小
compare () {
echo $1 > /app/0324/tmp
echo $2 >> /app/0324/tmp
max=$(cat /app/0324/tmp|sort -nr|head -1)
if [[ "$max" == "$1" ]];then
flag=1
else
flag=0
fi
}
#冒泡排序
length=${#array[*]}
for i in $(seq 0 $length);do
for j in $(seq 0 $[$length-$i]);do
t=$[$j+1]
compare ${array[$j]} ${array[$t]}
if [ $flag -eq 1 ];then
temp=${array[$j]}
array[$j]=${array[$t]}
array[$t]=$temp
fi
done
done
echo "從小到大排序為:${array[*]}"
[root@centos7 ~]# chmod +x maopao.sh
[root@centos7 ~]# ./maopao.sh
請輸入數(shù)字(輸入quit退出):4565
4565
請輸入數(shù)字(輸入quit退出):56
56
請輸入數(shù)字(輸入quit退出):12
12
請輸入數(shù)字(輸入quit退出):quit
從小到大排序為:4565 56 12