运维中的有趣事件

章节一

运维道路上的些许指令
1.端口测试

telnet IP 端口
root@519fa7d1930d:~# telnet qq.com 80
Trying 58.250.137.36...
Connected to qq.com.
Escape character is '^]'.
nc -vzw 2 ip 端口
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 192.168.29.7:6611.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.

2.sed替换

sed -ri 's/需要替换的变量/替换成这个变量/g' 文件名

3.Centos解决tab问题

先查找:
rpm -qa bash-completion

yum查找一下:
yum list | grep bash

安装:
yum -y install bash-completion

4.解决netstat命令没有的问题

yum install net-tools

netstat -ntulp

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      20708/php-fpm: mast 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      887/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1118/master         

5.清除Linux的buff/cache

首先执行:sync

在执行:echo 3 > /proc/sys/vm/drop_caches

6.查看io性能

iostat -x 1 999 -m

7.查看nload

nload

yum -y install nload

apt -y install nload

8.查看显卡

nvidia-smi

9.查看电源

ipmitool sdr type "power supply"

10.Linux查看实时网速

方式一:
sar -n DEV 1 100 

1代表一秒统计并显示一次
100代表统计一百次

方式二:
准备两台服务器,安装iperf
第一台:iperf -s
第二台:iperf -c 第一台的IP

方式三:
yum  -y install mtr

mtr  -r ip

[root@localhost ansible]# mtr -r 192.168.164.129
Start: Thu Mar 25 23:21:11 2021
HOST: localhost.localdomain       Loss%   Snt   Last   Avg  Best  Wrst StDev
  1.|-- 192.168.164.129            0.0%    10    0.8   0.6   0.3   1.3   0.0


$1:IP地址和域名
$2:节点丢包率
$3:每秒发送数据包,默认值是10,可加-c指定
$4:最近一次探测延迟值
$5\6\7:分别是探测延时的平均值、最小值和最大值
$8:标准偏差,值越大说明相应节点越不稳定

11.查看RAID信息

df -h

cat /proc/mdstat
Personalities : [raid0] [linear] [multipath] [raid1] [raid6] [raid5] [raid4] [raid10] 
md0 : active raid0 nvme7n1[7] nvme6n1[6] nvme5n1[5] nvme4n1[4] nvme3n1[3] nvme2n1[2] nvme1n1[1] nvme0n1[0]
      16002129920 blocks super 1.2 512k chunks
      
unused devices: <none>

12.强制取消挂载

fusermount -uz /storage-3-43

13.将文件中的列转换成行,并用空格隔开

cat test.txt
192.168.1.1
192.168.1.2

cat test.txt | xargs | tr ' ' ' '
192.168.1.1 192.168.1.2

重新导入到文件:
cat test.txt | xargs | tr ' ' ' ' >> test1.txt

cat test1.txt
192.168.1.1 192.168.1.2


将行转为列

cat name | xargs  -n1
root@Enami:/home/test# cat name |xargs -n1
丹东MJ
二人MJ
福建屏南
广东JPH

14.查找根下大于1G的文件

find  / -size +1G

15.查看Ubuntu驱动版本

cat /proc/driver/nvidia/version

16.文件去重
链接:https://cloud.tencent.com/developer/article/1613208

cat file.txt | sort | uniq >> xxx.txt

取出两个文件的并集(重复的行只保留一份)
cat file1  file2  | sort | uniq >> file3

取出两个文件的交集(只留下同时存在于两个文件中的文件)
cat file1  file2  | sort -d >> file3

删除交集,留下其他行
cat file1  file2 | sort | uniq -u >> file3

两个文件合并(上下)
cat file1  file2 > file3
两个文件合并(左右)
paste file1 file2 > file3

一个文件去掉重复的行
sort file | uniq
注意:重复的多行记为一行,也就是说这些重复的行还在,只是全部省略为一行!
sort file | uniq –u
上面的命令可以把重复的行全部去掉,也就是文件中的非重复行!

17.查看操作系统

centos:
cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core)

ubuntu:
lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04.1 LTS
Release:	20.04
Codename:	focal

18.删除vim编辑器光标所在行之前的所有内容

d1G

19.查看目录以时间排序

ls -lrt

20.删除未知文件

rm -f --  文件名

21.查看磁盘信息

fdisk -l


查看CPU拓扑
hwloc-ls  

22.时间

`date +%F`  #打印当前年月

cp/mv/mkdir/touch  `date +%F`_file.txt

2021-03-15_file.txt

23.关于rsync

[ root@backup-server-2 ~ ] #crontab -l
* * */1 * * rsync -avpog  root@IP:/home/ /home/

24.grep查找

取后10行
grep xx xxx.log | tail -10

显示前5行
head -n  5 head.txt

显示前5字节
head -c 5 head.txt

章节二:

1:查看网卡的实时流量

历史查看
sar -n DEV -f /var/log/sa/saxx              #查看xx日的网卡流量历史

实时查看:
sar -n DEV 1 5 (每隔1秒钟刷新一次,共5次)

2:如何查看当前系统每个IP的连接数

netstat -n|awk'/^tcp/{print$5}'|awk-F: '{print$1}'|sort|uniq-c