ss: Display Linux TCP / UDP Network and Socket Information

ss: Display Linux TCP / UDP Network and Socket Information


The ss command is used to show socket statistics. It can display stats for PACKET sockets, TCP sockets, UDP sockets, DCCP sockets, RAW sockets, Unix domain sockets, and much more. It allows showing information similar to netstat command. It can display more TCP and state information than other tools. It is a new, incredibly useful and faster (as compare to netstat) tool for tracking TCP connections and sockets. SS can provide information about:

  • All TCP sockets.
  • All UDP sockets.
  • All established ssh / ftp / http / https connections.
  • All local processes connected to X server.
  • Filtering by state (such as connected, synchronized, SYN-RECV, SYN-SENT,TIME-WAIT), addresses and ports.
  • All the tcp sockets in state FIN-WAIT-1 and much more.


Most Linux distributions are shipped with ss and many monitoring tools. Being familiar with this tool helps enhance your understand of what's going on in the system sockets and helps you find the possible causes of a performance problem.

Task: Display Sockets Summary

List currently established, closed, orphaned and waiting TCP sockets, enter:
# ss -s
Sample Output:

Total: 734 (kernel 904)
TCP:   1415 (estab 112, closed 1259, orphaned 11, synrecv 0, timewait 1258/0), ports 566
Transport Total     IP        IPv6
*	  904       -         -
RAW	  0         0         0
UDP	  15        12        3
TCP	  156       134       22
INET	  171       146       25
FRAG	  0         0         0  

Task: Display All Open Network Ports

# ss -l
Sample Output:

ss -l
Recv-Q Send-Q                                                  Local Address:Port                                                      Peer Address:Port
0      0                                                           127.0.0.1:smux                                                                 *:*
0      0                                                           127.0.0.1:10024                                                                *:*
0      0                                                           127.0.0.1:10025                                                                *:*
0      0                                                                   *:3306                                                                 *:*
0      0                                                                   *:http                                                                 *:*
0      0                                                                   *:4949                                                                 *:*
0      0                                                                   *:domain                                                               *:*
0      0                                                                   *:ssh                                                                  *:*
0      0                                                                   *:smtp                                                                 *:*
0      0                                                           127.0.0.1:rndc                                                                 *:*
0      0                                                           127.0.0.1:6010                                                                 *:*
0      0                                                        	   *:https                                                                *:*
0      0                                                                  :::34571                                                               :::*
0      0                                                                  :::34572                                                               :::*
0      0                                                                  :::34573                                                               :::*
0      0                                                                 ::1:rndc                                                                :::*       

Type the following to see process named using open socket:
# ss -pl
Find out who is responsible for opening socket / port # 4949:
# ss -lp | grep 4949
Sample output:

0      0                            *:4949                          *:*        users:(("munin-node",3772,5))

