shell脚本编程算法分析

阿里 阅读:227 2019-12-25 21:20:56 评论:0

2019.08.28

 1. 编写一个shell脚本,判断192.168.1.0/24络内,在线的ip有哪些,能ping通就代表在线。

[[email protected] scripts]# vim 001-judge_ip.sh 
#!/bin/bash 
 
for ip in $(seq 254) 
do 
  ping -c 1 192.168.1.$ip &>/dev/null 
  if [ $? = 0 ];then 
    echo 192.168.1.$ip is online 
  fi 
done

2019.08.29

 1. 编写一个shell脚本,批量创建5个用户并且创建8位随机密码。

# 第一种方法 
[[email protected] scripts]# vim 002-creat_user.sh 
#!/bin/bash 
 
for u in user{01..05} 
do 
  useradd $u 
  PWD=`tr -dc '0-z' </dev/urandom | head -c 8` 
  echo -e "user:$u\npassword:$PWD" >>/tmp/user.txt 
  echo "$u:$PWD" | chpasswd 
done 
# 第二种方法 
[[email protected] scripts]# vim 002-creat_user.sh 
#!/bin/bash 
 
for u in oldboy{01..05} 
do 
  useradd $u 
  for p in `echo $RANDOM | md5sum | cut -c 1-8` 
  do 
    echo -e "user:$u\npassword:$p" >>/tmp/user.txt 
    echo "$u:$p" | chpasswd 
  done 
done 

2019.08.30

 1. 写一个批量分发公钥的脚本。

# CentOS 7.x版本 
[[email protected] scripts]# vim batch.sh 
#!/bin/bash 
 
PWD=接收公钥主机的密码 
 
rm -rf /root/.ssh/id_dsa* 
ssh-keygen -t dsa -f /root/.ssh/id_dsa -P '' -q 
 
for ip in $* 
do 
  sshpass -p $PWD ssh-copy-id -i /root/.ssh/id_dsa.pub -o StrictHostKeyChecking=no $ip &>/dev/null 
  if [ $? != 0 ];then 
    echo -e "\n----- $ip distribution of failure -----\n"  
    continue 
  fi 
done

2019.09.01

 1. 猜数字游戏,60以内的整数。

[[email protected] scripts]# vim guess_number.sh 
#!/bin/bash 
 
RAN=$((RANDOM%61)) 
COUNT=0 
 
while true 
do 
  read -p 'Please enter a number:' num 
  ((COUNT++)) 
  expr $num + 0 &>/dev/null 
  if [ $? -ne 0 -o $num -gt 60 ];then 
    echo 'Invalid parameter,Please enter an integer within 60' 
  else 
    if [ $num -eq $RAN ];then 
      echo "Bingo!You guessed right,$COUNT times" 
      exit 
    elif [ $num -gt $RAN ];then 
      echo 'too big' 
    else 
      echo 'too small' 
    fi 
  fi 
done

2019.11.04

 1. 找出目录中大于200M的日志文件并压缩,压缩文件仍保留在原目录中,压缩完成后删除原日志文件。

[[email protected] scripts]# vim find.sh 
#!/bin/bash 
 
for file in `find /data/logs -type f -name '*.log' -size +200M` 
do 
        filename=`basename  ${file}` 
        dirname=`dirname  ${file}` 
        cd ${dirname} 
        tar -zcPf ${filename}.tar.gz ${filename} --remove-files 
done

2019.12.06

 1. CentOS 6&7 初始化脚本

# 首先需要启动你的网卡(根据实际的网卡名称,可用ip a查看网卡名)。 
[[email protected] scripts]# ifup eth0 
[[email protected] scripts]# cat optimize.sh 
#!/bin/bash 
 
# Author:JingXue 
# CreateDate:2019/12/06 
# Description:CentOS 6&7 初始化脚本 
 
NET=`ip a | egrep 'ens|eth0' | head -1 | awk -F '[ :]+' '{print $2}'` 
VERSION=`cat /etc/redhat-release | awk -F '.' '{print $1}' | awk '{print $NF}'` 
 
