Skip to main content

45 posts tagged with "linux"

View All Tags

· One min read

2台机器互传text

# A 起一个端口
nc -lk 5000
# B 链接 A
nc ip-a 5000

端口转发

windows

netsh interface portproxy add v4tov4 connectaddress=127.0.0.1 connectport=2375 listenport=2376 protocol=tcp

yum install -y nmap-ncat

ncat -l 8080 #

ncat --sh-exec "ncat localhost 80" -l 7777 --keep-open

· 3 min read

openssl

查看 cert

keytool -printcert -file ca.crt

# 普通
openssl genrsa -out private.key 2048
# password
openssl genrsa -passout pass:_passphrase_ -out private.key 2048

# 私钥 => 公钥
openssl rsa -in private.key -pubout -out public.key
# 私钥 => 公钥 password
openssl rsa -in private.key -passin 'pass:P@s5w0rD' -pubout -out public.key

## 私钥 => pfx
```shell
mima=_passphrase_
openssl req -new -key private.key -out ssl.csr # 证书请求文件
openssl x509 -req -days 3650 -in ssl.csr -signkey private.key -out ssl.crt # 证书文件
openssl pkcs12 -export -inkey private.key -in ssl.crt -out ssl.pfx

## pfx renew crt
openssl pkcs12 -in a.pfx -nocerts -out a.key -nodes
# copy crt to here
openssl pkcs12 -export -out b.pfx -inkey a.key -in b.crt # or b.pem

window pfx => https

openssl pkcs12 -in a.pfx -nodes -out a.pem -passin 'pass:GfosCn20!' openssl pkcs12 -in a.pfx -nocerts -out a.pem -nodes -passin 'pass:Sandy2020' # full chain openssl rsa -in a.pem -out a.key openssl x509 -in a.pem -out a.crt kubectl create secret tls ccm-https --key a.key --cert a.crt --namespace=gim-uat

end can

zx.sh

mima=$(cat zx.txt)
echo $mima

openssl pkcs12 -in zx.pfx -nokeys -passin "pass:${mima}" | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > a
openssl pkcs12 -in zx.pfx -nocerts -nodes -passin "pass:${mima}" | sed -ne '/-BEGIN PRIVATE KEY-/,/-END PRIVATE KEY-/p' > b

cat a | base64 -w 0 > aa
cat b | base64 -w 0 > bb

http auth

openssl passwd -crypt admin123 echo "admin:$(openssl passwd -crypt admin123)" > .espasswd


# 4要素
- cert.pem 服务端证书
- chain.pem 浏览器需要的所有证书但不包括服务端证书,比如根证书和中间证书
- fullchain.pem 包括了cert.pem和chain.pem的内容
- privkey.pem 证书的私钥

```sh
# cert
openssl pkcs12 -in a.pfx -nokeys -clcerts -out cert.pem -passin 'pass:GfosCn20!'
# chain
openssl pkcs12 -in a.pfx -nokeys -cacerts -out chain.pem -passin 'pass:GfosCn20!'
# fullchain
openssl pkcs12 -in a.pfx -nokeys -out fullchain.pem -passin 'pass:GfosCn20!'
# privkey
openssl pkcs12 -in a.pfx -nocerts -nodes -out privkey.pem -passin 'pass:GfosCn20!'

keytool

keytool -genkey -alias broker -keyalg RSA -keystore broker.ks
#导出经纪人的证书,以便可以与客户共享:
keytool -export -alias broker -keystore broker.ks -file broker_cert
#为客户端创建证书/密钥库:
keytool -genkey -alias client -keyalg RSA -keystore client.ks
#为客户端创建一个信任库,并导入代理的证书。这样可以确定客户“信任”经纪人:
keytool -import -alias broker -keystore client.ts -file broker_cert

openssl ca

# https://www.cnblogs.com/leffss/p/14705551.html
openssl genrsa -out ssl.key 2048
# openssl rsa -in ssl.key -des3 -out encrypted.key # 密码
openssl req -new -key ssl.key -out ssl.csr

# 自签
openssl x509 -req -in ssl.csr -signkey ssl.key -out ssl.crt

# ca 签
# openssl genrsa -out sign.key 2048
# openssl req -new -key sign.key -out sign.csr
openssl x509 -req -in sign.csr -extensions v3_ca -signkey sign.key -out sign.crt
openssl x509 -req -in ssl.csr -extensions v3_usr -CA sign.crt -CAkey sign.key -CAcreateserial -out ssl.crt

· One min read

supervisor

yum install -y supervisor

start

supervisord -c /etc/supervisord.conf

开机启动

