~~十面埋伏~~

 

网络班第十周

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]#

 

 

posted on 2021-01-31 17:32  ~~十面埋伏~~  阅读(113)  评论(1)    收藏  举报

导航