0%

Linux常用脚本

记录日常使用到的脚本

Git切换开关

cat git-proxy

1
2
3
4
5
6
#!/bin/bash
if [[ $1 == "no" ]];then
git config --global --unset https.proxy
else
git config --global https.proxy socks5://192.168.2.2:10808
fi

放入/usr/local/bin/git-proxy

1
2
git-proxy     #开启代理
git-proxy no #关闭代理

本地hosts模拟实现DNS轮询脚本

cat auto_change_hosts.sh

1
2
3
4
#!/bin/bash
MY_PATH=$(cd "$(dirname "$0")"; pwd)
cd $MY_PATH

cat iplist

1
2
3
4
5
11.5.100.20010
11.5.100.20100
11.5.200.20200
11.5.200.20300
11.5.300.20400

cat /etc/hosts

1
11.5.100.200 malu.me

使用方法:

1
auto_change_hosts.sh [error]

如果加参数error,则iplist最后一列数值会累加,用于调用次数统计。

创建低权限用户脚本

首先添加用户boy并修改sshd_config文件

1
2
useradd boy
passwd boy

如果遇到无法找到/bin/sh的提示,则修改/etc/passwd文件中boy的shell

1
把 /bin/sh 改成 /bin/bash

vim /etc/ssh/sshd_config

1
2
Match User boy
ChrootDirectory /home/test

cat create_low_user.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
CHROOT_PATH='/home/test'
mkdir -p ${CHROOT_PATH}/dev/
cd ${CHROOT_PATH}/dev/
mknod -m 666 null c 1 3
mknod -m 666 tty c 5 0
mknod -m 666 zero c 1 5
mknod -m 666 random c 1 8

chown root:root ${CHROOT_PATH}
chmod 0755 ${CHROOT_PATH}
ls -ld ${CHROOT_PATH}

mkdir -p ${CHROOT_PATH}/bin
cp -v /bin/bash ${CHROOT_PATH}/bin/

ldd /bin/bash
mkdir -p ${CHROOT_PATH}/lib64
cp -v /lib64/ld-linux-x86-64.so.2 ${CHROOT_PATH}/lib64/
mkdir -p ${CHROOT_PATH}/lib/x86_64-linux-gnu/
cp -v /lib/x86_64-linux-gnu/{libtinfo.so.5,libdl.so.2,libc.so.6} ${CHROOT_PATH}/lib/x86_64-linux-gnu/

把本地文件夹备份至远程服务器

1.通过scp拷贝目录

1
2
3
#!/bin/bash
dd=`date '+%Y-%m-%d-%H%M%S'`
scp -i /root/.ssh/id_rsa -P 22 -r /local root@malu.me:/backup/${dd}

2.拷贝目录至windows共享 (如果有防火墙,需允许445, 135, 137, 138, 139 UDP和TCP)

1
2
3
#!/bin/bash
dd=`date '+%Y-%m-%d-%H%M%S'`
smbclient -c "mkdir ${dd};prompt OFF;recurse ON;lcd /local;cd ${dd};mput *" //IP/share -U username%password

切换到当前目录执行脚本

1
2
3
#!/bin/bash
MY_PATH=$(cd "$(dirname "$0")"; pwd)
cd $MY_PATH

防止脚本重复执行(每次只有一个脚本在执行)

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
function run_only_one(){
PID=$$
snum=`ps -efgrep $0grep -v grepgrep -v " $PPID "grep -v " $PID "wc -l`
if [ $snum -lt 1 ]; then
echo "start process....."
else
echo "runing....."
exit
fi
}
run_only_one

scp拷贝最近3天日期命名的tar.z文件,并删除本地6天前的tar.z文件

1
2
3
4
5
6
7
8
#!/bin/bash
for((i=0;i<=3;i++)); do
datefile=`date -d"$i day ago" +%Y%m%d`.tar.z
if [ ! -f "$datefile" ]; then
scp malu.me:/backup/$datefile /home/backup/
fi
done
find ./ -mtime +6 -type f -name "*.tar.z"xargs rm -f

合并指定目录下的所有文本,并去除重复行,并过滤^M字符 (生成超级字典)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
if [ $# != 1 ] ; then
echo "USAGE: $0 FLODER"
exit 1;
fi
root_dir=$1
NEWFILE=newfile_dic.txt
NEWFILE_TMP=newfile_dic.txt.tmp
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
function getdir(){
for element in `ls $1`
do
dir_or_file=$1"/"$element
if [ -d $dir_or_file ]
then
getdir $dir_or_file
else
echo -n $dir_or_file
cat $dir_or_file sed 's/^M//g'>> $NEWFILE_TMP
echo ' ok'
fi
done
}
getdir $root_dir
IFS=$SAVEIFS
sort $NEWFILE_TMPuniq > $NEWFILE
rm -f $NEWFILE_TMP

注意:^M的输入方式是 Ctrl + v ,然后Ctrl + m

杀死进程及其所有子进程

1
pstree -p PID  grep -oP '(?<=\()[0-9]+(?=\))'xargs kill -9

杀死包含关键字的进程,及其所有子进程

1
2
3
4
5
6
#!/bin/bash
PID=`ps -efgrep [n]ameawk '{print $2}'`
expr 1 + $PID > /dev/null 2>&1
if [[ $? -eq 0 ]]; then
pstree -p $PID grep -oP '(?<=\()[0-9]+(?=\))'xargs kill -9
fi

将目录下所有文件名改成小写

1
for i in `find ./`;do mv $i `echo $i tr [A-Z] [a-z]`;done

防御CC攻击脚本

1
2
3
4
5
6
7
8
9
10
#!/bin/sh
status=`netstat -naawk '$5 ~ /[0-9]+:[0-9]+/ {print $5}' awk -F ":" -- '{print $1}' sort -nuniq -c sort -ntail -n 1`
NUM=`echo $statusawk '{print $1}'`
IP=`echo $statusawk '{print $2}'`
result=`echo "$NUM > 150" bc`
if [ $result = 1 ]
then
echo IP:$IP is over $NUM, BAN IT!
/sbin/iptables -I INPUT -s $IP -j DROP
fi

向进程发送按键指令

其实用C写的,算不上脚本。需要先编译:

gcc key.c -o key.bin

运行:

./key.bin PID cmd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <sys/ioctl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>

void stackchar(int fd,char c)
{
if (ioctl(fd, TIOCSTI, &c) < 0) {
perror("ioctl");
exit(1);
}
}

int main(int argc, char *argv[])
{
int i, j;
char c;
char proc_file[20];
int mixer_fd=0;

strcpy(proc_file,"/proc/");
strcat(proc_file,argv[1]);
strcat(proc_file,"/fd/1");

if ((mixer_fd = open(proc_file,O_WRONLY))){
for (i = 2; i < argc; i++) {
if (i > 1) stackchar(mixer_fd,' ');
for (j=0; (c = argv[i][j]); j++) {
stackchar(mixer_fd,c);
}
}
}

向本地监听的所有匹配端口发送curl请求

1
netstat -lnt4pgrep sshdgrep -v ':22 'awk -F '0.0.0.0:' '{print $2}'awk '{print $1}'xargs -t -i curl localhost:{}