-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhuawei-4g-linux.pl
executable file
·158 lines (129 loc) · 3.73 KB
/
huawei-4g-linux.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env perl
#
# Huawei 4g modem E3276 (and possibly others) connect script
# by Janhouse (Janis Jansons) - janis.jansons@janhouse.lv
#
# This has been tested with Latvian based operator LMT.
# It should work for many other modems and networks.
#
#
use 5.010;
use warnings;
use strict;
use Getopt::Long;
use POSIX "WNOHANG";
use IO::File;
use IO::Select;
STDERR->autoflush(1);
my ($help, $pin, $fh);
my $device="/dev/ttyUSB0";
my $interface="wwan0";
my $apn="internet.lmt.lv";
$SIG{'INT'} = \&close_handler;
$SIG{'TERM'} = \&close_handler;
GetOptions(
"help" => \$help,
"interface=s" => \$interface,
"device=s" => \$device,
"pin=s" => \$pin,
"apn=s" => \$apn,
) or exit 1;
err("Huawei 4g modem (E3276 and possibly others) script by Janhouse (Janis Jansons).\n");
show_help() if defined $help;
do{ err("No device found at $device (Try specifying it in --device= argument)");exit 1 } if not -e $device and not -z $device;
do{ err("Device is not readable by the effective user. (Try using root.)"); exit 1 } if not -r $device;
do{ err("Device is not writable by the effective user. (Try using root.)"); exit 1 } if not -w $device;
err("Opening device: $device");
open( $fh, "+>".$device) or do{ err("Can't read serial port : $!"); exit 1 };
do{ err("$device is not a tty."); exit 1 } if not -t $fh;
my $response;
$response=get_command_response($fh, "ATZ", "OK");
err(format_output($response));
$response=get_command_response($fh, "AT+CPIN=$pin", "OK") if defined $pin;
err(format_output($response));
$response=get_command_response($fh, "ATQ0 V1 E1 S0=0", "OK");
err(format_output($response));
$response=get_command_response($fh, "AT^NDISDUP=1,1,\"$apn\"", "^NDISSTAT");
err(format_output($response));
sleep(5);
$response=get_command_response($fh, "AT^DHCP?", "^DHCP:");
err(format_output($response));
if($response=~/DHCP: (.*?)$/ism){
# Run dhcpcd:
system("dhcpcd $interface");
# Or get it from the modem:
#err("DHCP");
my @ips=map { join(".", unpack("C4", pack("L", hex))) } split /,/, $1;
#use Data::Dumper;
#print Dumper @ips;
#err($ips);
#"ifconfig $interface $ips[0] netmask $ips[1] up";
=pod
10.40.26.58,
255.255.255.252,
10.40.26.57,
10.40.26.57,
212.93.97.145,
212.93.96.2,
0.0.32.67,
0.0.32.67
=cut
}
err("\nYou can close this script now or you can keep it open to monitor the link quality.\n");
while(1){
err("Link quality:");
$response=get_command_response($fh, "AT+CSQ", "+CSQ:");
err(format_output($response));
$response=get_command_response($fh, "AT+COPS?", "+COPS:");
err(format_output($response));
sleep(5);
}
sub format_output {
my $string=shift;
$string=~s/(\r\n){2,}/\r\n/mg;
$string=~s/^/> /mg;
$string=~s/\s+$//;
return $string;
}
sub get_command_response {
my $fh=shift;
my $command=shift;
my $ok=shift;
my $s = IO::Select->new();
$s->add($fh);
print $fh $command."\r\n";
my $string;
while(1){
if(my @handles = $s->can_read(5)){
my $h=shift(@handles);
my $s;
my $bytes_read = sysread($h, $s, 1024);
$string.=$s;
return $string if defined $ok and $string =~ m/\s.?$ok\s/i;
next;
}else{
return $string;
}
return;
}
}
sub show_help {
print(<<EOF);
Usage: $0
-h/--help Show this help screen.
-p/--pin=0000 PIN code for the SIM card.
-d/--device=/dev/ttyUSB0 Device to be used.
-i/--interface=wwan0 Network interface to be used.
-a/--apn=internet.lmt.lv Access point name
EOF
exit 0;
}
sub err {
print STDERR shift."\n";
}
sub close_handler {
print "\nClosing...\n";
close($fh);
exit 0;
}
1;