Here is a script I collected that does dialout and sets up a ppp connection (not written by me)
#!/usr/bin/perl # Rename this file to ppp.pl $number = "5627953"; #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 pr +ompt $passprompt = "assword:"; #the string searching for the password pr +ompt $pppprompt = ">"; #the prompt for the ppp command $pppon = "ppp\r"; #the command to send to the dialup for pp +p $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 mode +m $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 noipdefault + 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 pppo +n #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 w +hile loop 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 mod +em 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 speed + to 115200 syswrite( MODEM, $init, length($init) ); #sends the init sleep 1; 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 }
In reply to Re: Perl for dialling out
by zentara
in thread Perl for dialling out
by SteveB
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |