0

I am trying to run this php code as a command line in applescript. here is the code

"/usr/bin/php -r '($mac = 'gg:a2:gg:gg:gg:e6'; $porttemp =  '9'; $ip ='255.255.255.255';  
$mac_bytes = explode(\":\", $mac);
$mac_addr = \"\"; 
for ($i=0; $i<6; $i++) 
$mac_addr .= chr(hexdec($mac_bytes[$i]));
$packet = \"\"; 
for ($i=0; $i<6; $i++)  /*6x 0xFF*/ 
$packet .= chr(255); 
for ($i=0; $i<16; $i++) /*16x MAC address*/ 
$packet .= $mac_addr;
$port = $porttemp;
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 
socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, TRUE);
socket_sendto($sock, $packet, strlen($packet), 0, $ip, $port); 
socket_close($sock);
)'"

The syntax checks out fine on the applescript editor and the script runs but it pops this error:

Result: error "PHP Parse error: syntax error, unexpected ':' in Command line code on line 1" number 254

not sure what the issue is but is it the : in the mac address in the first line or the : later and also, I tried \":\" with the :`s but that did not work either. Any idea whats wrong?

  • Won't the multiple single quotes within the -r option throw it off? – Mike B Sep 29 '11 at 04:11
  • 1
    Seems to be ok to run it with single quotes, in fact you should. But try to change all the \" to just double quotes without the backslash. Change all the single quotes to double as well. so like, '($mac = "gg:a2:gg:gg:gg:e6"; instead. – Matt Sep 29 '11 at 04:17
  • @Matt I agree but this confuses me. "*Seems to be ok to run it with single quotes, in fact you should ... Change all the single quotes to double as well*". ??? Did you mean ok to run with single quotes around the entire command? – Mike B Sep 29 '11 at 04:20
  • 1
    @Mike B yea i misread you, i thought you meant the first single quotes. – Matt Sep 29 '11 at 04:22

1 Answers1

1

Try something like this instead:

"/usr/bin/php -r '($mac = "gg:a2:gg:gg:gg:e6";
$porttemp =  "9";
$ip ="255.255.255.255";
$mac_bytes = explode(":", $mac);
$mac_addr = ""; 
for ($i=0; $i<6; $i++) 
$mac_addr .= chr(hexdec($mac_bytes[$i]));
$packet = ""; 
for ($i=0; $i<6; $i++)  /*6x 0xFF*/ 
$packet .= chr(255); 
for ($i=0; $i<16; $i++) /*16x MAC address*/ 
$packet .= $mac_addr;
$port = $porttemp;
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 
socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, TRUE);
socket_sendto($sock, $packet, strlen($packet), 0, $ip, $port); 
socket_close($sock);
)'"
Matt
  • 7,049
  • 7
  • 50
  • 77
  • There's still the problem that those quotes are not escaped. Every `"` inside the enclosing `"`s should be `\"` – MrTrick Sep 29 '11 at 04:33
  • MrTrick, yea i thought about that. I guess you would need to escape all double quotes in this case. Not sure if it would work, but maybe you can create a variable to hold that and then replace it all with the variable like /usr/bin/php -r $somecode, not sure if that would wrk or not. – Matt Sep 29 '11 at 04:35
  • hey, I did this: "/usr/bin/php -r '($mac = \"01:a2:b3:c4:d5:e6\"; $porttemp = \"9\"; $ip = \"255.255.255.255\"; now Im getting this error: error "PHP Parse error: syntax error, unexpected ';' in Command line code on line 1" number 254 I guess I dont use the initial "? – aayush sharma Sep 29 '11 at 05:44
  • @user699863 if you were doing it directly in the terminal, then you would not need the double quote before /usr/bin..., I'm not sure how applescript works though. – Matt Oct 01 '11 at 05:38