#!/usr/bin/perl # # simpin.pl initializes HUAWEI 3G USB modems with SIM card password # # In order to use this script you have to create a Keychain entry under the # account 'simpin'. Here is how: # 1. Open Keychain Access (placed in Utilities folder of Applications) # 2. Menu: File -> New Password Item # 3. Keychain Item Name: Enter something usefull to you # 4. Account Name: simpin # 5. Password: Enter the PIN to your SIM card (usually a number) # 6. Press 'Add' # 7. When this scripts is executed the first time, a window will appear # asking you to allow access of this keychain. # 8. Enjoy! # (C) 2008, Mr. Bunbury -- may be freely copied and adapted. # # Code for communication with modem shamelessly stolen from CPAN: # http://www.cpan.org/scripts/assorted/talk-to-modem.pl use strict; my $debug = 0; # Set to 1 for debug-mode # PIN WILL BE PRINTED!!! my $pin = &getSIMPIN; # Retrieve simpin from # Keychain database my @commands = ( 'AT^HS=0,0', 'AT^CPIN="' . $pin . '"', # SIM PIN 'AT+CLCK="SC",2', 'AT+COPS=3,2', 'AT^CURC=1'); my $modem_dev = '/dev/cu.HUAWEIMobile-Modem'; # If code doesn't work # your device may differ! open MDM, "+>$modem_dev" or die "$0: Can't open 3G Modem\n"; system("/bin/stty -f $modem_dev raw -echo"); foreach my $out (@commands) { # Cycle through AT cmds my $in; syswrite(MDM, "$out\r", length("$out\r")); # Write AT command sleep(1); # Time for consideration sysread(MDM, $in, 1000); # Receive reaction of modem $debug && print STDERR "$0: $in\n"; # Print in debug-mode } close(MDM); sub getSIMPIN { # Retrieve 'simpin' Keychain entry my $pin; open CMD, "/usr/bin/security 2>&1 >/dev/null find-generic-password -ga simpin |" or die "$0: /usr/bin/security: some problem occurred or command not found\n"; while() { chomp; # We only want the pin s/password: // && ($pin = $_) && last; } close(CMD); $pin =~ s/"//g; # Remove dblquotes return $pin; }