第十周作业

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

#修改主机名:
#设置主机名
root@ubuntu1804:~# hostnamectl set-hostname test
#查看主机名配置文件
root@ubuntu1804:~# cat /etc/hostname 
test
#重新加载生效
root@ubuntu1804:~# logout
yang@ubuntu1804:~$ su -
Password: 
root@test:~#

#修改网卡名称:
root@test:~# sed -i.bak '/^GRUB_CMDLINE_LINUX=/s#"$#net.ifnames=0"#' /etc/default/grub
#生成新grub文件
root@test:~# grub-mkconfig -o /boot/grub/grub.cfg

#网卡配置:
#修改网卡配置文件
root@test:~# vim /etc/netplan/01-netcfg.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses: [10.0.0.12/24]
      gateway4: 10.0.0.251
      optional: true
      nameservers:
          addresses: [223.6.6.6,223.5.5.5]
#设置完后应用生效
root@test:~# netplan apply

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

#expect方式
yum -y install expect

vim remote_ssh.sh
#!/usr/bin/expect
set ip  [lindex $argv 0]
set password  [lindex $argv 1]
set user  [lindex $argv 2]
spawn ssh $user@$ip
expect {
            "yes/no" { send "yes\n";exp_continue }
            "password" { send "$password\n" }
}
interact

[root@centos8 ~]#chmod +x ssh-remote.sh
[root@centos8 ~]# ./ssh-remote.sh 10.0.0.18 root 123
spawn ssh root@10.0.0.18
The authenticity of host '10.0.0.18 (10.0.0.18)' can't be established.
ECDSA key fingerprint is SHA256:02uItPDAzOBaE2gpjGYc58RxdG9PSTv0nOKdGxyc5n8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.18' (ECDSA) to the list of known hosts.
root@10.0.0.18's password: 
Last login: Sun Jan 31 23:49:09 2021 from 10.0.0.8
[root@centos8 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:91:ba:50 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.18/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::64ed:295f:cba6:8479/64 scope link dadfailed tentative noprefixroute 
       valid_lft forever preferred_lft forever
[root@centos8 ~]# exit
logout
Connection to 10.0.0.18 closed.

#shell方式
[root@centos8 ~]#yum -y install sshpass
[root@centos8 ~]# vim ssh-remote.sh
#!/bin/bash
IP=$1
USER=$2
PASSWORD=$3

sshpass -p $PASSWORD ssh -o StrictHostKeyChecking=no $user@$ip

[root@centos8 ~]#chmod +x ssh-remote.sh
[root@centos8 ~]#./ssh-remote.sh 10.0.0.18 root 123

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

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[$i]} && max=${nums[$i]}&& continue 
    [ ${nums[$i]} -gt $max ] && max=${nums[$i]} 
    [ ${nums[$i]} -lt $min ] && min=${nums[$i]} 
done 
echo “Numbers are ${nums[*]}” 
echo Max is $max 
echo Min is $min

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

vim argssum.sh
read -p "请输入数值个数:" COUNT
declare -a nums
for ((i=0;i<$COUNT;i++));do
    num[$i]=$RANDOM
done
echo "${num[@]}"

declare -i n=$COUNT
for (( i=0; i<n-1; i++ ));do
    for (( j=0; j<n-1-i; j++ ));do
        let x=$j+1
        if (( ${num[$j]} < ${num[$x]} ));then
 #从大到小排列
           tmp=${num[$x]}
            num[$x]=${num[$j]}
            num[$j]=$tmp
        fi
    done
done
echo ${num[*]}
echo "the max integer is $num,the min integer is ${num[$((n-1))]}"
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、Ubuntu系统网络配置总结(包括主机名、网卡名称、网卡配置) 2、编写脚本实现登陆远程主机。(使用expec...
    Easy_8195阅读 303评论 1 0
  • 1、Ubuntu系统网络配置总结(包括主机名、网卡名称、网卡配置) 2、编写脚本实现登陆远程主机。(使用expec...
    陌路残蝉阅读 252评论 1 0
  • 1、Ubuntu系统网络配置总结(包括主机名、网卡名称、网卡配置) 主机名:root@template-ubunt...
    antikor阅读 216评论 1 0
  • 1、Ubuntu系统网络配置 1.1网卡名称的改变 1.2主机名称的更改 1.3IP地址的配置 2、编写脚本实现登...
    196_fendou阅读 238评论 2 0
  • 1、编写脚本,接受二个位置参数,magedu和/www,判断系统是否有magedu,如果没有则自动创建magedu...
    大唐百夫长阅读 65评论 0 0