首页 > 编程语言 > 详细

Ubuntu网络基础配置和算法练习

时间:2021-05-26 00:56:03      阅读:39      评论:0      收藏:0      [点我收藏+]

一、Ubuntu网络和基础信息配置

1、网络的基础设定

Ubuntu与Centos不同,Ubuntu使用的是netplan来作为网卡的基础配置参数,并且使用的是yaml格式。在系统安装完成后,会自动生成如下的配置参数 /etc/netplan/00-installer-config.yaml
技术分享图片

配置网卡ip地址和dns

# This is the network config written by ‘subiquity‘
network:
  ethernets:
    ens33:
      dhcp4: false
      addresses: [192.168.96.143/24]
      gateway4: 192.168.96.2
      nameservers:
        addresses:
          - 222.172.200.68
          - 114.114.114.114
  version: 2

确认dns地址配置正确
技术分享图片
确认网关地址配置正确
技术分享图片
网络连通性测试
技术分享图片

2、主机名配置

技术分享图片
重新登录shell,可以看到主机名已经修改。
技术分享图片
技术分享图片

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

1、使用expect方式

系统默认并没有安装expect工具,需要通过yum源安装
技术分享图片
编写脚本自动登录

#!/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

技术分享图片

2、使用bash shell脚本,这里仅用于演示登录,执行的命令是查看根的文件

#!/bin/bash

#================================================================
#   Copyright (C) 2021 All rights reserved.
#   
#   FileName  : expect.sh
#   Author:  : HuaGuobin
#   CreateDate: 2021-05-25
#   Decription: 
#
#================================================================

ip=$1
user=$2
password=$3
if [ !  $#  -eq 3 ];then
  echo "Argv is error"
  echo "Usage: $0 [IP] [USERNAME] [PASSWORD]"
  exit
fi
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
   "yes/no" { send "yes\n";exp_continue }
   "password" { send "$password\n" }
}
expect "]#" { send "ls / \n" }
expect "]#" { send "exit\n"}
expect eof
EOF

技术分享图片

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

#!/bin/bash

#================================================================
#   Copyright (C) 2021 All rights reserved.
#   
#   FileName  : rand.sh
#   Author:  : HuaGuobin
#   CreateDate: 2021-05-19
#   Decription: 
#
#================================================================

for i in {1..10};do
sum[$i]=$RANDOM
done
MAX=${sum[1]}
MIN=$MAX
for((j=1;j<10;j++));do
   tem=${sum[j]}
   [ $tem -gt $MAX ] && MAX=$tem
   [ $tem -lt $MIN ] && MIN=$tem
echo ${sum[$j]}
done
echo MAX=$MAX
echo MIN=$MIN

技术分享图片

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

#!/bin/bash

#================================================================
#   Copyright (C) 2021 All rights reserved.
#   
#   FileName  : read.sh
#   Author:  : HuaGuobin
#   CreateDate: 2021-05-25
#   Decription: 
#
#================================================================

i=1
LastNumber=a
while [  $LastNumber != ‘q‘ ]
do
    read -p "Please input The $i number:(press ‘q‘ exit)  " NUM[$i]
    LastNumber=${NUM[$i]}
    if [ ! -n "$LastNumber" ];then LastNumber="a"; fi
    [[ "${NUM[$i]}" =~ ^[1-9]+$ ]] &&  let i++ || ( [ $LastNumber != "q" ] && echo "You Input A Wrong Type,Please input again!  ")

done
let j=i-1
for ((k=1;k<=$j;k++));do
   for ((m=k;m<=$j;m++ ));do
      if [ ${NUM[$k]} -lt ${NUM[$m]} ];then
        SWP=${NUM[$k]}
        NUM[$k]=${NUM[$m]}
        NUM[$m]=$SWP
     fi
   done
done

echo "您输入的$j个数字,按照降序排列如下:"
for n in $(seq $j);do
  echo ${NUM[$n]}
done

技术分享图片

Ubuntu网络基础配置和算法练习

原文:https://blog.51cto.com/u_15131458/2813007

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!