Coder Social home page Coder Social logo

360-ops's Introduction

360-OPS

answer

每题对应相同序号的 md 文件,问题与答案均置于 md 文件中。

360-ops's People

Contributors

gosoon avatar strugglingyouth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

360-ops's Issues

11.sh

执行此脚本前,需要先进行公钥认证,先将本机的公钥复制到目标主机上

vssh.sh

!/bin/bash

${!#}-->$#--->$$

cmd=${!#}
logfile=$(mktemp)

i=1
success=0
failed=0
for ip in $@;do
if [ $i -eq $# ];then
break
fi
ssh $ip $cmd &> $logfile
if [ $? -eq 0 ];then
((success++))
echo -e "\n\033[32m $ip | success >> \033[0m \n"
cat $logfile
else
((failed++))
echo -e "\n\033[31m $ip | failed >> \033[0m\n "
cat $logfile
fi
((i++))
done
echo -e "\n\033[32m success: $success || failed: $failed \033[0m"

11

2.sh

脚本放在这里没有缩进了

!/bin/bash

Tmpfile1=$(mktemp)
Tmpfile2=$(mktemp)
Tmpfile3=$(mktemp)
ifconfig | grep '^[^[:space:]]' | awk '{print $1}' > $Tmpfile1
ifconfig | grep -A 1 '^[^[:space:]]' | awk '/addr:/ {print $2}' | awk -F ':' '{print $2}' > $Tmpfile2

paste $Tmpfile1 $Tmpfile2 > $Tmpfile3

cat $Tmpfile3 | while read line;do
key=echo $line|awk '{print $1}' value=echo $line |awk '{print $2}'
echo $key $value
done

default

6.sh

cpu_info.sh

!/bin/bash

save_file="/tmp/date +%F-cpuinfo"

cpuinfo=top -bn 1 | grep "Cpu"

if [ ! -e $save_file ] ;then
cat << EOF >> $save_file
注:
us:用户态使用的cpu时间比
sy:系统态使用的cpu时间比
ni:用做nice加权的进程分配的用户态cpu时间比
id:空闲的cpu时间比
wa:cpu等待磁盘写入完成时间
hi:硬中断消耗时间
si:软中断消耗时间
st:虚拟机偷取时间
EOF

echo -e "\033[32m \t\t\t\t\t\tdate:date +%F\033[0m\n" >> $save_file
echo -e "times:\t\tus:\t\tsy:\t\tni:\t\tid:\t\twa:\t\thi:\t\tsi:\t\tst:\t\n" >> $save_file
fi

us=$(echo $cpuinfo | awk '{print $2}')
sy=$(echo $cpuinfo | awk '{print $3}')
ni=$(echo $cpuinfo | awk '{print $4}')
id=$(echo $cpuinfo | awk '{print $5}')
wa=$(echo $cpuinfo | awk '{print $6}')
hi=$(echo $cpuinfo | awk '{print $7}')
si=$(echo $cpuinfo | awk '{print $8}')
st=$(echo $cpuinfo | awk '{print $9}')

hour=$(date +%H)
echo -e "${hour}\t\t${us}\t\t${sy}\t\t${ni}\t\t${id}\t${wa}\t\t${hi}\t\t${si}\t\t${st}" >> $save_file

然后创建一个定时任务,每小时收集一次cpu的信息:

0 */1 * * * /bin/bash /root/shell/cpu_info.sh &> /dev/null

6

4.sh

cat会先将文件读入内存,如果文件过大,会撑爆内存,还有,脚本执行时间太长

!/bin/bash

LANG=en_US

Usage() {
echo "Usage: $0 Logfile"
}

if [ $# -eq 0 ] ;then
Usage
exit 0
else
Log=$1
fi

cat $Log | while read line;do
Year=$(echo $line | awk '{print $4}'| awk -F '/' '{print $3}' |awk -F ':' '{print $1}')
Month=$(echo $line | awk '{print $4}'| awk -F '/' '{print $2}')
Day=$(echo $line | awk '{print $4}'| awk -F '/' '{print $1}' | sed 's/[//')
echo $line &gt;&gt; /tmp/log/${Year}-${Month}-${Day}
done

文件大小:

default

cat_time

程序执行的结果,8.4M的文件执行了5分多钟,心累:

result_log

3.sh

程序先输出定时任务,然后选择需要操作的序号,选择序号后再选择对其的操作 [ start | stop ],start 与 stop 是设置是否将其注释掉来控制的

!/bin/bash

[ $UID -ne 0 ] && echo "please use root to excute!"
[ -e /etc/init.d/functions ] && . /etc/init.d/functions || exit

task_file=/var/spool/cron/root
crontabfile=$(mktemp)
crontab -l > $crontabfile

delete blank line

sed -i '/^$/d' $crontabfile

line=$(cat $crontabfile | wc -l)

crontab_list(){
echo -e "\n\033[32m crontab as follows:\033[0m\n"
cat -n $crontabfile
}

input the number of modify

input_num(){
while true;do
echo -e "\n\033[32mplease input a task number:\033[0m\n"
read num
if [ "$num" -lt 1 -o "$num" -gt "$line" ];then
echo "input error!Please input again:"
continue
else
break
fi
done
}

crontab_list
input_num

operate task

while true;do

read -p "please input to perform operations:[ stop | start | quit ] " do

case $do in
quit|q):
exit
;;
start):
task=$(head -n $num $crontabfile | tail -n 1)
echo "$task" | grep '^#' &> /dev/null

       if [ $? -eq 0 ];then
           sed -i "${num}s/^#//" $crontabfile
           cat "$crontabfile" > $task_file
           action "success!" /bin/true
           break
       else
           echo "This task is already start.Pleasse input again:"
       fi    

       ;;
   stop):
       task=$(head -n $num $crontabfile | tail -n 1)
       echo "$task" | grep '^[^#]' &> /dev/null
       if [ $? -eq 0 ];then
           sed -i "${num}s/^/#/" $crontabfile
           cat "$crontabfile" > $task_file
           action "success!" /bin/true
           break
       else
           echo "This task is already stop.Pleasse input again:"
       fi    
       ;;
   *):
       echo "Input is error! Please input again:"
       ;;

esac
done
crontab_list

3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.