munin-node (PID # 3772) is responsible for opening port # 4949. You can get more information about this process (like memory used, users, current working directory and so on) visiting /proc/3772 directory:
# cd /proc/3772
# ls -l

Task: Display All TCP Sockets

# ss -t -a

Task: Display All UDP Sockets

# ss -u -a

Task: Display All Established SMTP Connections

# ss -o state established '( dport = :smtp or sport = :smtp )'

Task: Display All Established HTTP Connections

# ss -o state established '( dport = :http or sport = :http )'

Task: Find All Local Processes Connected To X Server

# ss -x src /tmp/.X11-unix/*

Task: List All The Tcp Sockets in State FIN-WAIT-1

List all the TCP sockets in state -FIN-WAIT-1 for our httpd to network 202.54.1/24 and look at their timers:
# ss -o state fin-wait-1 '( sport = :http or sport = :https )' dst 202.54.1/24

How Do I Filter Sockets Using TCP States?

The syntax is as follows:

 
## tcp ipv4 ##
ss -4 state FILTER-NAME-HERE
 
## tcp ipv6 ##
ss -6 state FILTER-NAME-HERE
 

Where FILTER-NAME-HERE can be any one of the following,

  1. established
  2. syn-sent
  3. syn-recv
  4. fin-wait-1
  5. fin-wait-2
  6. time-wait
  7. closed
  8. close-wait
  9. last-ack
  10. listen
  11. closing
  12. all : All of the above states
  13. connected : All the states except for listen and closed
  14. synchronized : All the connected states except for syn-sent
  15. bucket : Show states, which are maintained as minisockets, i.e. time-wait and syn-recv.
  16. big : Opposite to bucket state.

Examples

Type the following command to see closing sockets:

 
ss -4 state closing
 
Recv-Q Send-Q                                                  Local Address:Port                                                      Peer Address:Port
1      11094                                                  75.126.153.214:http                                                      175.44.24.85:4669

How Do I Matches Remote Address And Port Numbers?

Use the following syntax:

 
ss dst ADDRESS_PATTERN
 
## Show all ports connected from remote 192.168.1.5##
ss dst 192.168.1.5
 
## show all ports connected from remote 192.168.1.5:http port## 
ss dst 192.168.1.5:http
ss dst 192.168.1.5:smtp
ss dst 192.168.1.5:443
 

Find out connection made by remote 123.1.2.100:http to our local virtual servers:
# ss dst 123.1.2.100:http
Sample outputs:

State      Recv-Q Send-Q                                             Local Address:Port                                                 Peer Address:Port
ESTAB      0      0                                                 75.126.153.206:http                                               123.1.2.100:35710
ESTAB      0      0                                                 75.126.153.206:http                                               123.1.2.100:35758

How Do I Matches Local Address And Port Numbers?

 
ss src ADDRESS_PATTERN
### find out all ips connected to nixcraft.com ip address 75.126.153.214 ###
## Show all ports connected to local 75.126.153.214##
ss src 75.126.153.214
 
## http (80) port only ##
ss src 75.126.153.214:http
ss src 75.126.153.214:80
 
## smtp (25) port only ##
ss src 75.126.153.214:smtp
ss src 75.126.153.214:25
 
 
 

How Do I Compare Local and/or Remote Port To A Number?

Use the following syntax:

 
## Compares remote port to a number ##
ss dport OP PORT
 
## Compares local port to a number ##
sport OP PORT
 

Where OP can be one of the following:

  1. <= or le : Less than or equal to port
  2. >= or ge : Greater than or equal to port
  3. == or eq : Equal to port
  4. != or ne : Not equal to port
  5. < or gt : Less than to port
  6. > or lt : Greater than to port
  7. Note: le, gt, eq, ne etc. are use in unix shell and are accepted as well.

Examples

 
###################################################################################
### Do not forget to escape special characters when typing them in command line ###
###################################################################################
 
ss  sport = :http
ss  dport = :http
ss  dport \> :1024
ss  sport \> :1024
ss sport \< :32000
ss  sport eq :22
ss  dport != :22
ss  state connected sport = :http
ss \( sport = :http or sport = :https \)
ss -o state fin-wait-1 \( sport = :http or sport = :https \) dst 192.168.1/24
 

ss vs netstat Speed

Use the time command to run both programs and summarize system resource usage. Type the netstat command as follows:
# time netstat -at
Sample outputs:

real	2m52.254s
user	0m0.178s
sys	0m0.170s

Now, try the ss command:
# time
Sample outputs:

real	2m11.102s
user	0m0.124s
sys	0m0.068s

Note: Both outputs are taken from reverse proxy acceleration server running on RHEL 6.x amd64.

Recommended readings:

'统计字符串中是否有文字,是否全是数字 Function hasnoChinese(str) bl = True For i = 1 to Len(str) - 0 'ss=CStr(Asc(Mid(str,i,1))) 'msgbox ss If Asc(Mid(str,i,1)) < 0 Then bl = False End If Next hasnoChinese = bl End Function Function UnAdjustNote(FontClass,dx,dy,widscale,heightscale,nochinese) SSProcess.ClearSelection SSProcess.ClearSelectCondition SSProcess.SetSelectCondition "SSObj_Type", "=", "NOTE" SSProcess.SetSelectCondition "SSObj_FontClass", "=", FontClass SSProcess.SetSelectCondition "[DGNAttr_FontID]", "<>", "188" SSProcess.SelectFilter SSProcess.PushUndoMark notecount = SSProcess.GetSelNoteCount 'msgbox notecount For i=0 To notecount-1 txtx=SSProcess.GetSelNoteValue( i, "SSObj_FontString") If hasnoChinese(txtx) = nochinese Then pointcount = SSProcess.GetSelNotePointCount(i) For j=0 To pointcount-1 Dim x, y, z, pointtype, name SSProcess.GetSelNotePoint i, j, x, y, z, pointtype, name x = x - dx y = y - dy SSProcess.SetSelNotePoint i, j, x, y, z, pointtype, name Next wid=SSProcess.GetSelNoteValue( i, "SSObj_FontWidth") hit=SSProcess.GetSelNoteValue( i, "SSObj_FontHeight") wid1=CStr(CDbl(wid) / widscale) hit1=CStr(CDbl(hit) / heightscale) SSProcess.SetSelNoteValue i, "SSObj_FontWidth", wid1 SSProcess.SetSelNoteValue i, "SSObj_FontHeight", hit1 SSProcess.AddSelNoteToSaveNoteList i End If Next End Function Function changeNoteDw() UnAdjustNote "#",0,0,1.16,1,True UnAdjustNote "#",0,0,1.16,1,False UnAdjustNote "FZ",0,0,1.25,1,True UnAdjustNote "FZ",0,0,1.25,1,False UnAdjustNote "@",0,0,1.275,1,True UnAdjustNote "@",0,0,1.275,1,False UnAdjustNote "899000",0,0,1.27,1,True UnAdjustNote "899000",0,0,1.27,1,False UnAdjustNote "DXLC",0,0,1.333,1,True UnAdjustNote "DXLC",0,0,1.333,1,False UnAdjustNote "499000",-0.117,0,1,1.3,True UnAdjustNote "499000",-0.117,0,1,1.3,False End Function Function UnAdjustNote1(EpsFontClass,dx,dy,widscale,heightscale,nochinese) SSProcess.ClearSelection SSProcess.ClearSelectCondition SSProcess.SetSelectCondition "SSObj_Type", "=", "NOTE" SSProcess.SetSelectCondition "SSObj_FontClass", "=", EpsFontClass SSProcess.SelectFilter SSProcess.PushUndoMark notecount = SSProcess.GetSelNoteCount 'msgbox notecount For i=0 To notecount-1 txtx=SSProcess.GetSelNoteValue( i, "SSObj_FontString") If hasnoChinese(txtx) = nochinese Then pointcount = SSProcess.GetSelNotePointCount(i) For j=0 To pointcount-1 Dim x, y, z, pointtype, name SSProcess.GetSelNotePoint i, j, x, y, z, pointtype, name x = x - dx y = y - dy SSProcess.SetSelNotePoint i, j, x, y, z, pointtype, name Next wid=SSProcess.GetSelNoteValue( i, "SSObj_FontWidth") hit=SSProcess.GetSelNoteValue( i, "SSObj_FontHeight") wid1=CStr(CDbl(wid) / widscale) hit1=CStr(CDbl(hit) / heightscale) SSProcess.SetSelNoteValue i, "SSObj_FontWidth", wid1 SSProcess.SetSelNoteValue i, "SSObj_FontHeight", hit1 SSProcess.AddSelNoteToSaveNoteList i End If Next End Function Function changeNoteDw1() '属于86号字体(居民地注记) UnAdjustNote1 "297000",2.5,0,1.33,1.25,True UnAdjustNote1 "297000",2.5,0,1.33,1.25,False UnAdjustNote1 "297100",1.25,-0.3,1.33,1.25,True UnAdjustNote1 "297100",1.25,-0.3,1.33,1.25,False UnAdjustNote1 "297200",2.25,-0.3,1.33,1.25,True UnAdjustNote1 "297200",2.25,-0.3,1.33,1.25,False UnAdjustNote1 "297500",1.8,0,1.33,1.33,True UnAdjustNote1 "297500",1.8,0,1.33,1.33,False '属于86号字体(地貌注记) UnAdjustNote1 "881000",0.7,-0.15,1.3,1.25,True UnAdjustNote1 "881000",0.7,-0.15,1.3,1.25,False UnAdjustNote1 "882000",0.7,-0.15,1.3,1.33,True UnAdjustNote1 "882000",0.7,-0.15,1.3,1.33,False '属于242号字体(居民地注记) UnAdjustNote1 "297300",1.6,0,1.3,1.3,True UnAdjustNote1 "297300",1.6,0,1.3,1.3,False UnAdjustNote1 "297400",2,0,1.3,1.3,True UnAdjustNote1 "297400",2,0,1.3,1.3,False '属于86号字体(水系注记) 'UnAdjustNote "698100",0.3,0.25,1.25,1.4,True 'UnAdjustNote "698100",0.3,0.25,1.25,1.4,False UnAdjustNote1 "698100",-0.2,0,1.27,1.25,True UnAdjustNote1 "698100",-0.2,0,1.27,1.25,False UnAdjustNote1 "698200",-0.1,0.1,1.27,1.26,True UnAdjustNote1 "698200",-0.1,0.1,1.27,1.26,False UnAdjustNote1 "698300",-0.1,0.1,1.27,1.26,True UnAdjustNote "698300",-0.1,0.1,1.27,1.26,False '属于43号字体(居民地注记) UnAdjustNote1 "298000",0,-0.1,1.2,1.3,True UnAdjustNote1 "298000",0,-0.1,1.2,1.3,False UnAdjustNote1 "298200",0,-0.1,1.23,1.3,True UnAdjustNote1 "298200",0,-0.1,1.23,1.3,False '属于43号字体(交通注记注记) UnAdjustNote1 "498000",0.3,0,1.35,1.5,True UnAdjustNote1 "498000",0.3,0,1.35,1.5,False UnAdjustNote1 "498100",0.5,0,1.35,1.5,True UnAdjustNote1 "498100",0.5,0,1.35,1.5,False '属于88号字体(交通注记) UnAdjustNote1 "498200",1.3,-0.15,1.33,1.35,True UnAdjustNote1 "498200",1.3,-0.15,1.33,1.35,False UnAdjustNote1 "498300",1.3,-0.15,1.33,1.35,True UnAdjustNote1 "498300",1.3,-0.15,1.33,1.35,False UnAdjustNote1 "498400",1,-0.15,1.33,1.35,True UnAdjustNote1 "498400",1,-0.15,1.33,1.35,False End Function Function AddNewPointFormLine(Code,NewCode,distx,disty) SSProcess.ClearSelection SSProcess.ClearSelectCondition SSProcess.SetSelectCondition "SSObj_Type", "=", "LINE" SSProcess.SetSelectCondition "SSObj_Code", "=", Code SSProcess.SelectFilter geocount = SSProcess.GetSelGeoCount For i = 0 To geocount - 1 sso= SSProcess.GetSelGeoPointCount(i) If sso = 5 or sso = 4 Then '取出处理对象 ppid = SSProcess.GetSelGeoValue( i, "SSObj_ID") ppcode = SSProcess.GetSelGeoValue( i, "SSObj_Code") '取出对象点坐标 x0 = CDbl(SSProcess.GetObjectAttr( ppid, "SSObj_X(0)")) y0 = CDbl(SSProcess.GetObjectAttr( ppid, "SSObj_Y(0)")) x1 = CDbl(SSProcess.GetObjectAttr( ppid, "SSObj_X(1)")) Y1 = CDbl(SSProcess.GetObjectAttr( ppid, "SSObj_Y(1)")) x3 = CDbl(SSProcess.GetObjectAttr( ppid, "SSObj_X(3)")) y3 = CDbl(SSProcess.GetObjectAttr( ppid, "SSObj_Y(3)")) '算出准备插入点符号的位置x,y If ppcode <> 515100 Then x = (x0 + x3)/2 y = (y0 + y3)/2 '距离 dist1 = sqr((x0-x3)*(x0-x3)+(y0-y3)*(y0-y3)) dist2 = sqr((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)) scalex = dist1/distx scaley = dist2/disty '角度 Dx = x1-x0 Dy = y1-y0 If Dx = 0 and Dy > 0 Then arc = 0 ElseIf Dx = 0 and Dy < 0 Then arc = 3.1415926 ElseIf Dx < 0 and Dy < 0 Then arc = 3.1415926-atn(Dx/Dy) ElseIf Dx < 0 and Dy = 0 Then arc = 3.1415926/2 ElseIf Dx > 0 and Dy = 0 Then arc = -3.1415926/2 ElseIf Dx > 0 and Dy < 0 Then arc = 3*3.1415926-atn(Dx/Dy) Else arc = 2*3.1415926-atn(Dx/Dy) End If Else x = (x1 + x3)/2 y = (y1 + y3)/2 '距离 dist1 = sqr((x0-x3)*(x0-x3)+(y0-y3)*(y0-y3)) dist2 = sqr((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)) scalex = dist1/distx scaley = dist2/disty '角度 Dx = x1-x0 Dy = y1-y0 If Dx = 0 and Dy > 0 Then arc = 0 ElseIf Dx = 0 and Dy < 0 Then arc = 3.1415926 ElseIf Dx < 0 and Dy < 0 Then arc = 3.1415926-atn(Dx/Dy) ElseIf Dx < 0 and Dy = 0 Then arc = 3.1415926/2 ElseIf Dx > 0 and Dy = 0 Then arc = -3.1415926/2 ElseIf Dx > 0 and Dy < 0 Then arc = 3*3.1415926-atn(Dx/Dy) Else arc = 2*3.1415926-atn(Dx/Dy) End If End If '按照x,y,scale插入点符号 SSProcess.CreateNewObj 0 SSProcess.SetNewObjValue "SSObj_Code", NewCode SSProcess.SetNewObjValue "SSObj_LineType", "0" SSProcess.SetNewObjValue "SSObj_LayerName", "DEFAULT" SSProcess.SetNewObjValue "SSObj_Color", "RGB(255,255,0)" SSProcess.SetNewObjValue "SSObj_Angle", arc SSProcess.SetNewObjValue "SSObj_ScaleX", scalex SSProcess.SetNewObjValue "SSObj_ScaleY", scaley SSProcess.AddNewObjPoint x, y, 0, 0, "" SSProcess.AddNewObjToSaveObjList End If Next End Function Function lineTopoint() AddNewPointFormLine "221100","2211001","1","0.75" AddNewPointFormLine "861100","8611001","1","0.5" AddNewPointFormLine "515100","5151001","0.5","0.5" End Function Function arcTodgn() SSProcess.ClearSelection SSProcess.ClearSelectCondition SSProcess.SetSelectCondition "SSObj_Type", "==", "LINE" SSProcess.SetSelectCondition "SSObj_LineType", "==", "4" SSProcess.SetSelectCondition "<Clockwise>", "==", "1" SSProcess.SelectFilter geoCount = SSProcess.GetSelGeoCount For i=0 To geoCount-1 geoID = CLng(SSProcess.GetSelGeoValue (i, "SSObj_ID")) SSProcess.ObjectDeal geoID, "GotoPoints", "", result Next End Function Function ExportDgn( fileName ) SSProcess.SetMapStatus 1,2 '输出DGN前的锁库处理 changeNoteDw changeNoteDw1 lineTopoint arcTodgn '工作台面目录下的DGN种子文件 dgnSeedFile = SSProcess.GetSysPathName (8) & "seed5h.dgn" '工作台面目录下的线型对照文件 dgnlinestylefile = SSProcess.GetSysPathName (8) & "dgnlinestyle.lin" '清空转换参数 SSProcess.ClearDataXParameter '设置输出文件格式为DGN SSProcess.SetDataXParameter "DataType", "9" 'DGN种子文件 SSProcess.SetDataXParameter "EXCHANGE_DGN_SeedFile", dgnSeedFile 'DGN线型对照文件 SSProcess.SetDataXParameter "EXCHANGE_DGN_LineStyleFile", dgnlinestylefile '设置输出文件名 SSProcess.SetDataXParameter "ExportPathName", fileName '设置使用编码表 SSProcess.SetDataXParameter "FeatureCodeTBName", "FeatureCodeTB_OutDgn" SSProcess.SetDataXParameter "SymbolScriptTBName", "SymbolScriptTB_OutDgn" SSProcess.SetDataXParameter "NoteTemplateTBName", "NoteTemplateTB_OutDgn" SSProcess.SetDataXParameter "ExplodeObjLayerStatus","0" SSProcess.SetDataXParameter "DataBoundMode", "2" SSProcess.SetDataXParameter "LayerRelationCount", "1" SSProcess.SetDataXParameter "LayerRelation1", "图廓层:60:60:60:60:60" '打散对象编组输出 SSProcess.SetDataXParameter "ExplodeObjMakeGroup", "1" '颜色使用编码表指定 SSProcess.SetDataXParameter "ColorUseStatus", "0" '图层使用编码表指定 SSProcess.SetDataXParameter "LayerUseStatus", "0" '面地物输出方式 0 Shape 1 LineString SSProcess.SetDataXParameter "EXCHANGE_DGN_ExportAreaMode", "1" '线串最多点数 SSProcess.SetDataXParameter "EXCHANGE_DGN_MaxLineStringPointCount", "101" '开始输出数据 SSProcess.ExportData SSProcess.SetMapStatus 0,2 SSProcess.MapMethod "Unloaddata","" SSProcess.MapMethod "loaddata", "" End Function Sub OnClick() pathName = SSProcess.SelectPathName( ) If pathName = "" Then Exit Sub End If SSProcess.CreateMapFrame frameCount = SSProcess.GetMapFrameCount() For i=0 To frameCount-1 SSProcess.GetMapFrameCenterPoint i, x, y SSProcess.SetCurMapFrame x, y, 0, "" frameID = SSProcess.GetCurMapFrame() mapNumber = SSProcess.GetObjectAttr( CLng(frameID), "[MapNumber]") If mapNumber <> "" Then fileName = pathName & mapNumber & ".DGN" ExportDgn fileName End If Next SSProcess.FreeMapFrame End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值