Wednesday, September 21, 2011

Using PHP-cli to send APRS-IS messages

I really can't take credit for this script, I can only take credit for modifying it to send messages into the APRS stream.

The original script can be found here:
http://boxoblog.wordpress.com/2011/02/18/php-telnet-script-to-connect-to-tor-and-refresh-ip-address/

IT is a quick/dirty modify, and still needs to be tweeted, but here is the basics of it:

function tor_telnet($ip,$port,$auth,$command) {

//$fp = fsockopen($ip,$port,$error_number,$err_string,10);
$fp = fsockopen($ip,$port);
//if(!$fp) {echo “$error_number $err_string“;
//return;} else {
fwrite($fp, $auth . "\n");

$received = fread($fp,512);
echo $received;
sleep(10);

fwrite($fp,$command."\n");

echo "\n";
$received = fread($fp,512);
// list($rcode,$explanation) = explode(‘ ‘,$received,2);
echo $received;

//}
fclose($fp);
return;
}

//tor_telnet(’127.0.0.1′,’9051′,’password’,'signal NEWNYM’);

tor_telnet('aprsdcsp.ka7o.net', '1314', 'user KD8BXP pass PASSCODE', 'KD8BXP>TCPIP*::N4TRQ-9 :telnet script ack via twit');




I keep getting an error in the original script on line 5, I just didn't spend time to figure it out, and ended up commenting out the whole line. I also changed the fsockopen line thinking that may have been causing the other problem.

So, it's quick and dirty, logs into the message stream on APRS-IS, authorizes your callsign to send messages, waits a few seconds, and sends the message.

This is the line that makes the magic happen:
tor_telnet('aprsdcsp.ka7o.net', '1314', 'user CALLSIGN pass PASSCODE', 'KD8BXP>TCPIP*::N4TRQ-9 :telnet script ack via twit');

1st is the server to use, you'll probably want to change this, 2nd is the port number, in this case it's the message port, 3rd is the authorization request, just replace CALLSIGN with your base call, and PASSCODE with your APRS-IS passcode. (This is the same code that other APRS apps use, so you may already know it, if there are a few apps that will tell you it)
The last thing is the aprs message it's self: This took me a while to figure out, but I finally found some documentation on the right format to use.

SO, let's look at the message string:
KD8BXP>TCPIP*::N4TRQ-9 :message
^^^^^^
|- Your Callsign (can use SSID)

KD8BXP>TCPIP*::N4TRQ-9 :message
^^^^^^^^^
|- This needs to be left alone, do not change it

KD8BXP>TCPIP*::N4TRQ-9 :message
^^^^^^^^^^
|- The destination call (NOTE: IT"S 9 DIGITS and then a colon, N4TRQ-9 is 7 digits, so you need to add 2 spaces, if you were just using the base call N4TRQ you'd need to add 4 spaces THIS IS IMPORTANT)

KD8BXP>TCPIP*::N4TRQ-9 :message
^^^^^^^^
|- the message to send, remember no more then 60 characters

The Colon separate everything and need to be there.

So, probably a better way to do the string would be:
$call = 'KD8BXP';
$dest = 'N4TRQ-9 ';
$msg = 'This is a msg';


tor_telnet('server', 'port', 'authorization', $call . '>TCPIP*::' . $dest . ':' . $msg);

Yes I put the spaces in the $dest variable - but you could write a quick check, and add the spaces just as well.

Anyways, hope this helps you out, I use PHP and the CLI (Command Line Interface), but I don't see any reason why this simple script wouldn't work on a web server.

No comments:

Post a Comment