systemctl enable supervisord
systemctl is-enabled supervisord # check

supervisord : 启动supervisor
supervisorctl reload :修改完配置文件后重新启动supervisor
supervisorctl status :查看supervisor监管的进程状态
supervisorctl start 进程名 :启动XXX进程
supervisorctl stop 进程名 :停止XXX进程
supervisorctl stop all:停止全部进程,注:start、restart、stop都不会载入最新的配置文件。
supervisorctl update:根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启

tail -f /var/log/supervisor/supervisord.log

cd /var/log/supervisor/

/etc/supervisord.d/kline.ini

[program:super_kline]
command=/gosrc/src/git.bitdata.com.cn/bitdata/ws/cmd/kafka/kline
autostart=true
autorestart=true
startsecs=10
stdout_logfile=/var/log/supervisor/super_kline.log
stderr_logfile=/var/log/supervisor/super_kline.log
stdout_logfile_backups=10
stderr_logfile_backups=10
stdout_logfile_maxbytes=10MB
stderr_logfile_maxbytes=10MB
stdout_capture_maxbytes=10MB
stderr_capture_maxbytes=10MB

· One min read

ffmpeg -i test.mov -vcodec libx264 -s 960x540 -preset fast -crf 22 -y -acodec copy test.mp4

  • 通过调节"22",可以调节清晰度,(这个取值0~50,一般取18到30左右合适。0是无损,50就看不清了)

old

./ffmpeg.exe -list_devices true -f dshow -i dummy

./ffmpeg.exe -f dshow -i video="screen-capture-recorder":audio="virtual-audio-capturer" ab.mp4
./ffmpeg.exe -f dshow -i video="screen-capture-recorder" yo.mp4
./ffmpeg.exe -f dshow -i audio="virtual-audio-capturer" yo.mp3


./ffmpeg.exe -f dshow -i video="screen-capture-recorder" -f dshow -i audio="virtual-audio-capturer" 222.avi



./ffmpeg.exe -list_devices true -f dshow -i test.mp4
./ffmpeg.exe -f x11grab -s 842x676 -r 50 -i :0.0+228,213 test.mp4
./ffmpeg.exe -vcodec mpeg4 -b 1000 -r 10 -g 300 test.avi

· One min read

Admin password

docker run --rm httpd:2.4-alpine htpasswd -nbB admin 12341234

admin:$2y$05$1VZxOFif9eR0Or4eE6aj7.a8ccXUtO7zadQKU6Mmm7AFnL3WhZ7Xq //有换行

or

echo "admin:$(openssl passwd -crypt admin123)" > .espasswd

ab install

apt-get install apache2-utils
yum install httpd-tools

ab -c 50 -n 1000 http://api.bitdata.inner/
ab -c 1000 -n 10000 http://mgb.appcoachs.test/api/other/country
Server Software:        nginx/1.12.2
Server Hostname: api.bitdata.inner
Server Port: 80

Document Path: /
Document Length: 191 bytes

