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<UP,BROADCAST,RUNNING,MULTICAST>  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<link>
        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<UP,BROADCAST,RUNNING,MULTICAST>  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<link>
        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 root@192.168.238.30
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.
root@192.168.238.30'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<UP,BROADCAST,RUNNING,MULTICAST>  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 <<EOF
set timeout 20       //设置连接超时的时间
spawn ssh $ROOT@$IP
expect {
    "yes/no" { send "yes\n";exp_contuine }
    "password" { send "$PASSWORD\n" }

}
expect eof
EOF
[00:54:56  root@client scripts]#./shell_expect.sh 192.168.238.30 root zhaofan
spawn ssh root@192.168.238.30
root@192.168.238.30's password: 
Last login: Tue Apr 13 20:54:15 2021 from 192.168.238.10

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<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 ))]}"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、Ubuntu系统网络配置总结(包括主机名、网卡名称、网卡配置) 主机名:root@template-ubunt...
    antikor阅读 215评论 1 0
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,239评论 2 33
  • 一、文件/文件夹管理 ls 列出当前目录文件(不包括隐含文件)ls -a 列出当前目录文件(包括隐含文件)ls -...
    BerL1n阅读 7,735评论 0 78
  • 系统管理与维护命令 date date(选项)(参数) | 选项 | 说明 | | :-------- | ...
    蓓蓓的万能男友阅读 4,021评论 0 5
  • #########################################################...
    busy_living阅读 406评论 0 0