2021-01-29 第十周

1、Ubuntu系统网络配置

1.1网卡名称的改变

root@ubunto:~# 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=""
GRUB_CMDLINE_LINUX="net.ifnames=0"            //更改文件此行

#重新生成grub文件(1或2条命令都可)
root@ubunto:~# grub-mkconfig -o /boot/grub/grub.cfg         
root@ubunto:~# update-grub

#重启生效
root@ubunto:~# reboot                                     
root@ubunto:~# ifconfig eth0
eth0: flags=4163  mtu 1500
        inet 192.168.10.40  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:fe5b:21ac  prefixlen 64  scopeid 0x20
        ether 00:0c:29:5b:21:ac  txqueuelen 1000  (Ethernet)
        RX packets 138  bytes 13477 (13.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 116  bytes 16396 (16.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0        

1.2主机名称的更改

#命令行修改
root@ubunto-aerver:~# hostnamectl set-hostname ubunto        
#配置文件修改
root@ubunto:~# cat /etc/hostname                       
ubunto

1.3IP地址的配置

#修改网卡配置(配置静态ip):

root@ubunto:~# cat /etc/netplan/00-installer-config.yaml 
network:
  ethernets:
    eth0:
      addresses:
      - 192.168.10.40/24
      gateway4: 192.168.10.1
      nameservers:
        addresses:
        - 114.114.114.114
  version: 2

#动态获取IP地址
root@ubuntu:~# cat /etc/netplan/00-installer-config.yaml
network: 
  version: 2 
  renderer: networkd 
  ethernets:  
    eth0:   
      dhcp4: yes

root@ubunto:~# netplan apply           //是网卡配置文件生效 

#查看IP地址:
root@ubunto:~# ifconfig eth0
eth0: flags=4163  mtu 1500
        inet 192.168.10.40  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:fe5b:21ac  prefixlen 64  scopeid 0x20
        ether 00:0c:29:5b:21:ac  txqueuelen 1000  (Ethernet)
        RX packets 138  bytes 13477 (13.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 116  bytes 16396 (16.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2、编写脚本实现登陆远程主机。(使用expect和shell脚本两种形式)

2.1基于expect实现远程主机的登陆

[00:16:53  root@client scripts]#cat expect.sh 
#!/usr/bin/expect
set IP [lindex $argv 0]
set ROOT [lindex $argv 1]
set PASSWORD [lindex $argv 2]
spawn ssh $ROOT@$IP
expect {
      "yes/no" { send "yes\n";exp_continue }
      "password" {send "zhaofan\n" }
 }
interact

[00:13:08  root@client scripts]#chmod +x expect.sh
[00:16:37  root@client scripts]#./expect.sh 192.168.238.30 root zhaofan
spawn ssh [email protected]
The authenticity of host '192.168.238.30 (192.168.238.30)' can't be established.
ECDSA key fingerprint is SHA256:HRMpG+eu5A0m/gLeeq6GrSzBR1i5Y5Y7SM97A5PUnLE.
ECDSA key fingerprint is MD5:af:28:43:c2:84:f0:19:e5:20:f8:42:08:99:08:98:57.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.238.30' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Last login: Tue Apr 13 20:11:26 2021 from 192.168.238.2

#测试主机是否登陆成功
[20:17:12  root@router ~]#ifconfig ens34 | head -2
ens34: flags=4163  mtu 1500
        inet 192.168.238.30  netmask 255.255.255.0  broadcast 192.168.238.255

2.2基于shell的远程主机登陆

[00:55:26  root@client scripts]#cat  shell_expect.sh 
#!/bin/bash
IP=$1
ROOT=$2
PASSWORD=$3
expect <

3、生成10个随机数保存于数组中,并找出其最大值和最小值

[19:17:30  root@centos8 scripts]#cat suijimax.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[$i]} && max=${nums[$i]}&& 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

4、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序

[19:24:12  root@centos8 scripts]#cat numsort.sh 
declare -a number
for (( i=0; i<20; i++ ));do
      number[$i]=$RANDOM
  done
  echo "before sort:"
  echo ${number[@]}

  declare -i n=20
  for (( i=0; i

你可能感兴趣的:(2021-01-29 第十周)