第十周练习

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 

编写脚本实现登陆远程主机。

expect脚本:

cat ssh_expect.exp
#!/usr/bin/expect
spawn ssh 192.168.1.2
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send "passwd\n"}
}
interact        #允许用户交互模式,如果不增加的话你远程过去就直接退出了
chmod +x ssh_expect.exp
./ssh_expect.exp

shell调用expect

cat ssh_shell.sh
#!/bin/bash
ip=10.228.81.119
user=root
password=88888888
expect <<EOF
set timeout 10000
spawn ssh $user@$ip
expect {
       "yes/no" { send "yes\n";exp_continue }
       "password" { send "$password\n" }
}
expect "]#" { send "hostname\n" }
expect "]#" { send "exit\n" }
expect eof
EOF
bash ssh_shell.sh

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

#!/bin/bash
#此脚本用于生成10个随机数保存于数组中,并找出其最大值和最小值
declare -i min
declare -i mam
declare -a nums
for ((i=0;i<10;i++));do
 nums[$i]=$RANDOM
 if [ $i -eq 0 ];then
    min=${nums[0]}
 fi
 [[ ${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

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

#!/bin/bash
#此脚本用于生成10个随机数保存于数组中,并找出其最大值和最小值
declare -a nums                 #声明一个数组
read -p "请输入生成随机数个数:" number
# 生成一组随机数存入nums数组中
for (( i=0;i<$number;i++ ));do
        nums[$i]=$RANDOM
done
#打印生成的一组随机数
echo "before sort:${nums[*]}"
# 将数组重新赋值
declare -i n=$number
#使用冒泡法对上面的一组数做一个排序
#假设i=0,那么就需要循坏n-1次 排序
for (( i=0;i<n-1;i++ ));do
    for ((j=0;j<n-1;j++));do
        let next=$j+1
        if (( ${nums[$j]} > ${nums[$next]} ));then
          tmp=${nums[$next]}
          nums[$next]=${nums[$j]}
          nums[$j]=$tmp
        fi
    done
done
echo "after sort:${nums[*]}"
echo "the Min integer is ${nums[0]},the max integer is ${nums[$(( n-1 ))]}"
posted @ 2021-05-30 12:46  自在拉基  阅读(49)  评论(0编辑  收藏  举报