2019年3月26日 星期二

Linux 常用指令

Process 管理

//列出系統上最耗費記憶體的程式
//-e 輸出所有行程的資訊
//-o 指定輸出欄位
//--sort 排序
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

//取當前pid
echo $$

//process 資源限制
ls /proc/{pid}/fd
cat /proc/{pid}/limits

//開檔狀況
ls -l /proc/{pid}/fd | less

//總開檔數
ls -l /proc/{pid}/fd | wc -l

網路相關

//查看80是哪個procese監聽的
netstat -tlnp | grep :22

tcp    0  0 0.0.0.0:22  0.0.0.0:*  LISTEN   1444/sshd
tcp6   0  0 :::22       :::*       LISTEN   1444/sshd

User 管理

sudo -i 切換為root

ssh 管理

//scp
scp "fileName" ubuntu@xxx.xx.xx.xx:/home/ubuntu/
scp -i "keyfile" filename aa@xxx.xxx.xxx.xxx:/home/aa/
sshpass -p "pass" scp "fileName" ubuntu@xxx.xx.xx.xx:/home/ubuntu/

//指定port P大寫
scp -P port root@xxx.xxx.xxx:/root/abc.jpg abc.jpc 
scp -P port abc.jpg root@xxx.xxx.xxx:/root/abc.jpg

//指定key
ssh -i 'keyfile' aa@xxx.xxx.xxx.xxx

//設定ssh timeout 時間
vim /etc/ssh/sshd_config

//找到ClientAliveInterval 参数,如果没有就自己加一行。
//client alive 檢查,秒
ClientAliveInterval=600 

//client alive 檢查次數 
ClientAliveCountMax=12

//依據以上設定,就是600*12=7200秒都沒反應就斷線
//開啟root ssh login
sudo passwd root    //更改root密碼
sudo vi /etc/ssh/sshd_config
PermitRootLogin yes
PasswordAuthentication yes

//centos 
service sshd restart
systemctl restart sshd

//debain
service ssh restart
systemctl restart sshd

ssh agent

1. Start ssh-agent in the background.
 > Agent pid xxxxx
2. Add ssh key to agent
  $ ssh-add -K pathToPrivateKey
  (-K 只限mac 內建的ssh-add, 表示存 passphase in your keychain)

3. if you key with passphase, agent will ask you to input pass.

//看ssh-agent執行狀況
看變數,是否有輸出
echo $SSH_AUTH_SOCK
echo $SSH_AGENT_PID

//in shell script
//自動加入所有預設rsa的key
if [ -z "$SSH_AUTH_SOCK" ] ; then
  eval `ssh-agent -K`
  ssh-add
fi

//kill ssh-agent
ssh-agent -k

github 使用多個 ssh key 與帳號

Host   連線至遠端的別稱(可以隨便填)
HostName   遠端的Domain或IP
Port   遠端的SSH連接埠(為預設的話可以不填)
User   使用者名稱
PreferredAuthentications   偏好使用的驗證(這邊的值都為publickey)
IdentityFile   私鑰的位置(可為相對或絕對位置,但建議使用絕對位置)

//edit ~/.ssh/config

Host gh.aaa.my
 HostName github.com
 User Hi_AAA
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/id_rsa_aaa_github

Host gh.bbb.company
 HostName github.com
 User Hi_BBB
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/id_rsa_bbb_github

Host bb.aaa.my
 HostName bitbucket.org
 User Yo_AAA
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/id_rsa_aaa_bitbucket

原本 repo 位置會改變
原本 git@github.com:Hi_AAA/my_project.git  
新的 git@gh.aaa.my:Hi_AAA/my_project.git

Domain的部分可以使用設定檔的 Host 名稱取代

//key 加入 ssh-agent 管理
ssh-add 

apt


apt-cache policy mysql-server       列出mysql所有版本
apt-get install mysql-server=5.6.29 安裝mysql指定版本

2019年3月4日 星期一

網路 http

NotFalse 技術客詳細說明

HTTP

1.0 /1.1 /2.0

主要特色

簡單:request - response
不限傳輸資料內容(content-type定義)
無連接:一個request一個連接
無狀態

URI 與URL 參考

URI (Uniform Resource Identifier)統一資源識別符

web上的資源,例如檔案、圖片、程式,都是用URI定義的。
能定位到資源的就是URI,例如:
localhost:80/index.html
localhost:80/user/save

URL(Uniform Resource Location)統一資源定位符

