1. 程式人生 > 其它 >No.MN55-week7Ubuntu網路配置遠端登入資料比較排序網路測試指令碼

No.MN55-week7Ubuntu網路配置遠端登入資料比較排序網路測試指令碼

1.Ubuntu系統網路配置

Ubuntu系統網路配置總結(包括主機名、網絡卡名稱、網絡卡配置)

1.1設定主機名

方法一:通過檔案修改主機名,需要reboot永久生效

#檢視主機名
root@ubuntu1804:/home/hong# hostname
ubuntu1804
#通過檔案修改主機名,需要reboot,永久生效
root@ubuntu1804:/home/hong# vim /etc/hostname 
root@ubuntu1804:/home/hong# cat /etc/hostname 
nginx.ubuntu.org
#檢視修改後主機名
root@nginx:/home/hong# hostname
nginx.ubuntu.org
root@nginx:/home/hong# echo $HOSTNAME
nginx.ubuntu.org

方法二:用hostname命令修改主機名,臨時生效,重啟主機後不存在

#用hostname命令修改主機名,臨時生效,重啟主機後不存在
root@ubuntu1804:/home/hong# hostname mysql.ubuntu.org
#修改後立即檢視
root@ubuntu1804:/home/hong# hostname
mysql.ubuntu.org
#開啟其他終端檢視
root@mysql:/home/hong# echo $HOSTNAME
mysql.ubuntu.org

1.2修改網絡卡名

第一步:修改/etc/default/grub檔案

修改方法:

  1. 手動修改/etc/default/grub
  2. 命令修改/etc/default/grub
#方法一:手動修改/etc/default/grub檔案
root@nginx:/home/hong# vim /etc/default/grub
GRUB_CMDLINE_LINUX="net.ifnames=0"       #原配置為:GRUB_CMDLINE_LINUX=""
#方法二:
root@nginx:/home/hong# sed -Ei.bak 's/^(GRUB_CMDLINE_LINUX=.*)"$"/\1net.ifnames=0"/' /etc/default/grub

#檢視修改
root@nginx:/home/hong# cat /etc/default/grub 
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'
    
GRUB_DEFAULT=0
GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=0
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX="net.ifnames=0"

第二步:生成新的grub.cfg檔案

root@nginx:/home/hong# grub-mkcofnig -o /boot/grub/grub.cfg
root@nginx:/home/hong# update-grub
root@nginx:/home/hong# grep net.ifnames /boot/grub/grub.cfg     #確認修改內容
root@nginx:/home/hong# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc
#ip a 檢視修改的網絡卡結果,網口名已經從ens33改為eth0

1.3配置網絡卡IP

  1. 設定動態IP
  2. 設定靜態IP

設定動態IP:

root@nginx:/etc/netplan# vim 01-network-manager-all.yaml 
root@nginx:/etc/netplan# cat /etc/netplan/01-network-manager-all.yaml 
# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    eth0:
    dhcp4: yes
#執行命令使配置生效
root@nginx:/etc/netplan# netplan apply

設定靜態IP:

root@nginx:/etc/netplan# vim /etc/netplan/01-network-manager-all.yaml 
root@nginx:/etc/netplan# cat /etc/netplan/01-network-manager-all.yaml 
# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    eth0:
      dhcp4: no
      addresses: [10.0.0.51/24]
      gateway4: 10.0.0.2
      nameservers:
         addresses: [8.8.8.8,8.8.4.4] 
#執行命令使配置生效
root@nginx:/etc/netplan# netplan apply   

2.遠端登入主機

編寫指令碼實現登陸遠端主機。(使用expect和shell指令碼兩種形式)

方式一:expect工具實現遠端登入主機

#編輯登入指令碼
[root@CentOS8 ~]# cat auto-remotehost-ssh.exp 
#!/usr/bin/expect
set remoteip [ lindex $argv 0 ]
set ruser [ lindex $argv 1 ]
set rpassword [ lindex $argv 2 ]
spawn ssh $ruser@$remoteip
expect {
	"yes/no" { send "yes\n"; exp_continue }
	"password" { send "$rpassword\n"}
}
interact
#給檔案執行許可權
[root@CentOS8 ~]# chmod +x auto-remotehost-ssh.exp 
[root@CentOS8 ~]# expect auto-remotehost-ssh.exp 10.0.0.82 root wdwhr
spawn ssh [email protected]
[email protected]'s password: 
Web console: https://CentOS8:9090/ or https://10.0.0.82:9090/
Last login: Tue Jun 29 08:42:04 2021 from 10.0.0.1
[root@CentOS8 ~]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:65:bd:8e brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.82/24 brd 10.0.0.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
#退出遠端環境
[root@CentOS8 ~]# exit
logout
Connection to 10.0.0.82 closed.

