#!/usr/bin/env perl

use strict;
use warnings;

my $modem = "/dev/ttyUSB0";
# Substitute xxxx with your PIN.
# You should probably put your pin somewhere else, e.g. on an USB stick,
# an encrypted file system or something else, and read it from there...
# You have been warned!
my $pin = "xxxx";

$SIG{ALRM} = sub {
        die("timeout: no response from modem $modem\n");
};

open(MODEM, "+<", $modem) or die("can't open modem $modem");
alarm(10);
print(MODEM "AT+CPIN=\"$pin\"\n\r");
while (<MODEM>) {
        if (m/OK/) {
                close(MODEM);
                print("PIN accepted\n");
                exit(0);
        }
        if (m/ERROR/) {
                close(MODEM);
                print("PIN rejected\n");
                exit(1);
        }
} 
