#!/usr/bin/perl -s #################################################################### # netup v0.02 - connects using pppd. supports multiple phone numbers # and post-connection task automation. kills the # connection after $INACTIVITY minutes of inactivity. # # Copyright (c) 1996 Vipul Ved Prakash. All rights reserved. #################################################################### use POSIX; $PPPD = "/usr/sbin/pppd"; $IFCONFIG = "/sbin/ifconfig"; $LOG = "/var/log/netup-LOGFILE"; $MYNAME = $0; $PPPD_SCRIPT = "/root/.ppprc" unless defined $PPPD_SCRIPT; $MODEM = "/dev/modem"; $SPEED = "115200"; ## INITs for USRobotics Sportster 14.4 ## $INIT = "atq0v1&s0i3e1x4&a3&d2&b1&h1"; ## $INIT2 = "at&r2s10=255&n0&k1&m4&y3m0"; ## Inits for ZyXel Omni 28.8 # $INIT = "AT&F&Y2X0S10=250"; # $INIT2 = "ATM"; # $INIT = "AT&F&Y2X0S10=250L7"; ## Inits for GVC $INIT = "at&fzs10=250"; $INIT2 = "atmx0"; $INIT = "atz,s10=255s11=70"; $INIT2 = "at+ms=11,1,2400,24000"; $INIT3 = "atm0x0"; $EPBAX = ""; @PHONES = qw( 3339100 3368500 3366350 3334000 3334200 3367500 3367171 3347900 3349090 3349100 3360425 3367750 3366300 3334151 3334150 3334157 6246700 6246800 6246100 6246200 6246300 6246400 6246500 6246600 3367500 ); # fittest numbers @PHONES = qw( 3334200 3367750 3347900 6246300 3349090 3367171 3334000 3368500 3366350 3367500 3334156 3334151 3334154 3334152 3334155 ); $DIALTYPE = "T"; $WAIT = 70; $USERNAME = 'PUT-USERNAME-HERE' unless defined $USERNAME; $PASSWORD = 'PUT-PASSWORD-HERE' unless defined $PASSWORD; $INACTIVITY = "0" unless defined $INACTIVITY; $KEEPALIVE = "7"; # number of keepalive (?) packets sent on a ppp conection every minute. $PPP_INTERFACE = "ppp0"; # ppp interface $PPP_DEVICE = "/proc/net/dev"; # on linux systems. @COMMANDS = ("/usr/sbin/sendmail -q", "echo "); ## Die gracefully if another netup running and we are connected. ## Otherwise destroy the previous netup and start a new process. my @exec = grep {/$MYNAME/} `ps ax`; if ($#exec > 0) { if (&connected) { die "Another $MYNAME running, destroying this instance." } else { $exec[0] =~ s/^\s*(\d*).*/$1/; chomp $exec[0]; # now has pid of $MYNAME to be destroyed. print "Killing last instance of $MYNAME (PID: $exec[0])...\n"; system "kill -9 $exec[0]"; } } ## Read the password from the controlling terminal. if ($PASSWORD eq "") { print "Enter the password: "; $PASSWORD = ; chomp $PASSWORD; } ## Background myself. chdir '/'; fork && exit; POSIX::setsid; close 0,1,2; open (STDIN, ">>$LOG"); open (STDOUT, ">>$LOG"); open (STDERR, ">>$LOG"); ## The Dial Loop. Change phone numbers. DIALLOOP: srand ( time ); my $i = int ( (rand) * $#PHONES ); until (&connected) { $i < $#PHONES ? ($i+=1) : ($i=0); &new_script ($PHONES[$i]); &kill; `$PPPD`; sleep $WAIT; } print "** Connected on $PHONES[$i]\n\n"; &post_connect; if ( $persist ) { while ( &connected ) { sleep $WAIT * 2 } goto DIALLOOP; } #################################################################### sub new_script { my $phone = shift; open (SCRIPT, ">$PPPD_SCRIPT"); print SCRIPT "$MODEM\n"; print SCRIPT "$SPEED\n"; print SCRIPT "defaultroute\ncrtscts\n"; my $chat = &chat ($phone); print SCRIPT "$chat\n"; close SCRIPT; } #################################################################### sub kill { system "killall chat"; system "killall pppd"; } #################################################################### sub connected { if (grep /$PPP_INTERFACE/, `$IFCONFIG`) { return 1 } else { return undef; } } #################################################################### sub post_connect { grep `$_`, @COMMANDS; exit if $INACTIVITY == 0; my $newp; my $active = 1; my $sleep = 60 * $INACTIVITY; my $last = &packets; while ($active) { sleep $sleep; $newp = &packets; $last += $KEEPALIVE*$INACTIVITY; ($newp <= $last) ? ($active = 0) : ($last = $newp); } print "Killing the connection after $INACTIVITY minute(s) of inactivity.\n"; &kill; } #################################################################### sub chat { my $phone = shift; return "connect \"\/usr\/sbin\/chat -t$WAIT -v '' '$INIT' OK '$INIT2' OK '$INIT3' OK ATD$DIALTYPE$EPBAX$phone CONNECT \\c name: $USERNAME word: $PASSWORD 'PPP mode'" } #################################################################### sub packets { my ($packets) = grep /$PPP_INTERFACE/, `cat $PPP_DEVICE`; chomp $packets; $packets =~ s/$PPP_INTERFACE:\s+\S+\s+(\S*).*/$1/; return $packets += 0; }