Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,
How can I write a Perl script that dialup to my ISP and
connecnt my computer with Internet?Just like
/usr/doc/ppp-2.*.*/scripts/ppp-on and ppp-on-dialer have
done for us?
Thanx.

Replies are listed 'Best First'.
RE: Can Perl dialup?
by Jouke (Curate) on Oct 20, 2000 at 13:54 UTC
    Take a look at This node where someone asked the same question. It'll probably answer your question
    update: Take a look especially at the answer with the link to pTkPPP

    Jouke Visser, Perl 'Adept'
Re: Can Perl dialup?
by BastardOperator (Monk) on Oct 20, 2000 at 16:38 UTC
    This might get you started....or it might not :)
    #!/usr/bin/perl # Rename this file to ppp.pl $number = "9999999"; #the phone number to dial $username = "USER"; #the username to send (not required) $password = "USER"; #the password (not required) $init = "AT&F\r"; #the init string for the modem $nameprompt = "sername:"; #the string searching for the username + prompt $passprompt = "assword:"; #the string searching for the password + prompt $pppprompt = ">"; #the prompt for the ppp command $pppon = "ppp\r"; #the command to send to the dialup for ppp $redials = 3; #number of times to redial if busy $debug = 0; #set to 1 to see lots of info $tries = 3; #number of tries to reinitialize the modem $sleeptime = 3; #amount of time to wait for responses $dials=0; $looper=0; while($looper == 0) { print "Initializing Modem\n"; &ModemSetup; #run the ModemSetup subroutine print "Dialing $number\n"; &Dial; #run the Dial subroutine print "Connecting...\n"; $looper=&Loop; #store the return value of Loop sleep $sleeptime; #sleep to make sure that ppp } #has started on remote end print "Starting PPPD\n"; #start up ppp locally system "/usr/sbin/pppd debug lock modem crtscts /dev/modem 115200 noip +default defaultroute"; #This subroutine reads from the modmem 1 character at a time and appen +ds #it to a string. The string is then parsed to see if it matches one of + the #categories below. When the program has sent the string to start ppp +on #the remote computer it returns 1, if it doesn't get through everythin +g, and #it reads NO CARRIER from the modem, then it returns 0. sub Loop { $loop=0; $in=""; #initialize our variables, to get rid $in2=""; #begin our super while loop, while it can read from modem, it goes while(sysread(MODEM, $in2, 1)) { if(($in2 eq "\n") or ($in2 eq "\r")) { $in=""; } $in=$in.$in2; if($debug == 1) { print "$in"; #prints out everything read from modem $loop++; } #busy or no carrier errors, return 0, so we can redial. if(($in =~ /BUSY/) or ($in =~ /CARRIER/)) { $dials++; sleep $sleeptime; print "Error : $in\n"; sleep $sleeptime; return 0; } #hey we're connected if( $in =~ /CONNECT (\d*)/) { $speeds=$1; } #run the username stuff, if username is set to USER, then get user to #input it. if( $in =~ /$nameprompt/) { $resetu=0; #if we get hung up on we need to reset if($username eq "USER")#the username. { print "Please enter your Username: "; $username=<STDIN>; chomp $username; #gets rid of newline char $resetu=1; } print "\nSending $username...\n"; $username=$username."\r"; syswrite(MODEM, $username, length($username)); $in=""; if($resetu==1) { $username="USER"; } } #password stuff, not very good yet if(($in =~ /$passprompt/)) { $reset=0; if($password eq "USER") { print "Please enter your Password: "; system "stty -echo"; $password=<STDIN>; chomp $password; system "stty echo"; print "\n"; $reset=1; } $passwords=$password."\r"; print "Sending Password...\n"; syswrite(MODEM, $passwords, length($passwords)); $in=""; if($reset == 1) { $password="USER"; } } #launch the ppp on the remote end, then get out of this damn while loo +p if( $in =~ /$pppprompt/) { print "Sending $pppon\n"; syswrite(MODEM, $pppon, length($pppon)); last; } } print "Connected at $speeds\n"; return 1; } #set up modem, run all the initialization we need. sub ModemSetup { if (-f "/var/lock/LCK..modem") #this checks to see if the mode +m is { #locked, if it is exit. print "Hey the modem is locked!\n"; open LOCKFILE, "</var/lock/LCK..modem"; $pid=<LOCKFILE>; $running = system "ps h $pid"; if($running ne "No processes available.") { print "running is $running\n"; print "Hey there is something still using the modem\n"; exit; } print "There doesn't seem to be anything really using the mode +m\n"; print "Should we rm -f the file, and try anyway?\n"; $answer=<STDIN>; if($answer =~ /y/i) { system "rm -f /var/lock/LCK..modem"; } else { exit; } } $in = ""; #gets rid of the warning open MODEM, "+>/dev/modem"; #opens the modem system "setserial /dev/modem spd_vhi"; #runs setserial, set spe +ed to 115200 syswrite(MODEM, $init, length($init)); #sends the init sleep 5; sysread(MODEM, $in, 10); #reads the results of init #if not okay, then retry for a few times $n=0; while($in =~ /NO/) { print "Something Is Wrong\n"; sleep $sleeptime; $n++; print "Error $in\n"; syswrite(MODEM, $init, length($init)); if($n > $tries) { exit; } sysread(MODEM, $in, 10); } } #this is the subroutine that actually sends the dialing string to the +modem. sub Dial { $dials++; if($dials > $redials) { print "Failed after $dials redials\n"; exit; } print "Dial attempt #$dials\n"; $numberw = "ATDT $number\r\n"; #set up dialing string. syswrite(MODEM, $numberw, length($numberw)); #dial }
      Many thanks for your code at first.
      I try it on my Linux.When it began waitting for character
      ">",it stopped.So Not enter PPP mode.
      My ISP just need username and password.Does it need extra
      actions to launch remote PPP between sending password and
      launching local pppd?
        the ISP should give some type of prompt after your password has been sent and verified, try using minicom or the like to dial in manually and see what the prompt is if not ">". You could always just comment the "if($in =~ $pppprompt) {" and closing brace and see what happens, although if you do decide to do that, I'd suggest you put a small sleep in between the sending of the password and the $pppon.
How Do You Dial Up In Windows?
by Anonymous Monk on Nov 03, 2000 at 23:27 UTC
    I am also trying to dial up, but I am stuck with Windows instead of Linux. The current application uses $result = `RasDial95 $number` to launch Microsoft Dial Up Networking. It works a few times before the dialer locks up. I currently working on getting the module WIN32::RASE to work but it also uses Microsoft Dialup networking.
RE: Can Perl dialup?
by Jouke (Curate) on Oct 20, 2000 at 12:26 UTC
    It's sure possible. Do you want it on a windows-platform or linux/unix?

    Jouke Visser, Perl 'Adept'
      On Linux instead of "ppp-on" and "ppp-on-dialer".
RE: Can Perl dialup?
by Jonathan (Curate) on Oct 20, 2000 at 13:43 UTC
    LWP:Useragent documents this and gives example code. Check the section on proxy connections.

    Update Apologies Misread question - flame at will :-)

    "We are all prompted by the same motives, all deceived by the same fallacies, all animated by hope, obstructed by danger, entangled by desire, and seduced by pleasure." - Samuel Johnson
      Don't think so. LWP::Useragent allows you to fetch documents from the WWW, not dialling an ISP...

      Jouke Visser, Perl 'Adept'