Concurrency Level: 50
Time taken for tests: 19.608 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 661007 bytes
HTML transferred: 191000 bytes
Requests per second: 51.00 [#/sec] (mean)
Time per request: 980.379 [ms] (mean)
Time per request: 19.608 [ms] (mean, across all concurrent requests)
Transfer rate: 32.92 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 3 13.8 2 145
Processing: 48 953 835.3 741 10363
Waiting: 48 953 835.3 741 10363
Total: 50 957 835.5 743 10365

Percentage of the requests served within a certain time (ms)
50% 743
66% 1001
75% 1227
80% 1416
90% 1973
95% 2618
98% 3455
99% 3876
100% 10365 (longest request)

· 5 min read

support

thrift 1.0.0-dev

Building C++ Library ......... : no
Building C (GLib) Library .... : no
Building Java Library ........ : no
Building C# Library .......... : no
Building Python Library ...... : no
Building Ruby Library ........ : no
Building Haxe Library ........ : no
Building Haskell Library ..... : no
Building Perl Library ........ : no
Building PHP Library ......... : yes
Building Dart Library ........ : no
Building Erlang Library ...... : no
Building Go Library .......... : yes
Building D Library ........... : no
Building NodeJS Library ...... : yes
Building Lua Library ......... : no

demo1


git clone https://github.com/apache/thrift.git

./bootstrap.sh
./configure --with-boost=/usr/local

yum install -y libtool
yum install -y byacc #yacc: command not found

make
make install

go get git.apache.org/thrift.git/lib/go/thrift
新建项目目录testphp,然后把thrift/lib/php/lib复制到testphp目录下面

batu.thrift

namespace go batu.demo
namespace php batu.demo

/**
* 结构体定义
*/
struct Article{
1: i32 id,
2: string title,
3: string content,
4: string author,
}

const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'}

service batuThrift {
list<string> CallBack(1:i64 callTime, 2:string name, 3:map<string, string> paramMap),
void put(1: Article newArticle),
}

server.go

package main

import (
"fmt"
"os"
"time"

"./batu/demo" //注意导入Thrift生成的接口包
"git.apache.org/thrift.git/lib/go/thrift"
)

const (
NetworkAddr = "192.168.1.101:9090" //监听地址&端口
)

type batuThrift struct {
}

func (this *batuThrift) CallBack(callTime int64, name string, paramMap map[string]string) (r []string, err error) {
fmt.Println("-->from client Call:", time.Unix(callTime, 0).Format("2006-01-02 15:04:05"), name, paramMap)
r = append(r, "key:"+paramMap["a"]+" value:"+paramMap["b"])
return
}

func (this *batuThrift) Put(s *demo.Article) (err error) {
fmt.Printf("Article--->id: %d\tTitle:%s\tContent:%t\tAuthor:%d\n", s.ID, s.Title, s.Content, s.Author)
return nil
}

func main() {
transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
//protocolFactory := thrift.NewTCompactProtocolFactory()

serverTransport, err := thrift.NewTServerSocket(NetworkAddr)
if err != nil {
fmt.Println("Error!", err)
os.Exit(1)
}

handler := &batuThrift{}
processor := demo.NewBatuThriftProcessor(handler)

server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
fmt.Println("thrift server in", NetworkAddr)
server.Serve()
}

client.go

package main

import (
"fmt"
"net"
"os"
"strconv"
"time"

"./batu/demo"
"git.apache.org/thrift.git/lib/go/thrift"
)

const (
HOST = "127.0.0.1"
PORT = "9090"
)

func main() {
startTime := currentTimeMillis()

transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

transport, err := thrift.NewTSocket(net.JoinHostPort(HOST, PORT))
if err != nil {
fmt.Fprintln(os.Stderr, "error resolving address:", err)
os.Exit(1)
}

useTransport := transportFactory.GetTransport(transport)
client := demo.NewBatuThriftClientFactory(useTransport, protocolFactory)
if err := transport.Open(); err != nil {
fmt.Fprintln(os.Stderr, "Error opening socket to "+HOST+":"+PORT, " ", err)
os.Exit(1)
}
defer transport.Close()

for i := 0; i < 10; i++ {
paramMap := make(map[string]string)
paramMap["a"] = "batu.demo"
paramMap["b"] = "test" + strconv.Itoa(i+1)
r1, _ := client.CallBack(time.Now().Unix(), "go client", paramMap)
fmt.Println("GOClient Call->", r1)
}

model := demo.Article{1, "Go第一篇文章", "我在这里", "liuxinming"}
client.Put(&model)
endTime := currentTimeMillis()
fmt.Printf("本次调用用时:%d-%d=%d毫秒\n", endTime, startTime, (endTime - startTime))

}

func currentTimeMillis() int64 {
return time.Now().UnixNano() / 1000000
}

client.php

<?php
namespace batu\testDemo;
header("Content-type: text/html; charset=utf-8");
$startTime = getMillisecond();//记录开始时间

$ROOT_DIR = realpath(dirname(__FILE__) . '/');
$GEN_DIR = realpath(dirname(__FILE__) . '/') . '/gen-php';
require_once $ROOT_DIR . '/Thrift/ClassLoader/ThriftClassLoader.php';

use Thrift\ClassLoader\ThriftClassLoader;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TBufferedTransport;
use Thrift\Transport\TFramedTransport;
use Thrift\Transport\TSocket;
use Thrift\Transport\TSocketPool;

$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', $ROOT_DIR);
$loader->registerDefinition('batu\demo', $GEN_DIR);
$loader->register();

// $thriftHost = '127.0.0.1'; //UserServer接口服务器IP
$thriftHost = '192.168.1.101'; //UserServer接口服务器IP
$thriftPort = 9090; //UserServer端口

$socket = new TSocket($thriftHost, $thriftPort);
$socket->setSendTimeout(10000);#Sets the send timeout.
$socket->setRecvTimeout(20000);#Sets the receive timeout.
//$transport = new TBufferedTransport($socket); #传输方式:这个要和服务器使用的一致 [go提供后端服务,迭代10000次2.6 ~ 3s完成]
$transport = new TFramedTransport($socket); #传输方式:这个要和服务器使用的一致[go提供后端服务,迭代10000次1.9 ~ 2.1s完成,比TBuffer快了点]
$protocol = new TBinaryProtocol($transport); #传输格式:二进制格式
$client = new \batu\demo\batuThriftClient($protocol);# 构造客户端

$transport->open();
$socket->setDebug(TRUE);

for ($i = 1; $i < 11; $i++) {
$item = array();
$item["a"] = "batu.demo";
$item["b"] = "test" . $i;
$result = $client->CallBack(time(), "php client", $item); # 对服务器发起rpc调用
echo "PHPClient Call->" . implode('', $result) . "\n";
}

$s = new \batu\demo\Article();
$s->id = 1;
$s->title = '插入一篇测试文章';
$s->content = '我就是这篇文章内容';
$s->author = 'liuxinming';
$client->put($s);

$s->id = 2;
$s->title = '插入二篇测试文章';
$s->content = '我就是这篇文章内容';
$s->author = 'liuxinming';
$client->put($s);

$endTime = getMillisecond();

echo "本次调用用时: :" . $endTime . "-" . $startTime . "=" . ($endTime - $startTime) . "毫秒\n";

function getMillisecond() {
list($t1, $t2) = explode(' ', microtime());
return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
}

$transport->close();

· 4 min read

install

安装

下载 https://github.com/git/git/releases

sudo yum update

//安装依赖的包
sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker

wget https://github.com/git/git/archive/v2.9.0-rc1.tar.gz
tar -zxvf v2.9.0-rc1.tar.gz
cd git-2.9.0-rc1/
make prefix=/usr/local/git all
sudo make prefix=/usr/local/git install

sudo vim /etc/profile
export PATH=/usr/local/git/bin:$PATH
source /etc/profile

config user

git config --global user.email "825407762@qq.com"
git config --global user.name "wolanx"
git config --global credential.helper store

config buffer

git remote add origin https://git.oschina.net/zhaoyujie/www.825407762.com.git
git push -u origin master

git checkout --orphan gh-pages
git push origin gh-pages

# buffer
git config --list
git config --global http.postBuffer 524288000
git config http.postBuffer 524288000

权限

git update-index --chmod=+x gradlew

submodule

git submodule add -f -b main git@github.com:wolanx/jii.git
git submodule foreach git pull origin main

# 已有git的初始
git submodule init
git submodule update

multi key

Host ssh.dev.azure.com
HostName ssh.dev.azure.com
User git
IdentityFile C:\Users\admin\.ssh\id_azure
IdentitiesOnly yes

常用 cmd

git --no-pager log --graph --color=never --pretty=format:"%h %cr %an %s" -n20
git config alias.logs lg --color --graph --pretty=format:'%C(bold white)%h%Creset -%C(bold green)%d%Creset %s %C(bold green)(%cr)%Creset %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
git config alias.logs lg
git --no-pager lg --color=never

gist

# 获取 gitid
git rev-parse HEAD
git rev-parse --short HEAD

empty 完全覆盖

# 当前分支
git reset --hard origin/paytest
git push -f

hook 钩子

.git/hooks/commit-msg

#!/bin/sh
cd backend-gim

black . --check
if [ $? -ne 0 ]; then
echo
echo "black . --check failed"
echo
exit 1
fi

lfs 大文件

代码统计

find . -name "*.js" -or -name "*.css" | xargs grep -v "^$" | wc -l

git log --author="zhaoyujie" --since="2016-12-10" --pretty=tformat: --numstat -- ./controllers ./library/service | gawk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END { printf "add %s remove %s total %s\n",add,subs,loc }'
git log --author="zhaoyujie" --since="2016-12-10" --pretty=tformat: --numstat -- ./controllers ./library/service | awk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END { printf "add %s remove %s total %s\n",add,subs,loc }'

// 代码数量统计
git ls-files | xargs wc -l
git log --author="zhaoyujie" --pretty=tformat: --numstat -- ./app/assets/less ./app/views ./app/components | gawk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END { printf "add %s remove %s total %s zhaoyujie\n",add,subs,loc }'

代码统计 cloc

# https://github.com/AlDanial/cloc
PS C:\Users\106006\Desktop\www\gimc> cloc --vcs=git
1699 text files.
1630 unique files.
85 files ignored.

github.com/AlDanial/cloc v 2.00 T=17.38 s (93.8 files/s, 16301.6 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
JavaScript 670 10314 9544 150549
Python 426 10582 9395 52535
LESS 90 2228 670 11398
SQL 20 444 78 6757
JSON 5 0 0 6281
YAML 38 6 105 4022
SVG 318 49 219 3460
HTML 11 107 6 632
INI 1 16 0 564
PO File 2 257 366 456
Jupyter Notebook 6 0 797 350
Markdown 8 33 0 181
Lua 4 28 18 140
GraphQL 5 8 0 95
CSS 2 7 0 72
TypeScript 10 0 0 70
Dockerfile 3 33 20 68
Text 2 3 0 54
XML 1 0 0 50
Properties 2 16 72 43
Bourne Shell 4 10 8 37
TOML 1 0 1 21
make 1 4 0 12
-------------------------------------------------------------------------------
SUM: 1630 24145 21299 237847
-------------------------------------------------------------------------------

· One min read

doc

cat /usr/local/etc/goaccess.conf

time-format %T
date-format %d/%b/%Y
log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"

cat 1.sh

#!/bin/bash
fname=$1
echo $fname
cd /alidata/log/nginx/access/
goaccess -f $fname -a > /alidata/www/log/index.html
goaccess -a -f /pcmoto/log/nginx/www_access.log -p /etc/goaccess.conf > /pcmoto/web/test/index.html
Overall
Unique visitors
Requested files
Requested static files
Not found URLs
Hosts
Operating Systems
Browsers
Time Distribution
Referrers URLs
Referring sites
Status codes

· 3 min read
# 例1:抓取页面到指定文件,如果有乱码可以使用iconv转码
curl -o baidu.html www.baidu.com
curl –s –o baidu.html www.baidu.com |iconv -f utf-8 #减少输出信息
# 例2:模拟浏览器头(user-agent)
curl -A "Mozilla/4.0 (compatible;MSIE 6.0; Windows NT 5.0)" www.baidu.com
# 例3:处理重定向页面
curl –L http://192.168.1.100/301.php #默认curl是不处理重定向
# 例4:模拟用户登陆,保存cookie信息到cookies.txt文件,再使用cookie登陆
curl -c ./cookies.txt -F NAME=user -F PWD=***URL #NAME和PWD是表单属性不同,每个网站基本都不同
curl -b ./cookies.txt –o URL
# 例5:获取HTTP响应头headers
curl -I http://www.baidu.com
curl -D ./header.txt http://www.baidu.com #将headers保存到文件中
# 例6:访问HTTP认证页面
curl –u user:pass URL
#例7:通过ftp上传和下载文件
curl -T filename ftp://user:pass@ip/docs #上传
curl -O ftp://user:pass@ip/filename #下载
# 忽略 tls
curl --insecure https://10.10.18.158:6443

手动hosts

curl --resolve www.bitfinex.com:443:104.16.174.181 https://www.bitfinex.com curl --resolve www.accountchooser.com:443:216.239.38.10 https://www.accountchooser.com

时间分析

curl -o /dev/null -w %{time_namelookup}::%{time_connect}::%{time_starttransfer}::%{time_total}::%{speed_download}"\n" "http://www.taobao.com"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 28774 0 28774 0 0 1145k 0 --:--:-- --:--:-- --:--:-- 7550k
0.014::0.016::0.020::0.025::1173060.000

-o:把curl 返回的html、js 写到垃圾回收站[ /dev/null]
-s:去掉所有状态
-w:按照后面的格式写出rt
time_namelookup:DNS 解析域名[www.taobao.com]的时间
time_commect:client和server端建立TCP 连接的时间
time_starttransfer:从client发出请求;到web的server 响应第一个字节的时间
time_total:client发出请求;到web的server发送会所有的相应数据的时间
speed_download:下周速度 单位 byte/s

上面时间rt的理解【单位是 秒】
0.014: DNS 服务器解析www.taobao.com 的时间单位是s
0.015: client发出请求,到c/s 建立TCP 的时间;里面包括DNS解析的时间
0.018: client发出请求;到s响应发出第一个字节开始的时间;包括前面的2个时间
0.019: client发出请求;到s把响应的数据全部发送给client;并关闭connect的时间
1516256.00 :下周数据的速度

建立TCP连接到server返回client第一个字节的时间:0.018s - 0.015s = 0.003s
server把响应数据发送给client的时间:0.019s - 0.018 = 0.01s

-w

curl -w "
TCP handshake: %{time_connect}
SSL handshake: %{time_appconnect}
SSL handshake: %{time_appconnect}
time_total,%{time_total}
" -so /dev/null https://www.alipay.com

curl -w "
time_appconnect,%{time_appconnect},SSL handshake
time_connect,%{time_connect},TCP handshake
time_namelookup,%{time_namelookup}
time_pretransfer,%{time_pretransfer}
time_starttransfer,%{time_starttransfer}
time_redirect,%{time_redirect}
time_total,%{time_total}
url_effective,%{url_effective}
content_type,%{content_type}
local_ip,%{local_ip}
remote_ip,%{remote_ip}
speed_download,%{speed_download}
" -so /dev/null https://api.bitdata.com.cn/ | column -s',' -t

· 14 min read

类似man 查看命令用法

curl cht.sh/ls

用户权限

adduser www
passwd www # 修改密码
#修改 /etc/sudoers 文件,找到下面一行,在root下面添加一行,如下所示:
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
www ALL=(ALL) ALL
#修改完毕,现在可以用www帐号登录,然后用命令 sudo – ,即可获得root权限进行操作

some

ip 查看

netstat -nltp
iptables -L
lsof -i:80
lsof -n -P -i TCP -s TCP:LISTEN

netstat

yum install -y net-tools
netstat -nltp # 查看 ip

netstat -nap | grep 进程pid
netstat -nap | grep 端口号

chkconfig

chkconfig --list #列出所有的系统服务 chkconfig --list mysqld #列出mysqld服务设置情况 chkconfig --add httpd #增加httpd服务 chkconfig --del httpd #删除httpd服务 /etc/rc[0-6].d删除 chkconfig --level httpd 2345 on #设置httpd在运行级别为2、3、4、5的情况下都是on(开启)的状态 chkconfig --level 35 mysqld on #设定mysqld在等级3和5为开机运行服务,--level 35表示操作只在等级3和5执行,on表示启动,off表示关闭 chkconfig mysqld on #设定mysqld在各等级为on,“各等级”包括2、3、4、5等级

wget

#1:下载单个文件到当前目录下,也可以-P指定下载目录
wget http://nginx.org/download/nginx-1.8.0.tar.gz
#2:对于网络不稳定的用户可以使用-c和--tries参数,保证下载完成
wget --tries=20 -c http://nginx.org/download/nginx-1.8.0.tar.gz
#3:下载大的文件时,我们可以放到后台去下载,这时会生成wget-log文件来保存下载进度
wget -b http://nginx.org/download/nginx-1.8.0.tar.gz
#4:可以利用—spider参数判断网址是否有效
wget --spider http://nginx.org/download/nginx-1.8.0.tar.gz
#5:自动从多个链接下载文件
cat url_list.txt #先创建一个URL文件
http://nginx.org/download/nginx-1.8.0.tar.gz
http://nginx.org/download/nginx-1.6.3.tar.gz
wget -i url_list.txt
#6:限制下载速度
wget --limit-rate=1m http://nginx.org/download/nginx-1.8.0.tar.gz
#7:登陆ftp下载文件
wget --ftp-user=user --ftp-password=pass ftp://ip/filename

find xargs

find . -name ".py" | xargs -I name sh -c 'echo name ; cat name' find . -name ".py" | xargs -I name sh -c 'echo "\"\"\"@copyright {name}\"\"\"" ; cat name ; echo -e "\n\n\n\n"' > ~ /Desktop/a.txt find . -name "*.js" | xargs -I name sh -c 'echo "\"\"\"@copyright {name}\"\"\"" ; cat name ; echo -e "\n\n\n\n"' > ~ /Desktop/b.txt

iftop 图看io

https://www.vpser.net/manage/iftop.html
yum install -y iftop

iftop -m 5m

TX: 发送流量
RX: 接收流量
TOTAL: 总流量
Cumm: 运行iftop到目前时间的总流量
peak: 流量峰值
rates: 分别表示过去 2s 10s 40s 的平均流量

-P使host信息及端口信息默认就都显示;
-m设置界面最上边的刻度的最大值,刻度分五个大段显示,例:#
Host display: General:
n - toggle DNS host resolution P - pause display
s - toggle show source host h - toggle this help display
d - toggle show destination host b - toggle bar graph display
t - cycle line display mode B - cycle bar graph average
T - toggle cumulative line totals
Port display: j/k - scroll display
N - toggle service resolution f - edit filter code
S - toggle show source port l - set screen filter
D - toggle show destination port L - lin/log scales
p - toggle port display ! - shell command
q - quit
Sorting:
1/2/3 - sort by 1st/2nd/3rd column
< - sort by source name
> - sort by dest name
o - freeze current order

iotop http://man.linuxde.net/iotop

yum install -y iotop
iotop -d 5

常用快捷键:
左右箭头:改变排序方式,默认是按IO排序。
r:改变排序顺序。
o:只显示有IO输出的进程。
p:进程/线程的显示方式的切换。
a:显示累积使用量。

firewall

firewall-cmd --state
systemctl disable firewalld.service # 在开机时禁用一个服务

iostat

yum install -y sysstat

iostat -d -k 1 10 #查看TPS和吞吐量信息(磁盘读写速度单位为KB)
iostat -d -m 2 #查看TPS和吞吐量信息(磁盘读写速度单位为MB)
iostat -d -x -m 1 10 #查看设备使用率(%util)

参数
-d 表示,显示设备(磁盘)使用状态;-k某些使用block为单位的列强制使用Kilobytes为单位;2表示,数据显示每隔2秒刷新一次。
-x,该选项将用于显示和io相关的扩展数据。

rrqm/s:每秒这个设备相关的读取请求有多少被Merge了(当系统调用需要读取数据的时候,VFS将请求发到各个FS,如果FS发现不同的读取请求读取的是相同Block的数据,FS会将这个请求合并Merge);wrqm/s:每秒这个设备相关的写入请求有多少被Merge了。
rsec/s:每秒读取的扇区数;
wsec/:每秒写入的扇区数。
rKB/s:The number of read requests that were issued to the device per second;
wKB/s:The number of write requests that were issued to the device per second;
avgrq-sz 平均请求扇区的大小
avgqu-sz 是平均请求队列的长度。毫无疑问,队列长度越短越好。
await: 每一个IO请求的处理的平均时间(单位是微秒毫秒)。这里可以理解为IO的响应时间,一般地系统IO响应时间应该低于5ms,如果大于10ms就比较大了。
这个时间包括了队列时间和服务时间,也就是说,一般情况下,await大于svctm,它们的差值越小,则说明队列时间越短,反之差值越大,队列时间越长,说明系统出了问题。
svctm 表示平均每次设备I/O操作的服务时间(以毫秒为单位)。如果svctm的值与await很接近,表示几乎没有I/O等待,磁盘性能很好,如果await的值远高于svctm的值,则表示I/O队列等待太长, 系统上运行的应用程序将变慢。
%util: 在统计时间内所有处理IO时间,除以总共统计时间。例如,如果统计间隔1秒,该设备有0.8秒在处理IO,而0.2秒闲置,那么该设备的%util = 0.8/1 = 80%,所以该参数暗示了设备的繁忙程度
。一般地,如果该参数是100%表示设备已经接近满负荷运行了(当然如果是多磁盘,即使%util是100%,因为磁盘的并发能力,所以磁盘使用未必就到了瓶颈)。

sed

http://man.linuxde.net/sed

sed -i 's/book/books/g' file

# 删除#开头行
sed -i '/^#/'d filebeat.yml
# 删除空白行
sed -i /^[[:space:]]*$/d filebeat.yml

cat

cat <<EOF
cat > filename # 敲完内容,ctrl+d
cat > filename <<EOF
敲完内容
EOF

tee

读取标准输入的数据,并将其内容输出成文件

tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://cmf3ratl.mirror.aliyuncs.com"]
}
EOF

[root@localhost ~]# who | tee who.out
root pts/0 2009-02-17 07:47 (123.123.123.123)
[root@localhost ~]# cat who.out
root pts/0 2009-02-17 07:47 (123.123.123.123)
[root@localhost ~]# pwd | tee -a who.out
/root
[root@localhost ~]# cat who.out
root pts/0 2009-02-17 07:47 (123.123.123.123)
/root
[root@localhost ~]#

rename

rename 's/\.jpeg$/\.jpg/' *.jpeg
rename 's/_\w*/_/' *.mp3

split文件分割

split -l 2000 data.mt -d seofile_
curl -H 'Content-Type:text/plain' --data-binary @seofile_00 "http://data.zz.baidu.com/urls?site=www.app-echo.com&token=3iyzwDoYB6IQAMKL"

//每3行拆分成一个文件,拆分后的文件名以name开头,以数字作为后缀后缀长度为1
split -l 3 test -d -a 1 name

//每三个字节拆分成一个文件,默认不加单位就是字节,也可以带单位比如KB,MB等
split -b 3 test -d -a 1 new

redis key分布

# 1.sh
for i in `seq 1 100`;do
redis-cli -h 230f029eefb64e44.m.cnsha.kvstore.aliyuncs.com -a zc1BK5JM randomkey
done

./1.sh > r.keys

cat r.keys | awk -F ':' '{print $1}' | sort | uniq -c | sort -r -n

text len 排序

awk '{ print length, $0}' bj.conf | sort -n | sed 's/.* //'

grep

-i  忽略大小写
-n  显示行号
--color   高亮关键字,centos7默认已经高亮
-c  统计符合条件的行数
-o  只打印关键字,每个被匹配的关键字单独显示一行
-B   同时显示之前的行,后面必须有数字,如 -B2
-A   同时显示之后的行
-w  只匹配独立单词,也就是精确匹配
-v  反向查找
-e  同时匹配多个目标
-q  静默模式,只关心有没有匹配到,不关心内容
-E  可以使用扩展正则,,相当于egrep
-P  使用兼容perl的正则
fgrep: 不支持正则表达式,只能匹配写死的字符串,但是速度奇快,效率高,fastgrep

tail -f api::header-debug-`date +"%w"`.log | grep ' {.*}' -o --line-buffered | jq

进程

#查看端口情况
netstat -apn | grep 9000 # ss better
ps -aux | grep php

scp

#文件
scp goaccess.conf 139:196:14:10:/usr/local/etc/goaccess.conf
#文件夹
scp -qr -P 20248 name1 root@172.16.3.121:/data/name2/

文件大小

du -sh du -sh *

文件个数

ls -l | wc -l

解压

zip

zip -r a.zip dirname
zip a.zip tests/* -x tests/ln.log #排除一个文件
zip -m a.zip test.md #向压缩文件中a.zip中添加test.md文件

unzip

unzip -r a.zip *
unzip -n test.zip -d /tmp #在指定目录/tmp解压缩,如已存在,-n 不覆盖 ~ -o 覆盖

tar

tar -zcvf filename.tar.gz folderName #压缩
tar -zcvf FileName.tar.gz DirName --exclude DirName/DirExcName #排除
tar -zcvf FileName.tar.gz DirName --exclude DirName/Dir1Name --exclude DirName/Dir2Name #多个排除

tar -zxvf filename.tar.gz -C /pcmoto/club/ #解压
tar -zxvf filename.tar.gz -C .

seq

seq -f "%04g" 5 4 1000 | xargs -n 15

completion

yum -y install bash-completion iot-echo completion bash > /etc/bash_completion.d/iot-echo.bash source /etc/bash_completion.d/iot-echo.bash

cpu

查看各cpu状况,top 然后按1

查看物理CPU的个数
#cat /proc/cpuinfo |grep &quot;physical id&quot;|sort |uniq|wc -l

查看逻辑CPU的个数
#cat /proc/cpuinfo |grep &quot;processor&quot;|wc -l

查看CPU是几核
#cat /proc/cpuinfo |grep &quot;cores&quot;|uniq

查看CPU的主频
#cat /proc/cpuinfo |grep MHz|uniq

### uname -a
Linux euis1 2.6.9-55.ELsmp #1 SMP Fri Apr 20 17:03:35 EDT 2007 i686 i686 i386 GNU/Linux
(查看当前操作系统内核信息)

### cat /etc/issue | grep Linux
Red Hat Enterprise Linux AS release 4 (Nahant Update 5)
(查看当前操作系统发行版信息)

### cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
8 Intel(R) Xeon(R) CPU E5410 @ 2.33GHz
(看到有8个逻辑CPU, 也知道了CPU型号)

### cat /proc/cpuinfo | grep physical | uniq -c
4 physical id : 0
4 physical id : 1
(说明实际上是两颗4核的CPU)

### getconf LONG_BIT
32
(说明当前CPU运行在32bit模式下, 但不代表CPU不支持64bit)

### cat /proc/cpuinfo | grep flags | grep &#39; lm &#39; | wc -l
8
(结果大于0, 说明支持64bit计算. lm指long mode, 支持lm则是64bit)

如何获得CPU的详细信息:
linux命令:cat /proc/cpuinfo
用命令判断几个物理CPU,几个核等:
逻辑CPU个数:
### cat /proc/cpuinfo | grep &quot;processor&quot; | wc -l
物理CPU个数:
### cat /proc/cpuinfo | grep &quot;physical id&quot; | sort | uniq | wc -l
每个物理CPU中Core的个数:
### cat /proc/cpuinfo | grep &quot;cpu cores&quot; | wc -l
是否为超线程?
如果有两个逻辑CPU具有相同的”core id”,那么超线程是打开的。
每个物理CPU中逻辑CPU(可能是core, threads或both)的个数:
### cat /proc/cpuinfo | grep &quot;siblings&quot;

column

cat  $(find . -name 310.html) | column -s',' -t

hostname

hostname m201
hostnamectl set-hostname m201
cat /proc/sys/kernel/hostname

tcpdump

# 端口
tcpdump -i eth0 port 61617 -w ~/tcpdump_61617.pcap

sudo tcpdump 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)' -A -nn
所以常用的选项也就这几个:
tcpdump -D
tcpdump -c num -i int -nn -XX -vvv