实验1
脚本要求:实现传入进程pid,查看对应进程/proc下CPU和内存指标
#!/bin/bash
#
#*********************************************************
#Author: Shen
#Date: 2019-10-07
#Filename: pid_proc.sh
#Description: description of the script
#Copyright(C): 2019 All rights reserved
#*********************************************************
read -p "Please input pid number: " pid
ps aux|tr -s ' ' ':'|cut -d':' -f 2|grep -q $pid && echo Process "$pid" information: || (echo Process "$pid" does not exist!; exit 4)
cat /proc/$pid/'status' | grep -i "^cpu"
cat /proc/$pid/'status' | grep -i "^mem"
实验2
脚本要求:实现每分钟检查一次主机端口是否存活,若不在线,则sleep 10s,3次都不存在,则记录到日志
#!/bin/bash
#
#*********************************************************
#Author: Shen
#Date: 2019-10-07
#Filename: port_scan.sh
#Description: description of the script
#Copyright(C): 2019 All rights reserved
#*********************************************************
read -p "Please input a port: " port
num=0
echo Checking Port $port ······
while [ $num -le 3 ];do
status=`nmap 127.0.0.1 -p $port | grep "^$port" | cut -d' ' -f2`
if [ $status = "open" ];then
echo Port $port is open
break
elif [ $status = "closed" ];then
sleep 10
let num++
if [ $num -eq 3 ];then
echo Port $port is $status >> /data/nmap.log
echo "Result has been redirected to /data/nmap.log"
break
fi
fi
done
实验3
脚本要求:判断参数文件是否为sh后缀的普通文件,若是,则添加所有人可执行权限,否则提示用户非脚本文件
#!/bin/bash
#
#*********************************************************
#Author: Shen
#Date: 2019-10-07
#Filename: excute.sh
#Description: description of the script
#Copyright(C): 2019 All rights reserved
#*********************************************************
read -p "Please Input File(path and name both included): " file
if echo "$file" | grep -q '.sh$' &> /dev/null;then
chmod +x $file
else
echo "$file is not excutable file"
fi
实验4
脚本要求:实现禁止和允许普通用户登录
#!/bin/bash
#
#*********************************************************
#Author: Shen
#Date: 2019-10-07
#Filename: login.sh
#Description: description of the script
#Copyright(C): 2019 All rights reserved
#*********************************************************
read -p 'Please Input An User Name: ' name
if grep "^$name:" /etc/passwd &>/dev/null;then
sed -i -r "s#(^$name:.*:).*#\1/bin/bash#" /etc/passwd
echo User $name has been allowed to login!!!
else
read -p 'User does not exist!'
exit 4
fi
#!/bin/bash
#
#*********************************************************
#Author: Shen
#Date: 2019-10-07
#Filename: nologin.sh
#Description: description of the script
#Copyright(C): 2019 All rights reserved
#*********************************************************
read -p 'Please Input An User Name: ' name
if grep "^$name:" /etc/passwd &>/dev/null;then
sed -i -r "s#(^$name:.*:).*#\1/sbin/nologin#" /etc/passwd
echo User $name has been banned to login!!!
else
read -p 'User does not exist!'
exit 4
fi
实验5
脚本要求:计算/etc/passwd文件中第10个和第20个之间所有用户的id之和
#!/bin/bash
#
#*********************************************************
#Author: Shen
#Date: 2019-10-07
#Filename: sumid.sh
#Description: description of the script
#Copyright(C): 2019 All rights reserved
#*********************************************************
id=`sed -nr '10,20s#.*:([0-9]+):[0-9].*#\1#p' /etc/passwd`
num=(`echo $id`)
for i in `seq 0 10`;do
let sum+=${num[$i]}
done
echo $sum
实验6
用shell脚本实现自动登录机器
#!/usr/bin/expect
set user root
set ip 192.168.136.130
set password abc123
set teimeout 10
spawn ssh $user@$ip
expect {
"yes/no" { send "yes\n" ;exp_continue }
"password" { send "$password\n" }
}
interact
实验7
shell 判断一个值bone是否在数组arrayZ=( one two three four five five )中
#/bin/bash
declare -a arrayZ
arrayZ=(one two three four five six)
for i in $(seq 0 $[`echo ${#arrayZ[@]}`-1]);do
if [ ${arrayZ[$i]} == bone ];then
echo "${arrayZ[$i]} exists in arrayZ"
exit 0
fi
done
echo "bone does not exist in attayZ"
实验8
用命令或者脚本实现 0057AF051EFF 变为 00:57:AF:05:1E:FF
#!/bin/bash
str="0057AF051EFF"
for i in `seq 0 2 10`;do
if [ $i -le 8 ];then
STR=`echo ${str:$i:2}:`
echo -n $STR
elif [ $i -eq 10 ];then
STR=`echo ${str:$i:2}`
echo -n $STR
fi
done
echo
实验9
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9 0 ! @ # $ % ^ & * ( ) - _ = + \ / ’ " ; : [ ] { } , . ?
用以上字符,结合数组,实现一个随机生成20位密码的脚本
#/bin/bash
declare -a array
array=(a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9 0 \! \@ \# \$ \% \^ \& \* \( \) \- \_ \= \+ \\ \/ \' \" \; \: \[ \] \{ \} \, \. \?)
for ((i=0;i<=19;i++));do
num=$RANDOM%89
echo -n ${array[$num]}
done
实验10
判断UID是否大于等于500,如果为真就显示为普通用户,如果为假就显示为系统或管理用户
/bin/bash
read -p "Please input a user name: " user
user_num=`getent passwd $user|awk -F: '{print $3}'`
if [ $user_num -gt 500 ];then
echo $user is System user
else
echo $user is Normal user
fi
实验11
显示用户id为奇数的用户
#!/bin/bash
for i in `cat /etc/passwd|awk -F: '{print $1":"$3}'`;do
if [ $[`echo $i|awk -F":" '{print $2}'`%2] != 0 ];then
echo $i
fi
done
实验12
统计web服务访问日志中的ip访问量
cat access.log |cut -d ' ' -f 1 |sort |uniq -c | sort -nr | awk '{print $0 }'