set_network () { 
        egrep 'ONBOOT=yes|ONBOOT="yes"' /etc/sysconfig/network-scripts/ifcfg-${NET} &>/dev/null || sed -i 's/ONBOOT.*/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-${NET} 
        grep persistent /etc/rc.local &>/dev/null || echo '>/etc/udev/rules.d/70-persistent-net.rules' >>/etc/rc.local            # 初始化模板时需要执行 
        egrep 'HWADDR|UUID' /etc/sysconfig/network-scripts/ifcfg-${NET} &>/dev/null && sed -ri '/HWADDR|UUID/d' /etc/sysconfig/network-scripts/ifcfg-${NET}   # 初始化模板时需执行 
} 
 
set_yum_repos() {                       # 阿里云主机可省此步骤 
    grep aliyun CentOS-Base.repo.bak &>/dev/null 
    if [ $? != 0 ];then 
        mv /etc/yum.repos.d/CentOS-Base.repo{,.bak} 
        if [ ${VERSION} = 6 ];then 
            curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo 
        elif [ ${VERSION} = 7 ];then 
            curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 
        fi 
    else 
        continue 
    fi 
} 
 
set_public() { 
    yum clean all && yum makecache fast 
    yum -y update 
    yum -y install epel-release 
    yum -y install tree lrzsz nmap nc telnet vim wget lsof net-tools bash-completion bash-completion-extras psmisc bind-utils sysstat htop yum-utils sshpass git expect unzip 
    grep 'rm=' /etc/bashrc &>/dev/null || echo "alias rm='rm is not used'" >>/etc/bashrc 
    grep 'PS1=' /etc/profile &>/dev/null || echo 'export PS1="\[\e[37;40m\][\[\e[31;40m\]\u\[\e[37;40m\]@\h \[\e[33;40m\]\W\[\e[0m\]]\\$ "' >>/etc/profile 
    source /etc/profile /etc/bashrc 
    grep 'SELINUX=disabled' /etc/selinux/config &>/dev/null || sed -i.bak 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config      # 阿里云ECS可省 
    setenforce 0        # 阿里云ECS可省 
    grep 'time sync' /var/spool/cron/root &>/dev/null || echo -e '# time sync\n*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null' >>/var/spool/cron/root    # 阿里云ECS可省 
} 
 
set_limits() { 
    grep 'nofile 65536' /etc/security/limits.conf &>/dev/null || echo -e '* soft nofile 65536\n* hard nofile 65536' >>/etc/security/limits.conf 
cat >>/etc/sysctl.conf <<EOF 
net.ipv4.tcp_syncookies = 1 
net.ipv4.tcp_tw_reuse = 1 
net.ipv4.tcp_tw_recycle = 1 
net.ipv4.tcp_fin_timeout = 30 
vm.max_map_count=262144 
EOF 
    sysctl -p 
    if [ ${VERSION} = 6 ];then 
        sed -i 's/1024/4096/g' /etc/security/limits.d/90-nproc.conf 
    else 
        continue 
    fi 
} 
 
set_hostname() { 
    read -p '请设置主机名:' HOSTNAME 
    if [ ${VERSION} = 6 ];then 
        hostname ${HOSTNAME} 
        sed -i.bak "s/^HOST.*/HOSTNAME=${HOSTNAME}/" /etc/sysconfig/network 
        sed -i.bak "s/localhost\.localdomain/${HOSTNAME}/" /etc/hosts 
    elif [ ${VERSION} = 7 ];then 
        hostnamectl set-hostname ${HOSTNAME} 
        sed -i.bak "s/localhost\.localdomain/${HOSTNAME}/" /etc/hosts 
    fi 
 
} 
 
set_services() { 
    if [ ${VERSION} = 6 ];then 
        service iptables stop 
        service postfix stop 
        chkconfig iptables off 
        chkconfig postfix off 
        chkconfig network on 
        chkconfig sshd on 
        chkconfig crond on 
    elif [ ${VERSION} = 7 ];then 
        systemctl start sshd.service network.service crond.service 
        systemctl enable sshd.service network.service crond.service 
        systemctl stop NetworkManager firewalld.service postfix.service 
        systemctl disable NetworkManager firewalld.service postfix.service 
    fi 
} 
 
set_network 
set_yum_repos 
set_public 
set_limits 
set_hostname 
set_services 
 
echo -e "\033[31m*******************系统初始化完毕,将在20s后重启!!!*******************\033[0m" 
sleep 20s && reboot 



未完待续......

标签:linuxShell
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
关注我们

扫一扫关注我们,了解最新精彩内容