URL是URI的子集。
URL是路徑,URI是實際資源。
URI:localhost:80/user/save
URL:localhost:80/user

Message

HTTP/1.1傳輸是以message為單位。
(HTTP/2是以message frame訊息框為單位)
Message稱為訊息、報文、消息。

Message格式

Start-line(起始行)
Headers(多個header-field+CRLF)
CRLF
Message-body(可選的)

Request Message範例

POST /?id=1 HTTP/1.1        (Start-line=>request line)
Host: echo.paw.cloud        (header-field)
Content-Type: application/json; charset=utf-8 (header-field)
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0 (header-field)
Connection: close           (header-field)
Content-Length: 136         (header-field)
(CRLF)
(以下為message-body)
{
  "status": "ok",
  "extended": true,
  "results": [
    {"value": 0, "type": "int64"},
    {"value": 1.0e+3, "type": "decimal"}
  ]
}

Response Message範例

HTTP/1.1 200 OK                      (Start-line=>status line)
Content-Type: text/html; charset=utf-8    (header-field)
Date: Sat, 18 Feb 2017 00:01:57 GMT       (header-field)
Server: nginx/1.11.8                      (header-field) 
transfer-encoding: chunked                (header-field)
Connection: Close                         (header-field)
(CRLF)
(以下為message-body)
<html lang="en">
<head>
<title>echo<title>
</head>

Start-line 起始行

Request start line,依序是:
方法(method)、空白(space)、請求目標(request target)、空白(space)、HTTP版本(http version)、CRLF

Response start line,依序是:
HTTP版本(http version)、空白(space)、狀態碼(status code)、空白(space)、原因短語(reason phrase)、CRLF

Method

HTTP1.0 定義三種方法:post,get,head
HTTP1.1 定義的方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT

GET  请求指定的页面信息,并返回实体主体。
HEAD     类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头
POST     向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。POST请求可能会导致新的资源的建立和/或已有资源的修改。
PUT      从客户端向服务器传送的数据取代指定的文档的内容。
DELETE   请求服务器删除指定的页面。
CONNECT  HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。
OPTIONS  允许客户端查看服务器的性能。
TRACE    回显服务器收到的请求,主要用于测试或诊断
  • POST 
  • 規定資料要放在body中
    四種常用的content-type:
    application/x-www-form-urlencoded
    multipart/form-data
    application/json
    text/xml

Status Code

1xx 請求接受,繼續處理。
2xx 請求成功。
3xx 重定向-要完成請求要進一步處理。
4xx 客戶端錯誤,例如請求語法錯誤。請求無法實現:403。
5xx 服務器端錯誤。

Content Type

NotFalse 技術客詳細說明
  • application/x-www-form-urlencoded
  • 常用於POST表單,是key-value形式

    POST http://www.example.com HTTP/1.1
    Content-Type: application/x-www-form-urlencoded;charset=utf-8
    .....省略
    title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3
    

  • multipart/form-data
  • 使用表單上傳文件時,必須對form的enctype設定為multipart-/form-data

    POST http://www.example.com HTTP/1.1
    Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA
    
    ------WebKitFormBoundaryrGKCBY7qhFd3TrwA
    Content-Disposition: form-data; name="text"
    
    title
    ------WebKitFormBoundaryrGKCBY7qhFd3TrwA
    Content-Disposition: form-data; name="file"; filename="chrome.png"
    Content-Type: image/png
    
    PNG ... content of chrome.png ...
    ------WebKitFormBoundaryrGKCBY7qhFd3TrwA--
    

  • application/json
  • POST http://www.example.com HTTP/1.1 
    Content-Type: application/json;charset=utf-8
    
    {"title":"test","sub":[1,2,3]}
    


  • text/xml
  • POST http://www.example.com HTTP/1.1 
    Content-Type: text/xml
    
    
    <methodcall>
        <methodname>examples.getStateName</methodname>
        <params>
            <param />
                <value><i4>41</i4></value>
            
        </params>
    </methodcall>

keep alive模式

參考
keep alive 需client-server兩邊配合

http 1.0中默認是關閉的,需要在http頭加入"Connection: Keep-Alive”,才能啓用Keep-Alive;

http 1.1中默認啓用Keep-Alive,如果加入"Connection: close “,才關閉。

目前大部分瀏覽器都是用http1.1協議,也就是說默認都會發起Keep-Alive的連接請求了

所以是否能完成一個完整的Keep- Alive連接就看服務器設置情況。