1

I want to use one of the values from a command and use it in a different command Example Command is as follows

 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"

The lines that pass UNREACHABLE are as follows.

[Oct 8 04:10:55] NOTICE[29814] chan_sip.c: Peer 'sip-437-id' is now UNREACHABLE! Last qualify: 26

awk '{print $7;}'

'sip-437-id' I get this information

sort | uniq 

sorting and uniq device only

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

I get an more information belong to xargs wt this user extansion

* 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"

I get the information I need from this information.

> * 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

this information will be written on the screen Addr->IP : 10.34.34.45:5063

I want to ping the IP address and print it under the information

for example:

>   * 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

should show the result and continue processing

  • how many request packets should be sent by ping? – RomanPerekhrest Oct 11 '17 at 15:09
  • 4 is enough for me – Tahir ERGEÇ Oct 11 '17 at 15:11
  • I suppose, this ' sip-437-id' is a typo (the 7th column), there should not be a whitespace, 'sip-437-id', right? – RomanPerekhrest Oct 11 '17 at 15:21
  • yes there is no space at the beginning – Tahir ERGEÇ Oct 11 '17 at 15:24
1

Optimized solution:

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)}'
  • dear my friend thank you very much for the solution worked very successfully – Tahir ERGEÇ Oct 11 '17 at 16:03
  • @TahirERGEÇ, welcome, I'm glad it's working – RomanPerekhrest Oct 11 '17 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.