
shell脚本
shell脚本
weshell_
花有重开日,人无再少年。
展开
-
利用/etc/hosts.deny防ssh暴力破解
前言之前写了个利用防火墙防ssh暴力破解脚本传送门但是利用iptables需要重启服务才能使黑名单ip生效后面发现用/etc/hosts.deny文件可以不重启实现黑名单代码#!/bin/bashlogFile=/opt/cronTask/log/ssh_deny.log# 白名单whiteList=""while truedo # 过滤登陆失败大于5次的ip ipList=$(grep "Failed password" /var/log/secure|awk原创 2021-08-04 15:37:17 · 66885 阅读 · 0 评论 -
cron任务不生效问题
问题背景之前写了个使用ansible收集服务器负载和磁盘空间的脚本,想着用crontab每天定时跑一下。但是发现无论怎么改,重启都没有生效,但是手动跑脚本就可以,后来就不了了之了。问题原因今天有时间查了下,发现有其他人也有类似的问题,在/var/spool/mail/root可以看到失败原因/var/spool/mail/root记录了一些系统信息,包括了crontab失败的信息之前也是因为提示太频繁把这个推送给关了vim /var/spool/mail/root解决现在问题原创 2021-06-16 09:57:37 · 81017 阅读 · 0 评论 -
linux自动将恶意登录ip加入防火墙黑名单
背景服务器每天都有大量的登陆失败信息查看日志tail -n 15 /var/log/secure加黑名单脚本#!/bin/bashlogFile=/opt/cronTask/log/login_black_list.txt# 过滤登陆失败大于5次的ipipList=$(grep "Failed password" /var/log/secure|awk '{print $(NF-3)}'|sort|uniq -c|sort -nr|awk '{if($1>=5) print $2原创 2021-06-09 17:17:18 · 83843 阅读 · 0 评论 -
根据mongo数据库列表动态备份
需求不同服务器上的mongo数据库可能不一样,而且可能数据库表会增加或者删减。因此写成固定的列表无法满足需求方案通过与shell交互操作实现mongo命令查看数据库表项代码#!/bin/bashset -x# collections=mongo $dbName --eval "db.getCollectionNames()"# echo $collectionsret=`mongo << EOFshow dbsEOF`# mongo version forma原创 2021-05-31 00:01:10 · 86517 阅读 · 0 评论 -
使用ansible批量给服务器添加环境变量
因为服务器比较多,一个个添加比较麻烦,故写个shell脚本批量添加测试代码#!/bin/bashset -xwan=(wan1 wan2 ...)lan=(lan1 lan2 ...)domains=yxn=0 for i in ${wan[@]}do ansible $i -m shell -a 'echo \# sid >> ~/.bashrc' ansible $i -m shell -a 'echo "export domain='${domains[原创 2021-04-28 10:00:52 · 97320 阅读 · 0 评论 -
git bash使用chmod不生效
问题现象在windows下使用git bash执行shell命令$ chmod +x test.sh-rw-r--r-- 1 OME 197121 997 Apr 21 10:08 test.sh·问题原因shell脚本前面没有加上#!/bin/bash原创 2021-04-21 10:21:09 · 98445 阅读 · 0 评论 -
shell遍历名称带有空格的文件或目录
问题背景:遍历带有空格的文件,直接把单个文件分隔成两个#!/bin/bashfor i in `ls 1/`do echo -e "file = $i\n" cat 1/$i >> total.txtdone目录结构问题原因:在linux中内置分隔符IFS(Internal Field Seperator)默认为空格、制表符、换行符查看默认分隔符:手动设置IFS排除空格分隔符IFS=$'\n'for i in `ls 1/`do echo -原创 2021-04-03 01:31:18 · 98952 阅读 · 0 评论 -
脚本汇总文件(sed的使用)
需求:1、将指定名称的文件拷贝到指定目录;2、并把文件路径间隔符/替换成_此处以统计Jenkinsfile为例#!/bin/bashfilelist=`find . -name Jenkinsfile`i=0for f in $filelistdo echo "file:$f" name=`echo $f |sed 's!\.\/!!g'|sed 's!\/!_!g'` cp $f ./jflist/$name let i++done注意此处的路.原创 2021-03-25 11:13:59 · 96734 阅读 · 1 评论 -
linux定时对git拉取和推送
准备工作查看当前拉取和推送地址git remote -v更改当前地址为ssh地址,通过将ssh公钥放到git实现免密登陆git remote set-url ssh地址pull时告警去除# 3种pull策略根据自己的需求选择git config pull.rebase false # 合并(缺省策略)git config pull.rebase true # 变基git config pull.ff only # 仅快进pull#!/bin/bashd原创 2021-03-24 17:21:14 · 96896 阅读 · 1 评论 -
重新登陆终端,后台运行脚本无输出问题定位
测试脚本mytest.sh#!/bin/bashlocaltime=`date "+%Y-%m-%d_%H:%M:%S"`logfile=logs/${localtime}.txtPID=$(ps aux| grep node_exporter | grep -v 'grep' | awk '{print $2}')while [ "PID" != "" ]do cpuinfo=$(top -n 1 -b -d 1 | grep 'node_exporter'|grep -v原创 2021-03-17 11:29:09 · 96678 阅读 · 0 评论 -
脚本计算后台程序消耗资源
脚本计算node_exporter在服务器上消耗的cpu和内存#!/bin/bashproc=node_exporterlogfile=node_log.txtlocaltime=`date "+%Y-%m-%d_%H:%M:%S"`PID=$(ps aux| grep node_exporter | grep -v 'grep' | awk '{print $2}')while [ "PID" != "" ]do cpu=$(ps aux| grep node_exporter原创 2021-03-15 12:17:02 · 96827 阅读 · 0 评论