網絡班第十周
1.Ubuntu系統網絡配置總結
1.1主機名
修改主機名(永久生效)
# 查看當前主機名 [Sat Jan 30 root@ubuntu 20:09~]#hostname ubuntu [Sat Jan 30 root@ubuntu 20:11~]#cat /etc/hostname ubuntu [Sat Jan 30 root@ubuntu 20:11~]#hostnamectl Static hostname: ubuntu Icon name: computer-vm Chassis: vm Machine ID: cc43625c00a84c8081f1477b19fac412 Boot ID: a8d2132083a24808804ae596668a6c3e Virtualization: vmware Operating System: Ubuntu 18.04.4 LTS Kernel: Linux 4.15.0-130-generic Architecture: x86-64 [Sat Jan 30 root@ubuntu 20:11~]#uname -n ubuntu [Sat Jan 30 root@ubuntu 20:11~]#uname -a Linux ubuntu 4.15.0-130-generic #134-Ubuntu SMP Tue Jan 5 20:46:26 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux [Sat Jan 30 root@ubuntu 20:11~]# # 永久修改主機名 [Sat Jan 30 root@ubuntu 20:17~]#hostnamectl set-hostname ubuntu1804 [Sat Jan 30 root@ubuntu 20:22~]#hostname ubuntu1804 [Sat Jan 30 root@ubuntu 20:22~]#cat /etc/hostname ubuntu1804 [Sat Jan 30 root@ubuntu4 20:23~]# # 驗證 [Sat Jan 30 root@ubuntu 20:23~]#bash [Sat Jan 30 root@ubuntu1804 20:23~]#
1.2 網卡名稱
默認ubuntu的網卡名稱和 CentOS 7 類似,如:ens33,ens38等
修改網卡名稱為傳統命名方式:
# 修改配置文件為下面形式 [root@ubuntu1804~]#vi /etc/default/grub GRUB_CMDLINE_LINUX="net.ifnames=0" # 或者sed修改 [root@ubuntu1804~]# sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#' /etc/default/grub # 生效新的grub.cfg文件 [root@ubuntu1804~]# grub-mkconfig -o /boot/grub/grub.cfg # 或者 [root@ubuntu1804~] update-grub [root@ubuntu1804~]# grep net.ifnames /boot/grub/grub.cfg linux /vmlinuz-4.15.0-96-generic root=UUID=51517b88-7e2b-4d4a-8c14- fe1a48ba153c ro net.ifnames=0 linux /vmlinuz-4.15.0-96-generic root=UUID=51517b88-7e2b-4d4a- 8c14-fe1a48ba153c ro net.ifnames=0 linux /vmlinuz-4.15.0-96-generic root=UUID=51517b88-7e2b-4d4a- 8c14-fe1a48ba153c ro recovery nomodeset net.ifnames=0 linux /vmlinuz-4.15.0-76-generic root=UUID=51517b88-7e2b-4d4a- 8c14-fe1a48ba153c ro net.ifnames=0 linux /vmlinuz-4.15.0-76-generic root=UUID=51517b88-7e2b-4d4a- 8c14-fe1a48ba153c ro recovery nomodeset net.ifnames=0 # 重啟生效 [root@ubuntu1804~]# reboot
1.3 Ubuntu網卡配置
1.3.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: eth0: dhcp4: yes
修改網卡配置文件后需執行命令生效:
[root@ubuntu1804~]#netplan apply
1.3.2 配置靜態IP
范例:
[root@ubuntu1804~]#vim /etc/netplan/01-netcfg.yaml network: version: 2 renderer: networkd ethernets: eth0: addresses: [192.168.8.10/24,10.0.0.10/8] #或者用下面兩行,兩種格式不能混用 - 192.168.8.10/24 - 10.0.0.10/8 gateway4: 192.168.8.1 nameservers: search: [magedu.com, magedu.org] addresses: [180.76.76.76, 8.8.8.8, 1.1.1.1]
2.編寫腳本實現登陸遠程主機
2.1 交互式轉化批處理工具 expect
expect 是由Don Libes基于 Tcl( Tool Command Language )語言開發的,主要應用于自動化交互式操作的場景,借助 expect 處理交互的命令,可以將交互過程如:ssh登錄,ftp登錄等寫在一個腳本上,使之自動化完成。尤其適用于需要對多臺服務器執行相同操作的環境中,可以大大提高系統管理人員的工作效率
[root@centos7 ~]# yum provides expect Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com file:///mnt/cdrom/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/cdrom/repodata/repomd.xml" Trying other mirror. expect-5.45-14.el7_1.x86_64 : A program-script interaction and testing utility Repo : base expect-5.45-14.el7_1.x86_64 : A program-script interaction and testing utility Repo : cdrom expect-5.45-14.el7_1.x86_64 : A program-script interaction and testing utility Repo : @base [root@centos7 ~]# yum -y install expect-5.45-14.el7_1.x86_64 Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com file:///mnt/cdrom/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/cdrom/repodata/repomd.xml" Trying other mirror. Package expect-5.45-14.el7_1.x86_64 already installed and latest version Nothing to do [root@centos7 ~]#
expect 語法:
expect [選項] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]
常見選項:
-c:從命令行執行expect腳本,默認expect是交互地執行的
-d:可以輸出輸出調試信息
expect中相關命令
spawn 啟動新的進程
expect 從進程接收字符串
send 用于向進程發送字符串
interact 允許用戶交互
exp_continue 匹配多個字符串在執行動作后加此命令
expect最常用的語法(tcl語言:模式-動作)
單一分支模式語法:
[root@centos7 ~]# expect expect1.1> expect "hi" {send "You said hi\n"} hahahuhixixi You said hi expect1.2> exit [root@centos7 ~]#
匹配到hi后,會輸出“you said hi”,并換行
多分支模式語法:
[root@centos7 ]#expect expect1.1> expect "hi" { send "You said hi\n" } "hehe" { send "Hehe yourself\n" } "bye" { send "Good bye\n" } hehe Hehe yourself expect1.2> expect "hi" { send "You said hi\n" } "hehe" { send "Hehe yourself\n" } "bye" { send "Good bye\n" } bye Good bye expect1.3> expect "hi" { send "You said hi\n" } "hehe" { send "Hehe yourself\n" } "bye" { send "Good bye\n" } hi You said hi expect1.4>
匹配hi,hello,bye任意字符串時,執行相應輸出。等同如下:
expect { "hi" { send "You said hi\n"} "hehe" { send "Hehe yourself\n"} "bye" { send " Good bye\n"} } [root@centos8 ~]#expect expect1.1> expect { +> "hi" { send "You said hi\n"} +> "hehe" { send "Hehe yourself\n"} +> "bye" { send " Good bye\n"} +> } bye Good bye expect1.2>
范例1
#!/usr/bin/expect spawn scp /etc/fstab 10.0.0.7:/data expect { "yes/no" { send "yes\n";exp_continue } "password" { send "magedu\n" } } expect eof
范例2
#!/usr/bin/expect spawn ssh 10.0.0.7 expect { "yes/no" { send "yes\n";exp_continue } "password" { send "magedu\n" } } interact
范例3:expect 變量
#!/usr/bin/expect set ip 10.0.0.7 set user root set password magedu set timeout 10 spawn ssh $user@$ip expect { "yes/no" { send "yes\n";exp_continue } "password" { send "$password\n" } } interact
范例4:expect 位置參數
[root@centos8 scripts]#cat expect4 #!/usr/bin/expect set ip [lindex $argv 0] set user [lindex $argv 1] set password [lindex $argv 2] spawn ssh $user@$ip expect { "yes/no" { send "yes\n";exp_continue } "password" { send "$password\n" } } interact [root@centos8 scripts]#./expect4 10.0.0.7 root magedu spawn ssh root@10.0.0.7 root@10.0.0.7's password: Last login: Wed Apr 29 15:34:14 2020 from 10.0.0.8 [root@centos7 ~]#
范例5:expect 執行多個命令
#!/usr/bin/expect set ip [lindex $argv 0] set user [lindex $argv 1] set password [lindex $argv 2] set timeout 10 spawn ssh $user@$ip expect { "yes/no" { send "yes\n";exp_continue } "password" { send "$password\n" } } expect "]#" { send "useradd haha\n" } expect "]#" { send "echo magedu |passwd --stdin haha\n" } send "exit\n" expect eof #./ssh4.exp 10.0.0.7 root magedu
范例6:shell腳本調用expect
#!/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 "]#" { send "useradd hehe\n" } expect "]#" { send "echo magedu |passwd --stdin hehe\n" } expect "]#" { send "exit\n" } expect eof EOF #./ssh5.sh 192.168.8.10 root magedu
范例7: shell腳本利用循環調用expect在CentOS和Ubuntu上批量創建用戶
#!/bin/bash NET=10.0.0 user=root password=magedu for ID in 6 7 111;do ip=$NET.$ID expect <<EOF set timeout 20 spawn ssh $user@$ip expect { "yes/no" { send "yes\n";exp_continue } "password" { send "$password\n" } } expect "#" { send "useradd test\n" } expect "#" { send "exit\n" } expect eof EOF done
練習1:expect腳本實現登陸遠程主機
[root@centos7 ~]# cd /data/scripts [root@centos7 scripts]# ls [root@centos7 scripts]# vim expect1 [root@centos7 scripts]# cat expect1 [root@centos7 scripts]# cat expect1 #!/usr/bin/expect set ip [lindex $argv 0] set user [lindex $argv 1] set password [lindex $argv 2] spawn ssh $user@$ip expect { "yes/no" { send "yes\n";exp_continue } "password" { send "$password\n" } } interact [root@centos7 scripts]# chmod +x expect1 [root@centos7 scripts]# ./expect1 10.0.0.88 root 666666 spawn ssh root@10.0.0.88 root@10.0.0.88's password: Web console: https://centos8:9090/ or https://10.0.0.88:9090/ Last login: Sun Jan 31 05:29:54 2021 from 10.0.0.10 [Sun Jan 31 root@centos8 05:30~]#exit logout Connection to 10.0.0.88 closed. [root@centos7 scripts]#
練習2:shell腳本實現登陸遠程主機
[root@centos7 scripts]# ls expect1 [root@centos7 scripts]# ll total 4 -rwxr-xr-x 1 root root 231 Jan 31 05:23 expect1 [root@centos7 scripts]# vim ssh.sh [root@centos7 scripts]# chmod +x ssh.sh [root@centos7 scripts]# ll total 8 -rwxr-xr-x 1 root root 231 Jan 31 05:23 expect1 -rwxr-xr-x 1 root root 726 Jan 31 05:39 ssh.sh [root@centos7 scripts]# cat ssh.sh [root@centos7 scripts]# vim ssh.sh [root@centos7 scripts]# cat ssh.sh #!/bin/bash ip=$1 user=$2 password=$3 expect <<EOF spawn ssh $user@$ip expect { "yes/no" { send "yes\n";exp_continue } "password" { send "$password\n" } } expect "]#" { send "exit\n" } expect eof EOF [root@centos7 scripts]# ./ssh.sh 10.0.0.88 root 666666 spawn ssh root@10.0.0.88 root@10.0.0.88's password: Web console: https://centos8:9090/ or https://10.0.0.88:9090/ Last login: Sun Jan 31 06:07:59 2021 from 10.0.0.10 [Sun Jan 31 root@centos8 06:25~]#exit logout Connection to 10.0.0.88 closed. [root@centos7 scripts]#
備注:在第二個EOF的時候,后面不要有空格,否則報錯:
invalid command name "EOF"
while executing
"EOF "
把后面的空格去掉即可
3.數組array
3.1 數組介紹
變量:存儲單個元素的內存空間
數組:存儲多個元素的連續的內存空間,相當于多個變量的集合
數組名和索引
索引的編號從0開始,屬于數值索引
索引可支持使用自定義的格式,而不僅是數值格式,即為關聯索引,bash4.0版本之后開始支持
bash的數組支持稀疏格式(索引不連續)
3.2 聲明數組
#普通數組可以不事先聲明,直接使用 declare -a ARRAY_NAME #關聯數組必須先聲明,再使用 declare -A ARRAY_NAME
注意:兩者不可相互轉換
3.3 數組賦值
數組元素的賦值
(1) 一次只賦值一個元素
ARRAY_NAME[INDEX]=VALUE
weekdays[0]="Sunday" weekdays[4]="Thursday"
(2) 一次賦值全部元素
ARRAY_NAME=("VAL1" "VAL2" "VAL3" ...)
title=("ceo" "coo" "cto") num=({0..10}) alpha=({a..g}) file=( *.sh )
(3) 只賦值特定元素
ARRAY_NAME=([0]="VAL1" [3]="VAL2" ...)
(4) 交互式數組值對賦值
read -a ARRAY
3.4 顯示所有數組
顯示所有數組:
declare -a
3.5 引用數組
引用數組元素
${ARRAY_NAME[INDEX]}
#如果省略[INDEX]表示引用下標為0的元素
[root@centos8 ~]#declare -a title=([0]="ceo" [1]="coo" [2]="cto") [root@centos8 ~]#echo ${title[1]} coo [root@centos8 ~]#echo ${title} ceo [root@centos8 ~]#echo ${title[2]} cto [root@centos8 ~]#
引用數組所有元素
${ARRAY_NAME[*]}
${ARRAY_NAME[@]}
[root@centos8 ~]#echo ${title[@]} ceo coo cto [root@centos8 ~]#echo ${title[*]} ceo coo cto
數組的長度,即數組中元素的個數
${#ARRAY_NAME[*]}
${#ARRAY_NAME[@]}
[root@centos8 ~]#echo ${#title[*]} 3
3.6 刪除數組
刪除數組中的某元素,會導致稀疏格式
unset ARRAY[INDEX]
[root@centos8 ~]#echo ${title[*]} ceo coo cto [root@centos8 ~]#unset title[1] [root@centos8 ~]#echo ${title[*]} ceo cto
刪除整個數組
unset ARRAY
3.7 數組數據處理
數組切片:
${ARRAY[@]:offset:number}
offset #要跳過的元素個數
number #要取出的元素個數
#取偏移量之后的所有元素
{ARRAY[@]:offset}
[root@centos8 ~]#num=({0..10})
[root@centos8 ~]#echo ${num[*]:2:3}
2 3 4
[root@centos8 ~]#echo ${num[*]:6}
6 7 8 9 10
向數組中追加元素:
ARRAY[${#ARRAY[*]}]=value
ARRAY[${#ARRAY[@]}]=value
[root@centos8 ~]#num[${#num[@]}]=11
[root@centos8 ~]#echo ${#num[@]}
12
[root@centos8 ~]#echo ${num[@]}
0 1 2 3 4 5 6 7 8 9 10 11
3.8 關聯數組
declare -A ARRAY_NAME ARRAY_NAME=([idx_name1]='val1' [idx_name2]='val2‘...)
注意:關聯數組必須先聲明再調用
[root@centos8 ~]#name[ceo]=mage [root@centos8 ~]#name[cto]=wang [root@centos8 ~]#name[coo]=zhang [root@centos8 ~]#echo ${name[ceo]} zhang [root@centos8 ~]#echo ${name[cto]} zhang [root@centos8 ~]#echo ${name[coo]} zhang [root@centos8 ~]#echo ${name} zhang [root@centos8 ~]#declare -A name -bash: declare: name: cannot convert indexed to associative array [root@centos8 ~]#unset name [root@centos8 ~]#declare -A name [root@centos8 ~]#name[ceo]=mage [root@centos8 ~]#name[cto]=wang [root@centos8 ~]#name[coo]=zhang [root@centos8 ~]#echo ${name[coo]} zhang [root@centos8 ~]#echo ${name[ceo]} mage [root@centos8 ~]#echo ${name[cto]} wang [root@centos8 ~]#echo ${name[*]} mage wang zhang
[root@centos8 ~]#declare -A student [root@centos8 ~]#student[name1]=lijun [root@centos8 ~]#student[name2]=ziqing [root@centos8 ~]#student[age1]=18 [root@centos8 ~]#student[age2]=16 [root@centos8 ~]#student[gender1]=m [root@centos8 ~]#student[city1]=nanjing [root@centos8 ~]#student[gender2]=f [root@centos8 ~]#student[city2]=anhui [root@centos8 ~]#student[gender2]=m [root@centos8 ~]#student[name50]=alice [root@centos8 ~]#student[name3]=tom [root@centos8 ~]#for i in {1..50};do echo student[name$i]=${student[name$i]}; done
練習3:
生成10個隨機數保存于數組中,并找出其最大值和最小值
[root@centos7 ~]# cd /data/scripts/ [root@centos7 scripts]# ls expect1 ssh.sh [root@centos7 scripts]# vim min_max.sh [root@centos7 scripts]# cat min_max.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 scripts]# chmod +x min_max.sh [root@centos7 scripts]# ./min_max.sh All numbers are 5555 22926 3870 323 28707 7512 2245 23751 19875 3346 Max is 28707 Min is 323 [root@centos7 scripts]# ./min_max.sh All numbers are 20910 6826 5802 2442 225 22828 25655 27198 11012 8001 Max is 27198 Min is 225 [root@centos7 scripts]#
練習4:
輸入若干個數值存入數組中,采用冒泡算法進行升序或降序排序
# 降序排列 [root@centos7 scripts]# vim bubble_array.sh [root@centos7 scripts]# cat bubble_array.sh #!/bin/bash declare -a number read -p "Please enter the number of generated random numbers:" num for (( i=0; i<$num; i++ ));do number[$i]=$RANDOM done echo "before sort:" echo ${number[@]} declare -i n=$num for (( i=0; i<n-1; i++ ));do for (( j=0; j<n-1-i; j++ ));do let next=$j+1 if (( ${number[$j]} < ${number[$next]} ));then tmp=${number[$next]} number[$next]=${number[$j]} number[$j]=$tmp fi done done echo "after sort:" echo ${number[*]} echo "the max integer is ${number[0]},the min integer is ${number[$(( n-1 ))]}" [root@centos7 scripts]# chmod +x bubble_array.sh [root@centos7 scripts]# ./bubble_array.sh Please enter the number of generated random numbers:5 before sort: 6205 32176 17353 17808 428 after sort: 32176 17808 17353 6205 428 the max integer is 32176,the min integer is 428 [root@centos7 scripts]# ./bubble_array.sh Please enter the number of generated random numbers:7 before sort: 32755 24283 6732 1471 30819 25775 9916 after sort: 32755 30819 25775 24283 9916 6732 1471 the max integer is 32755,the min integer is 1471 [root@centos7 scripts]# # 升序排列 [root@centos7 scripts]# vim bubble_array2.sh [root@centos7 scripts]# cat bubble_array2.sh #!/bin/bash read -p "Please enter the number of generated random numbers:" num declare -a number for (( i=0; i<$num; i++ ));do number[$i]=$RANDOM done echo "before sort:" echo ${number[@]} declare -i n=$num for (( i=0; i<n-1; i++ ));do for (( j=0; j<n-1-i; j++ ));do let next=$j+1 if (( ${number[$j]} > ${number[$next]} ));then tmp=${number[$next]} number[$next]=${number[$j]} number[$j]=$tmp fi done done echo "after sort:" echo ${number[*]} echo "the max integer is ${number[$(( n-1 ))]},the min integer is ${number[0]}" [root@centos7 scripts]# chmod +x bubble_array2.sh [root@centos7 scripts]# ./bubble_array2.sh Please enter the number of generated random numbers:5 before sort: 10717 32617 27697 11975 8984 after sort: 8984 10717 11975 27697 32617 the max integer is 32617,the min integer is 8984 [root@centos7 scripts]# ./bubble_array2.sh Please enter the number of generated random numbers:9 before sort: 8736 6940 25929 23407 3386 31846 12175 32605 29410 after sort: 3386 6940 8736 12175 23407 25929 29410 31846 32605 the max integer is 32605,the min integer is 3386 [root@centos7 scripts]#
浙公網安備 33010602011771號