简介:工欲善其事必先利其器,一些高效的shell命令相关可以极大提升工作效率。本人小白程度,欢迎补充纠正。
历史攻略:
locust2.0+教程:016 - 结合ssh压测shell命令
Python:paramiko+shell交互式处理sudo等交互输入密码等场景
汇总区:
1、查看文件内匹配的字符串及其个数
2、当前路径排序文件大小
3、查看某命令占用的端口
4、查看打印cpu、内存、系统信息、及其当前时间
5、检查网络和端口开放情况
6、设置ssh免密
7、校验文件完整性MD5
8、快捷键
1、查看文件内匹配的字符串及其个数:
匹配符合的行:grep -a -i “hg” myfile.log
匹配符合的行的个数:grep -a -i “hg” myfile.log | wc -l
案例:
tom@tom:~/tests$ cat myfile.log
dsaf
sdaf
sdf
sdf
43rt
yhg
cfg54w
56
67
hg
dgh
tom@tom:~/tests$ grep -a -i "hg" myfile.log
yhg
hg
tom@tom:~/tests$ grep -a -i "hg" myfile.log | wc -l
2
tom@tom:~/tests$
2、当前路径排序文件大小:du -sh ./ | sort -rh*
案例:
$ du -sh ./* | sort -rh
2.1G ./myfile-02.img
1.1G ./myfile-01.img
4.0K ./myfile.log
3、查看某命令占用的端口:
ps aux | grep “运行的名称”
案例:窗口1
tom@tom:~/tests$ vim test_run.sh
tom@tom:~/tests$
tom@tom:~/tests$
tom@tom:~/tests$ cat test_run.sh
#!/bin/bash
echo 123
sleep 30
echo 456
sleep 40
tom@tom:~/tests$ ./test_run.sh
123
Killed
tom@tom:~/tests$
窗口2:在1运行后,2查出PID端口,并杀死PID进程
tom@tom:~$ ps aux | grep "test_run"
tom 1130 0.0 0.0 13320 3280 pts/1 S+ 23:13 0:00 /bin/bash ./test_run.sh
tom 1133 0.0 0.0 14860 1052 pts/2 S+ 23:13 0:00 grep --color=auto test_run
tom@tom:~$ sudo kill -9 1130
tom@tom:~$
4、查看打印cpu、内存、系统信息、及其当前时间:
hostnamectl && free -h && lscpu && date
案例:
tom@ctos-WHITLEY:~$ hostnamectl && free -h && lscpu && date
Static hostname: ctos-WHITLEY
Icon name: computer-desktop
Chassis: desktop
Machine ID: 83543c959df3416ab35727712c815f40
Boot ID: 3505da155f224e44b5efa95815bc17a4
Operating System: Ubuntu 18.04.6 LTS
Kernel: Linux 5.4.0-150-generic
Architecture: x86-64
total used free shared buff/cache available
Mem: 125G 10G 101G 24M 13G 113G
Swap: 2.0G 0B 2.0G
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 32
On-line CPU(s) list: 0-31
Thread(s) per core: 2
Core(s) per socket: 16
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 106
Model name: Intel(R) Xeon(R) Gold 6326 CPU @ 2.90GHz
Stepping: 6
CPU MHz: 884.276
CPU max MHz: 3500.0000
CPU min MHz: 800.0000
BogoMIPS: 5800.00
Virtualization: VT-x
L1d cache: 48K
L1i cache: 32K
L2 cache: 1280K
L3 cache: 24576K
NUMA node0 CPU(s): 0-31
2024年 07月 17日 星期三 23:16:17 CST
5、检查网络和端口开放情况:
ping + ip / ping + 网址:sudo apt install inetutils-ping
telnet ip + 空格 + 端口
案例:
tom@tom:~/tests$ sudo apt install inetutils-ping
Reading package lists... Done
Building dependency tree
Reading state information... Done
inetutils-ping is already the newest version (2:1.9.4-3ubuntu0.1).
The following packages were automatically installed and are no longer required:
efibootmgr libcap2-bin libefiboot1 libefivar1 libpam-cap
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 19 not upgraded.
tom@tom:~/tests$ ping www.baidu.com
PING www.baidu.com (157.148.69.80): 56 data bytes
64 bytes from 157.148.69.80: icmp_seq=0 ttl=53 time=94.081 ms
64 bytes from 157.148.69.80: icmp_seq=1 ttl=53 time=69.739 ms
64 bytes from 157.148.69.80: icmp_seq=2 ttl=53 time=14.773 ms
^C--- www.baidu.com ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 14.773/59.531/94.081/33.172 ms
# telnet
tom@tom:~/tests$ telnet 157.148.69.80 80
Trying 157.148.69.80...
Connected to 157.148.69.80.
Escape character is '^]'.
6、设置ssh免密:
案例:安全性考虑谨慎使用。
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub root@容器所在主机的IP -p 映射的端口
7、校验文件完整性MD5
debsums dpkg_pkg_name
rpm -V rpm_pkg_name
md5sum file
# 详见当天发布的案例攻略
8、快捷键
ctrl+a 光标定位到行首
ctrl+e 光标定位到行尾
ctrl+u 删除命令行开始到光标处
ctrl+k 删除光标至命令行结尾
shift+insert 复制剪贴板内容到shell
tab 命令补齐(提示)