Linux 杂记
说明:在使用linux的过程中的一些笔记,都是别人经验的总结。因为当时写在本地,没有写摘抄自何处,所以如果有侵权的地方,请联系本人。
命令解读
jobs 命令
查看当前终端后台运行的任务
文本排序分析
cat fenxi.txt | sort | uniq -c | sort -gr > analysis.txt
fenxi.txt是每行只有一个字符串的文本文件,先试用sort进行排序。uniq会将临近的重复的行合并,所以需要先用sort
进行排序。uniq -c
的作用是在每行开头增加重复次数。然后再使用sort -gr
进行统计**-g根据数字排序**,-r结果倒序排列。
显示当前端口占用情况
netstat -tunlp
用于显示 tcp,udp 的端口和进程等相关情况。
netstat 查看端口占用语法格式:
netstat -tunlp | grep 端口号
- -t (tcp) 仅显示tcp相关选项
- -u (udp)仅显示udp相关选项
- -n 拒绝显示别名,能显示数字的全部转化为数字
- -l 仅列出在Listen(监听)的服务状态
- -p 显示建立相关链接的程序名
例如查看 8000 端口的情况,使用以下命令:
# netstat -tunlp | grep 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 26993/nodejs
获取当前正在执行脚本的绝对路径
SHELL_FOLDER=$(dirname $(readlink -f "$0"))
grep 拿到错误输出匹配的部分
sshd -v 2>&1 | grep -o 'OpenSSH_[0-9]\.[0-9]'
sshd -v 是一个错误的命令,但是错误内容中可以显示openssh的版本信息,然后使用grep 匹配字符串。 grep -o
只打印出匹配的部分。
kill掉某tty终端
先使用w
查看想要kill掉的终端,然后 fuser -k /dve/pts/0
因为可能不存在fuser命令,可以使用pkill -kill -t pts/2
shell脚本调试
可以使用命令 sh -v
生成一个不包含特定目录的列表
想要生成一个不包含yara目录的列表。这个列表包含目录和文件用于其他的扫描。比如包含/root/aa/bb目录,那么列表里就不能有/root/aa/bb/cc目录。他们都必须是不包含的。
yara_path=`pwd`
#找到yara所在目录的深度
depth=$(echo $yara_path | grep -o "/" | wc -l)
path=$(find / -mindepth $depth -maxdepth $depth -type d | grep -v "/proc\|/mnt\|/dev\|yara")
for file in $path
do
echo $file >> aaa.txt
done
path2=$(find / -maxdepth $depth -type f | grep -v "/proc\|/mnt\|/dev\|yara")
for file in $path2
do
echo $file >> aaa.txt
done
命令行编辑快捷键
跳转到命令行行首:ctrl+a
跳转到命令行行尾:ctrl+e
shell 杂记
判断文件是否存在
#!/bin/sh
myPath="/var/log/httpd/"
myFile="/var /log/httpd/access.log"
# 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限
if [ ! -x "$myPath"]; then
mkdir "$myPath"
fi
# 这里的-d 参数判断$myPath是否存在
if [ ! -d "$myPath"]; then
mkdir "$myPath"
fi
# 这里的-f参数判断$myFile是否存在
if [ ! -f "$myFile" ]; then
touch "$myFile"
fi
# 其他参数还有-n,-n是判断一个变量是否是否有值
if [ ! -n "$myVar" ]; then
echo "$myVar is empty"
exit 0
fi
# 两个变量判断是否相等
if [ "$var1" = "$var2" ]; then
echo '$var1 eq $var2'
else
echo '$var1 not eq $var2'
fi
-f 和-e的区别
Conditional Logic on Files
-a file exists.
-b file exists and is a block special file.
-c file exists and is a character special file.
-d file exists and is a directory.
-e file exists (just the same as -a).
-f file exists and is a regular file.
-g file exists and has its setgid(2) bit set.
-G file exists and has the same group ID as this process.
-k file exists and has its sticky bit set.
-L file exists and is a symbolic link.
-n string length is not zero.
-o Named option is set on.
-O file exists and is owned by the user ID of this process.
-p file exists and is a first in, first out (FIFO) special file or
named pipe.
-r file exists and is readable by the current process.
-s file exists and has a size greater than zero.
-S file exists and is a socket.
-t file descriptor number fildes is open and associated with a
terminal device.
-u file exists and has its setuid(2) bit set.
-w file exists and is writable by the current process.
-x file exists and is executable by the current process.
-z string length is zero.
shell 文件包含
. filename # 注意点号(.)和文件名中间有一空格
或
source filename
Shell中的$0、$1、$2的含义
在shell脚本中看到的$0
是你写的shell脚本本身的名字,$1
是你给你写的shell脚本传的第一个参数,$2
是你给你写的shell脚本传的第二个参数
Shell ∗ 和 *和 ∗和@的区别
$*
和 $@
都表示传递给函数或脚本的所有参数。当 $* 和 $@ 不被双引号" “包围时,它们之间没有任何区别,都是将接收到的每个参数看做一份数据,彼此之间以空格来分隔。
当它们被双引号” "包含时,就会有区别了:
"$*"
会将所有的参数从整体上看做一份数据,而不是把每个参数都看做一份数据。"$@"
仍然将每个参数都看作一份数据,彼此之间是独立的。
比如传递了 5 个参数,那么对于" ∗ " 来 说 , 这 5 个 参 数 会 合 并 到 一 起 形 成 一 份 数 据 , 它 们 之 间 是 无 法 分 割 的 ; 而 对 于 " *"来说,这 5 个参数会合并到一起形成一份数据,它们之间是无法分割的;而对于" ∗"来说,这5个参数会合并到一起形成一份数据,它们之间是无法分割的;而对于"@"来说,这 5 个参数是相互独立的,它们是 5 份数据。
${ } 的一些特殊功能: 获取文件名,获取文件目录
假设我们定义了一个变量为:file=/dir1/dir2/dir3/my.file.txt
${file#*/}:拿掉第一条 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt
${file##*/}:拿掉最后一条 / 及其左边的字符串:my.file.txt
${file#*.}:拿掉第一个 . 及其左边的字符串:file.txt
${file##*.}:拿掉最后一个 . 及其左边的字符串:txt
${file%/*}:拿掉最后条 / 及其右边的字符串:/dir1/dir2/dir3
${file%/*/*}: 拿到的结果为 /dir1/dir2
${file%%/*}:拿掉第一条 / 及其右边的字符串:(空值)
${file%.*}:拿掉最后一个 . 及其右边的字符串:/dir1/dir2/dir3/my.file
${file%%.*}:拿掉第一个 . 及其右边的字符串:/dir1/dir2/dir3/my
记忆的方法为:
# 是去掉左边(在鉴盘上 # 在 $ 之左边)
% 是去掉右边(在鉴盘上 % 在 $ 之右边)
单一符号是最小匹配﹔两个符号是最大匹配。
${file#/}(不加*号)表示只去掉最左边的/
命令
awk
awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大。awk把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。
awk中的内置变量$NF
awk中的$NF,表示字段数,在执行过程中对应于当前的字段数。
例子:
netstat -an | grep 22
-------------------------------------------------
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 192.168.125.86:22 10.80.73.229:54425 ESTABLISHED
tcp 0 48 192.168.125.86:22 10.80.73.229:54415 ESTABLISHED
tcp 0 0 192.168.125.86:22 192.168.125.85:40346 ESTABLISHED
tcp 0 0 192.168.125.86:22 192.168.125.85:40406 ESTABLISHED
tcp6 0 0 :::22 :::* LISTEN
netstat -an | grep 22 | awk '{print $NF}'
-------------------------------------------------------
LISTEN
ESTABLISHED
ESTABLISHED
LISTEN