Linux

擴展管道鏈的處理(… Asterisk -> grep -> ping)

  • October 11, 2017

我想使用命令中的一個值並在不同的命令中使用它範例命令如下

cat /var/log/asterisk/full |
  grep "UNREACHABLE" |
  awk '{print $7;}'|
  sort | uniq |
  xargs -I % asterisk -rx 'sip show peer %'|
  grep "Callerid\|Useragent\|Name\|Addr->IP"

通過 UNREACHABLE 的行如下。

$$ Oct 8 04:10:55 $$注意$$ 29814 $$chan_sip.c:對等 ‘sip-437-id’ 現在無法訪問!最後資格:26

awk '{print $7;}'

‘sip-437-id’ 我得到了這個資訊

sort | uniq 

僅排序和唯一設備

xargs -I % asterisk -rx 'sip show peer %'

我得到一個屬於 xargs wt 這個使用者擴展的更多資訊

* Name       : sip-437-id
Secret       : <Set>
MD5Secret    : <Not set>   
Remote Secret: <Not set>   
Context      :default   
Subscr.Cont. : default   
Language     :   
AMA flags    :Unknown   
Transfer mode: open   
CallingPres  : 
Presentation Allowed,Not Screened   
Callgroup    :   
Pickupgroup  :   
MOH Suggest  :  
Mailbox      : 492@default   
VM Extension : asterisk   
LastMsgsSent : 0/0   
Call limit   : 2   
Max forwards : 0   
Dynamic      : Yes  
Callerid     : "TELEPHONE" <492>   
MaxCallBR    : 384 kbps   
Expire   : 1671   
Insecure     : no   
Force rport  : No   
ACL          : Yes  
DirectMedACL : No   
T.38 support : No   
T.38 EC mode : Unknown T.38
> MaxDtgrm: 4294967295   
DirectMedia  : No   
PromiscRedir : No  
> User=Phone   : No   
Video Support: No   
Text Support : No   
Ign SDPver  : No   
Trust RPID   : Yes   
Send RPID    : Yes   
TrustIDOutbnd:Legacy   
Subscriptions: Yes   
Overlap dial : Yes   
DTMFmode     :rfc2833   
Timer T1 : 500   
Timer B      : 32000   
ToHost       :  
Addr->IP     : 10.34.34.45:5063   
Defaddr->IP  : (null)   
Prim.Transp. : UDP   
Allowed.Trsp : UDP   Def. U
sername: sip-492-id   
SIP Options : (none)   
Codecs       : 0x10c (ulaw|alaw|g729)   
Codec Order  : (g729:20,ulaw:20,alaw:20)   
Auto-Framing : No   
Status       : OK (26 ms)   
Useragent    : Yealink SIP-T20P 9.61.0.85   
Reg. Contact : sip:sip-437-id @10.34.34.45:5063   
Qualify Freq : 60000 ms   
Sess-Timers : Accept   
Sess-Refresh : uas   
Sess-Expires : 1800 secs   
Min-Sess   : 90 secs   
RTP Engine   : asterisk   
Parkinglot   :   
Use Reason   : No   
Encryption   : No
grep "Callerid\|Useragent\|Name\|Addr->IP"

我從這些資訊中得到了我需要的資訊。

> * Name       : sip-437-id
> Callerid     : "TELEPHONE" <492>
> Addr->IP     : 10.34.34.45:5063
> Useragent    : Yealink SIP-T20P 9.61.0.85
> Addr->IP     : 10.34.34.45:5063

此資訊將寫入螢幕 Addr->IP : 10.34.34.45:5063

我要ping IP地址,在資訊下列印

例如:

>   * Name       : sip-437-id   
> Callerid     : "TELEPHONE" <492>
> Addr->IP     : 10.34.34.45:5063   
> Useragent    : Yealink SIP-T20P 9.61.0.85 
> PING 10.34.34.45 (10.34.34.45) 56(84) bytes of data. 64 bytes from
> 10.34.34.45: icmp_req=1 ttl=62 time=6.22 ms 64 bytes from
> 10.34.34.45: icmp_req=2 ttl=62 time=6.25 ms

應該顯示結果並繼續處理

優化解決方案:

awk '/UNREACHABLE/{ print $7 }' /var/log/asterisk/full | sort -u \
   | xargs -I % asterisk -rx 'sip show peer %' \
   | grep "Callerid\|Useragent\|Name\|Addr->IP" \
   | awk -F':' '1;/Addr->IP/{ system("ping -c4 "$2)}'

引用自:https://unix.stackexchange.com/questions/397474