方式二:shell工具實現遠端登入主機

#編寫登入指令碼
[root@CentOS8 ~]# cat auto-remotehost-ssh.sh 
#!/bin/bash
rip=$1
ruser=$2
rpassword=$3
expect <<EOF
set timeout 5
spawn ssh $ruser@$rip
expect {
	"yes/no" { send "yes\n"; exp_continue }
	"password" { send "$rpassword\n"}
}
EOF
#給檔案執行許可權
[root@CentOS8 ~]# chmod +x auto-remotehost-ssh.sh
[root@CentOS8 ~]# sh auto-remotehost-ssh.sh 10.0.0.82 root wdwhr
spawn ssh [email protected]
[email protected]'s password: 
Web console: https://CentOS8:9090/ or https://10.0.0.82:9090/
Last login: Tue Jun 29 09:15:55 2021 from 10.0.0.81
[root@CentOS8 ~]# hostname -I
10.0.0.82 
#退出遠端環境
[root@CentOS8 ~]# exit
logout
Connection to 10.0.0.82 closed.

3.隨機數比較

生成10個隨機數保存於陣列中,並找出其最大值和最小值

思路:用for迴圈遍歷10次,每次用環境變數$RANDOM隨機生成一個0-32767的數,存入num陣列,同時和max和min變數值比較,並替換,直到比較完為止。

方法一:用for迴圈巢狀if-else實現最大最小值查詢

[root@localhost shscripts]# cat -n numcompare.sh 
     1	#!/bin/bash
     2	#
     3	#**********************************
     4	# Author:      wanhonron
     5	# Date:        2021-06-21
     6	# FileName:    /home/wang/shscripts/numcompare.sh
     7	# Url:         http://www.weda.com
     8	# Copyright(C):2021All rights reserved
     9	#**********************************
    10	declare -i min max
    11	declare -a nums
    12	for (( i=0; i<10; i++ )); do
    13	  nums[$i]=$RANDOM
    14	  if [ $i -eq 0 ];then
    15	    max=${nums[$i]}
    16	    min=${nums[$i]}
    17	  else
    18	    if [ "$max" -lt "${nums[$i]}" ]; then
    19	      max=${nums[$i]}
    20	    elif [ "$min" -gt "${nums[$i]}" ]; then
    21	      min=${nums[$i]}
    22	    else
    23	      true
    24	    fi
    25	  fi
    26	done
    27	echo "nums member are ${nums[*]}"	#用*萬用字元取出陣列所有元素
    28	echo max is $max
    29	echo min is $min
[root@localhost shscripts]# chmod +x numcompare.sh 
[root@localhost shscripts]# sh numcompare.sh 
nums member are 2453 8790 22437 9294 11261 1225 13227 15080 27934 24567
max is 27934
min is 1225

方法二:隨機生成10個數,比較出最大最下值

[root@localhost shscripts]# cat -n numcompare2.sh 
     1	#!/bin/bash
     2	#
     3	#**********************************
     4	# Author:      wanhonron
     5	# Date:        2021-06-21
     6	# FileName:    numcompare2.sh
     7	# Url:         http://www.weda.com
     8	# Copyright(C):2021All rights reserved
     9	#**********************************
    10	declare -i max min
    11	declare -a num
    12	for i in $(seq 10);do
    13	  num[$i]=$RANDOM
    14	  [ $i -eq 0 ] && max=${num[$i]} && min=${num[$i]} && continue
    15	  [[ ${num[$i]} -gt $max ]] && max=${num[$i]}
    16	  [[ ${num[$i]} -lt $min ]] && min=${num[$i]}
    17	done
    18	echo "num member are ${num[@]}"		#用@萬用字元取出陣列所有元素
    19	echo max is $max
    20	echo min is $min
[root@localhost shscripts]# chmod +x numcompare2.sh 
[root@localhost shscripts]# sh numcompare2.sh 
num member are 10103 13631 750 11306 8778 1782 2184 25443 11947 14544
max is 25443
min is 750

4.陣列排序

輸入若干個數值存入陣列中,採用冒泡演算法進行升序或降序排序

#編寫指令碼
[root@CentOS8 ~]# cat sort_arrays.sh 
#!/bin/bash
#
#**********************************
# Author:      wanhonron
# Date:        2021-06-29
# FileName:    sort_arrays.sh
# Url:         http://www.weda.com
# Copyright(C):2021All rights reserved
#**********************************
declare -a anums
read -p "please input nums:" number
for (( i=0;i<$number;i++ ));do
	anums[i]=$RANDOM
