-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.sh
executable file
·65 lines (54 loc) · 1.66 KB
/
tool.sh
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
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
set -ueo pipefail
function log_info() { echo -e "$(date +'%Y-%m-%d %H:%M:%S %z') $@" > /dev/tty; }
function log_erro() { echo -e "$(date +'%Y-%m-%d %H:%M:%S %z') $@" > /dev/tty; exit -1; }
function do_lastlog() {
lastlog $@ | awk '
NR > 1 && NF > 6 {
user = $1
time = $(NF-5) FS $(NF-4) FS $(NF-3) FS $(NF-2) FS $(NF-1) FS $NF
cmd = "date -d\""time"\" +\"%Y-%m-%d %H:%M:%S %z\""
cmd | getline time
close(cmd)
printf("%s => %s\n", time, user)
}' | sort
}
function do_ps() {
: ${1:?use $0 pattern}
argv=
for key in $@; do
[[ "$argv" != "" ]] && argv=${argv},
argv=$argv$(pgrep -d, $key || log_erro "not find $key")
done
ps -o etimes,ruser:10,pid,nlwp,cmd --sort=-etime --no-headers -p $argv | awk '
{
$1 = strftime("%Y-%m-%d %H:%M:%S %z", systime() - $1)
cmd = ""
for (i = 5; i <= NF; ++i)
cmd=cmd" "$i
printf("%s %10s %6s %4s => %s\n", $1, $2, $3, $4, cmd)
}'
}
function do_lsnet() {
sudo ss -Htlnp | \
awk '{print $4, $6}' | \
awk '{
sub(/users.*pid=/, "");
sub(/,fd.*/, "");
sub(/.*:/, "");
print
}' | sort -n | uniq | awk '{
cmd = "ps -o pid,user,comm --no-headers -p " $2;
cmd | getline detail;
close(cmd);
print $1, detail }' | column -t
}
function do_test() {
g++ -std=c++14 $@ && ./a.out
}
begin_time=$(date +%s)
cmd=$1
shift
do_$cmd $@
end_time=$(date +%s)
echo "took: $(( end_time - begin_time ))"