Ubuntu網絡配置及shell腳本

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 [email protected]
[email protected]'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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章