我一般用 tcpdump 有两种用法,一个是存盘,拿 wireshark 看,那么的话,就这么写
tcpdump -s 0 -i any -p udp and src 10.170.7.40 -w `date +%s`.pcap
或者实时打印到屏幕,就参考这里,http://www.askbjoernhansen.com…,这么写
I always forget the parameters for this and have to look them up in the …… 阅读全文
Tag Archives: Linux
linux ss
ss 是一个类似 netstat 的网络连接查看工具,man page 是这么说的
ss – another utility to investigate sockets
而 netstat 的 man page 中也有说到
This program is obsolete. Replacement for netstat is ss.
常用命令有
-s 参数,打印 summary
# ss -s
Total: 1596 (kernel 1890)
TCP: 7587 (estab …… 阅读全文
获取当前机器 IP
std::string GetLocalIP()
{
struct ifreq req;
int sock_fd;
char szLocalIP[16]= {0};
string strLocalIP="";
sock_fd = socket(AF_INET,SOCK_STREAM,0);
if(sock_fd < 0)
return strLocalIP;
snprint…… 阅读全文
清除旧日志脚本
bash 的语法真是反人类啊,反人类啊啊啊
#!/bin/bash
# created: 2015-10-10 13:24:15
size=204800
echo "loop delete oldest log file until directory size < "$size" KB"
while [ 1 ]; do
directory_size=$(du -s | cut -f1)
if [ "$size" -gt "$dire…… 阅读全文
unix domain socket dgram
大体上参考这里,http://philherlihy.com/unix-domain-sockets-datagram/
改动之后如下
服务端
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <…… 阅读全文
Linux 调用 system 函数的注意事项
来看这个代码
#include <stdio.h>
#include <stdlib.h>
int main() {
system("sleep 5 && touch test.txt");
FILE *fp = fopen("test.txt", "r");
if (!fp) {
printf("%m\n");
return -1;
}
fclose(fp);…… 阅读全文
动态链接库中的全局变量
想给自己之前的日志库,加一个染色的功能
gcc 有一个自己的特性,叫 __thread,介绍可以看这里,https://gcc.gnu.org/onlinedocs…,是可以用来修饰一个全局变量,从而让这个全局变量在每个线程都有一份拷贝,例子如下:
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#inc…… 阅读全文
分享一个多线程的日志库
思路部分参考了陈硕的 muduo,主要就是多线程写的安全性,以及一些日志分级,自动分割等常用功能,线上几十近百台机器一两年的运行没有发现什么问题
/*
* encoding: gbk
* created: 2013-09-05 10:33:59
*
* 日志工具
* 工具在初始化的时候从堆上分配一片内存,当作二维 char 数组来用
* 每个写入线程在写…… 阅读全文
Linux bash explode string
想要对一个很长的行根据分隔符断开,搜了一下,这次万能的 stackoverflow 居然没有合适的答案,于是只能自己搞,想了想,其实很简单,这么写就可以了
head part-00079 -n 1 | sed "s/\t/\r\n/g"
其实就是 sed,这个思路可以自由发挥的空间很大,想实现什么效果都简单
阅读全文
Linux 交换分区耗尽
今天在跑自己的业务进程的时候发现很卡,看了一下这个 free 命令的输出
$ free -m
total used free shared buffers cached
Mem: 7972 727 7244 0 0 420
-/+ buffers/cache: 306 7666
Swap: 1913 …… 阅读全文