Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I found a small bash script to do exactly what I need but I tweaked a few things. Now I've realised I need this in PHP instead.

Would anyone mind helping me convert this to PHP please? I've done some parts, but with the "==" parts I'm not sure how that works out.

PHP
ssrc=$(tshark -n -r pcap_filename_here -R rtp -T fields -e rtp.ssrc -Eseparator=, | sort -u | awk 'FNR ==1{print}')
echo $ssrc

sudo tshark -n -r pcap_filename_here -R rtp -R "rtp.ssrc == $ssrc" -T fields -e rtp.payload | tee payloads

for payload in `cat payloads`; do IFS=:; for byte in $payload;
do printf "\\x$byte" >> output.raw; done;
done

sox -t raw -r 8000 -v 4 -c 1 -e a-law output.raw output.wav



PHP
<?php

$filename = $_GET['filename'];

exec("tshark -n -r $filename -R rtp -T fields -e rtp.ssrc -E separator=, | sort -u | awk 'FNR ==1{print}'", $ssrc);

echo $ssrc;

exec("sudo tshark -n -r $filename -R rtp -R "rtp.ssrc == $ssrc" -T fields -e rtp.payload | tee payloads");

# unsure how to do this loop

exec('sox -t raw -r 8000 -v 4 -c 1 -e a-law output.raw output.wav');

?>


What I have tried:

--------------------------------------------------------------------------------
Posted
Updated 4-Apr-18 23:28pm
v2
Comments
Richard MacCutchan 5-Apr-18 4:57am    
Why not just 'exec' the entire bash script?
[no name] 5-Apr-18 5:00am    
Although I didn't think of that, I need to add some things to the script which I don't know how to do in bash, as I'm much more experienced with php. Also that would require an additional php file to exec.

1 solution

When you need to include a double quote within a string, you have to escape it as described at PHP: Strings - Manual[^]:
PHP
exec("sudo tshark -n -r $filename -R rtp -R \"rtp.ssrc == $ssrc\" -T fields -e rtp.payload | tee payloads");

The for loop requires understanding the bash commands:
The for loop is a little bit different from other programming languages. Basically, it let's you iterate over a series of 'words' within a string.
So the outer loop will iterate over the white space separated words of the text file payloads and store them in the variable $payload (cat payloads prints the content of text files).

The characters used to detect word boundaries are stored in the $IFS variable (see Advanced Bash-Scripting Guide: Chapter 9. Another Look at Variables[^]). So IFS=: changes this from white spaces to the colon character. The inner for loop splits $payload into : separated words and store them in $byte. That is printed out with a "\x" prefix to the file output.raw.

What the loop finally does is converting a text file with colon separated hex byte values to C style notation skipping all white spaces. Example:
Input : 01:ff:3a:4c 5B:88\n 99:43
Output: \x01\xff\x3a\x4c\x5B\x88\x99\x43
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900