Simple PPP dialing script


#!/usr/bin/perl

# ppp-start - start a PPP connection
# (C) Copyright Greg Ercolano 2003. This software is public domain.
# Tested for redhat9.0

$tty   = "/dev/cua1";             # tty
$baud  = 57600;
$dns   = "DNS_ADDRESS";
$login = "LOGIN_NAME\r";          # quote '@' sign!
$pass  = "PASSWORD\r";
$init  = "ate0\r";
$dial  = "atdtPHONE_NUMBER\r";
$abort = "NO DIALTONE|NO CARRIER|BUSY|ERROR";

# The following byzantine 'stty' hex data was the output of 'stty -g < /dev/cua0' 
# while minicom(1) had the tty open, a 'cheat' to get the right flags ;)
# You can do the same if these numbers don't work for you.
#
$stty  = "stty ".
         "1:0:80000cbf:0:3:1c:7f:15:4:5:1:0:11:13:1a:0:".
         "12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0 < $tty";

# READ TTY UNTIL EXPECTED STRING, OR ABORT STRING
#     Returns -1 on error.
#
sub ExpectAbort($$)
{
    my $char;
    my ($expect,$abort) = @_;
    my $in = "";
    print STDERR "--- expect($expect) abort($abort): ";
    while ( 1 )
    {
        $char = "";
        if ( sysread(TTY, $char, 1) != 1 )
	    { next; }
	if ( $char eq "\r" ) { $char = "[CR]"; }
	if ( $char eq "\n" ) { $char = "[LF]"; }
	$in .= $char;
	print STDERR $char;
	if ( $in =~ m/$expect/ ) { print STDERR " **OK**\n"; return(0); }
	if ( $in =~ m/$abort/ ) { print STDERR " **ABORT**\n"; return(-1); }
    }
}

# SEND A STRING TO THE TTY
sub Send($)
{
    print STDERR "$_[0]\n";
    syswrite(TTY, $_[0], length($_[0]));
    # print TTY $_[0];
}

# SETUP DNS
#     Only configure one resolver; using a secondary DNS 
#     is silly; if the primary ain't workin, secondary takes
#     forever to respond, so why bother!
#
open(RES, ">/etc/resolv.conf");
print RES "nameserver $dns\n";
close(RES);

# DISABLE ANY ETHERNET
system("ifconfig eth0 down");

# DISABLE ALL SERVER STUFF
system("killall rushd xinetd portmap sshd httpd sendmail");

# OPEN
print STDERR "--- open $tty: ";
unless ( open(TTY, "+>$tty") )
    { print STDERR "ERROR: $!\n"; exit(1); }
print STDERR "OK\n";

# STTY
print STDERR "--- stty: ";
system($stty);
print STDERR "OK\n";

# MODEM INIT, DIAL
print STDERR "--- init: "; Send($init); 
if ( ExpectAbort("OK", $abort) < 0 ) { exit(1); }

print STDERR "--- dial: "; Send($dial); 
if ( ExpectAbort("Login: ", $abort) < 0 ) { exit(1); }
sleep(3);
print STDERR "--- login: "; Send($login);

if ( ExpectAbort("Password: ", $abort) < 0 ) { exit(1); }
sleep(3);
print STDERR "--- Password: "; Send($pass);

if ( ExpectAbort("PPP", "Invalid login") < 0 ) { exit(1); }

system("pppd lock modem crtscts defaultroute $tty $baud");

sleep(3);
close(TTY);

print STDERR "\n--- Use 'killall pppd' to hang up. ".
             "Tailing /var/log/messages:\n";
exec("tail -f /var/log/messages");