Ubuntu网络配置以及shell脚本

1、Ubuntu系统网络配置总结(包括主机名、网卡名称、网卡配置)

主机名修改

#1 命令修改
root@ubuntu:~# cat /etc/hostname 
ubuntu
root@ubuntu:~# hostnamectl set-hostname ubuntu1804
root@ubuntu:~# cat /etc/hostname 
ubuntu1804

#2 直接修改hostname文件
root@ubuntu:~# vim /etc/hostname 
ubuntu1804.test
root@ubuntu:~# cat /etc/hostname 
ubuntu1804.test

网卡名称的修改

#手工修改
root@ubuntu:~# vi /etc/default/grub 
#在文件中把下面一行做修改
GRUB_CMDLINE_LINUX=""
#修改后
GRUB_CMDLINE_LINUX="net.ifnames=0"

#sed命令修改
root@ubuntu:~# sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#' /etc/default/grub 

#修改后使配置生效
#方法1  生成新的grub.cfg 文件
root@ubuntu:~# grub-mkconfig -o /boot/grub/grub.cfg
#方法2 命令生效
root@ubuntu:~# update-grub
#方法3 重启生效
root@ubuntu:~# reboot

网卡的配置

#方法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:
    ens33:
      dhcp4: yes

#方法2 配置静态IP
root@ubuntu:~# 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:
    ens33:
      addresses: [10.0.0.10/24]
      gateway4: 10.0.0.2
      nameservers:
              addresses: [202.101.172.35,202.101.172.47]

##修改网卡配置文件后需执行命令生效
root@ubuntu:~# netplan apply 

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

#方法1
[root@centos7 ~]# vim expect_login.sh
#!/usr/bin/expect
set ip 10.0.0.8
set user root
set password 123456
set timeout 10
spawn ssh $user@$ip
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send "$password\n" }
}
interact

[root@centos7 ~]# chmod +x expect_login.sh 
[root@centos7 ~]# ./expect_login.sh 
spawn ssh root@10.0.0.8
The authenticity of host '10.0.0.8 (10.0.0.8)' can't be established.
ECDSA key fingerprint is SHA256:suSC6K0UIGayYlZBfnpZClLdT+8n0b/a+vgPRbUoeEA.
ECDSA key fingerprint is MD5:b3:90:95:86:fa:e8:fa:e5:ed:b0:7f:2c:17:1a:2b:f2.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.8' (ECDSA) to the list of known hosts.
root@10.0.0.8's password: 
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Fri Jan 29 03:34:22 2021 from 10.0.0.1
[root@centos8 ~]# 


#方法2 
[root@centos7 ~]# vim shell_login.sh
#!/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 eof
EOF

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

[root@centos7 ~]# vim max_min.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 ~]# chmod +x max_min.sh 
[root@centos7 ~]# ./max_min.sh 
All numbers are 6045 22477 28851 3375 14140 19251 6128 11183 1775 17015
Max is 28851
Min is 1775

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

[root@centos7 ~]# vim maopao.sh 
#!/bin/bash
i=0
flag=0
wrong=0
#保存数组值
while true;do
    read -p "请输入数字(输入quit退出):" num
    if [[ "$num" == ^"" ]];then
        echo "请输入数字 你已经输错了$wrong 次"
        let wrong++
    fi
    if [ $wrong -eq 5 ];then
        echo "你居然能输错 $wrong 次 我受不了你了"
        break
    elif [[ $num == quit ]];then
                                break
    elif [[ $num =~ ^[-]{0,1}[[:digit:]]{0,}[.]{0,1}[[:digit:]]{0,}[[:digit:]]$ ]];then #筛选出 整数 小数 负数
        echo $num
        array[$i]=$num
        let i++
    else
        echo "请输入数字 你已经输错了$wrong 次"
        let wrong++
    fi
done

#比较两个数大小
compare () {
    echo $1 > /app/0324/tmp
    echo $2 >> /app/0324/tmp
    max=$(cat /app/0324/tmp|sort -nr|head -1)
    if [[ "$max" == "$1" ]];then
        flag=1
    else
        flag=0
    fi
}

#冒泡排序
length=${#array[*]}
for i in $(seq 0 $length);do
    for j in $(seq 0 $[$length-$i]);do
        t=$[$j+1]
        compare ${array[$j]} ${array[$t]}
        if [ $flag -eq 1 ];then
             temp=${array[$j]}
             array[$j]=${array[$t]}
             array[$t]=$temp
        fi
done
done
echo "从小到大排序为:${array[*]}"

[root@centos7 ~]# chmod +x maopao.sh
[root@centos7 ~]# ./maopao.sh 
请输入数字(输入quit退出):4565
4565
请输入数字(输入quit退出):56
56
请输入数字(输入quit退出):12
12
请输入数字(输入quit退出):quit


从小到大排序为:4565 56 12  

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 网卡配置文件: 网卡最文件简配置: 网络配置一般包括如下内容: 网络配置方式: CentOS 6网卡名称 网络接口...
    JevonWei阅读 5,327评论 0 0
  • 一.Ip协议特征 (1)internet协议特征 运行于 OSI 网络层  面向无连接的协议  独立处理数据包...
    楠人帮阅读 4,334评论 0 0
  • 常用判断网络是否中断的方法: 1.Traceroute,Tracert traceroute命令用于追踪数据包在网...
    TimLi_51bb阅读 5,465评论 0 1
  • ifcfg命令家族: ifconfig, route, netstat ifconfig命令:接口及地址查看和管理...
    SRE1阅读 7,220评论 0 0
  • 一、知识点回顾 1、网络? 1、传输介质 2、 硬件 2、两台主机之间进行通信? 3、交换网络(局域网内多台计...
    唐朝_6f2a阅读 3,110评论 0 0