done
echo "source member of arrays: ${anums[@]}"
declare -i num=$number
for (( i=0;i<num-1;i++ ));do
	for(( j=0;j<num-1;j++ ));do
		let next=$j+1
	  if (( ${anums[$j]}>${anums[$next]} ));then
		tmp=${anums[$next]}
		anums[$next]=${anums[$j]}
		anums[$j]=$tmp
	  fi
	done
done
echo "sorted member of arrays:${anums[*]}"
echo -e "the minest int is ${anums[0]}\nthe maxest int is ${anums[$((num-1))]}"
#給指令碼執行許可權
[root@CentOS8 ~]# chmod +x sort_arrays.sh 
#執行指令碼
[root@CentOS8 ~]# sh sort_arrays.sh 
please input nums:5
source member of arrays: 25090 1978 24497 4386 21738
sorted member of arrays:1978 4386 21738 24497 25090
the minest int is 1978
the maxest int is 25090

氣泡排序原理圖:

資料對比交換演示圖(列舉兩輪,後面依次類推):

5.統計記憶體

顯示統計佔用系統記憶體最多的程序,並排序。

方法一:統計記憶體佔用程序

[root@CentOS8 ~]# ps aux --sort -rss | head -4
USER  PID %CPU %MEM   VSZ  RSS TTY  STAT START   TIME COMMAND
root  810  0.0  2.1 224860 40228 ?  S  08:37  0:00 /usr/libexec/sssd/sssd_nss --uid 0 --gid 0 --logger=files
root  809  0.0  2.1 298272 40120 ?  Ssl 08:37 0:00 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid
root  833  0.0  1.7 426292 31228 ?  Ssl 08:37 0:04 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P

方法二:統計記憶體佔用程序

[root@CentOS8 ~]# ps aux | sort -k4nr | head -4
root  809  0.0  2.1 298272 40120 ?  Ssl  08:37  0:00 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid
root  810  0.0  2.1 224860 40228 ?  S  08:37  0:00 /usr/libexec/sssd/sssd_nss --uid 0 --gid 0 --logger=files
root  833  0.0  1.7 426292 31228 ?  Ssl 08:37 0:04 /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P
polkitd  779  0.0  1.4 1764276 26884 ? Ssl 08:37 0:00 /usr/lib/polkit-1/polkitd --no-debug

6.網路測試

編寫指令碼,使用for和while分別實現10.0.0.0/24網段內,地址是否能夠ping通,若ping通則輸出"success!",若ping不通則輸出"fail!"

方法一:for迴圈實現網路ping

#編寫指令碼
[root@CentOS8 ~]# cat netfeild_ping_for.sh 
#!/bin/bash
#
#**********************************
# Author:      wanhonron
# Date:        2021-06-29
# FileName:    netfield_ping.sh
# Url:         http://www.weda.com
# Copyright(C):2021All rights reserved
#**********************************
netfeild=10.0.0.
for hostip in $(seq 254);do
{	if /bin/ping -c1 -W1 ${netfeild}${hostip} > /dev/null;then
		echo  "${netfeild}${hostip} is success."
	else
		echo  "${netfeild}${hostip} is fail."
	fi
} &										#併發執行
done
wait
#給檔案執行許可權
[root@CentOS8 ~]# chmod +x netfeild_ping_for.sh
#執行測試
[root@CentOS8 ~]# sh netfeild_ping_for.sh 
10.0.0.2 is success.
10.0.0.1 is success.
10.0.0.82 is success.
10.0.0.81 is success.
10.0.0.3 is fail.
10.0.0.7 is fail.
10.0.0.6 is fail.
...

方法二:while實現ping測試

[root@CentOS8 ~]# cat netfeild_ping_while.sh 
#!/bin/bash
#
#**********************************
# Author:      wanhonron
# Date:        2021-06-29
# FileName:    netfield_ping.sh
# Url:         http://www.weda.com
# Copyright(C):2021All rights reserved
#**********************************
declare -i i=1
netfeild="10.0.0."
while [ $i -le 254 ];do
	ping -c1 -W1 ${netfeild}$i &> /dev/null
	if [ $? -eq 0 ];then
		echo "${netfeild}$i is success."
	else
		echo "${netfeild}$i is fail."
	fi
	let i++ 
done 
#給檔案執行許可權
[root@CentOS8 ~]# chmod +x netfeild_ping_while.sh
#執行測試
[root@CentOS8 ~]# sh netfeild_ping_while.sh 
10.0.0.1 is success.
10.0.0.2 is success.
10.0.0.3 is fail.
10.0.0.4 is fail.
...