#!/usr/local/bin/expect -f # ## PROPERTY OF BRANT PUTTKAMMER, PLEASE CONTACT ME IF YOU HAVE QUESTIONS ## PLEASE USE CAUTION WHEN RUNNING THIS PROGRAM ON YOUR SYSTEM set force_conservative 1 ;# set to 1 to force conservative mode even if ;# script wasn't run conservatively originally if {$force_conservative} { set send_slow {1 .1} proc send {ignore arg} { sleep .1 exp_send -s -- $arg } } # BEGIN EXPECT SCRIPT BELOW #* PROPERTY OF BRANT PUTTKAMMER, PLEASE CONTACT ME IF YOU HAVE QUESTIONS # SET THE EMAIL COMMANDS FOR COMPLETION AND ERRORS set AlertUECUEmail "bputtkammer@uecu.coop" set EmailCommand "echo 'Subject:Problem with CU43-CHK\nCU43 did not complete' | sendmail AlertUECUEmail" set DoneCommand "echo 'Subject:Done with CU43-CHK\nCU43 completed!' | sendmail UECUEmail" # DATEADD FUNCTIONS AND VARIABLES INCLUDED BELOW source /udadmin/expectscripts/DateFormats.exp source /udadmin/expectscripts/ExpectConfig.exp # TURN ON LOGGING - SET LOG VARIABLE NAME set LogFileName "/udadmin/expectlogs/INITIATE-CHK-$FileDate.log" log_file $LogFileName # CHANGE FILE PERMISSIONS AND OWNER ON LOG FILE system "chmod 777 $LogFileName" system "chown expect $LogFileName" # SET THE TIMEOUT TO 10 MINUTES set timeout 600 set EmailText "\n" set LineBreak "\n" set RemainingChecks 0 set EmailNotes "" set FailureNotes "" set AccountDirectory "$DefaultLiveAccountPath" set ScriptName "initiate-chk.exp" if {$AccountDirectory == "/eom/MEA.EOM"} { set EmailSubject "CU43-Check - TEST" set SubjectNotes "Done with CU43-Check" set LoginScript "/udadmin/expectscripts/TrainUsers.exp" } else { set EmailSubject "CU43-Check - LIVE" set SubjectNotes "Done with CU43-Check" set LoginScript "/udadmin/expectscripts/LiveUsers.exp" } # SET THE CHECK PRINTER TERMINAL NUMBERS UP IN THE LIST # ADD NEW TERMINALS OR CHANGE THEM HERE - YOU MUST UPDATE THE NEXT PART WITH THE CHECK NUMBERS TOO, SEPARATE EACH ENTRY WITH A SPACE set Terminals {18 24 92 140 142 143} # SET THE CHECK PRINTER NUMBERS TO BE USED IN ADMIN22 WHEN SWITCHING BETWEEN THEM, SEPARATE EACH ENTRY WITH A SPACE set Printers {20 19 15 16 17 18} set PrinterNames {AV-OPS-LCHKS BACK-OFFICE-CHECKS ACCTING-CHK EP-LASER-CHECKS CR-LASER-CHECKS WBY-LASER-CHECKS} set PrinterStringEnabled "\n" set PrinterStringSkipped "" # ESTABLISH THE ACCOUNT YOU WANT TO LOGIN TO source $LoginScript # RUN LOGIN SCRIPT, BASED ON THE ACCOUNT YOU WISH TO ACCESS source /udadmin/expectscripts/login.exp # RUN LPSTAT ONCE LOGGED ON expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } ":" { send -- "LPSTAT\r" append EmailText $expect_out(0,string)$LineBreak } } for {set counter [expr [llength $Printers]-1]} {$counter>=0} {incr counter -1} { # GET THE CURRENT TERMINAL NUMBER FROM LIST ABOVE set TerminalNumber [lindex $Terminals $counter] # GET THE CURRENT CHECK PRINTER NUMBER FROM LIST ABOVE set PrinterNumber [lindex $Printers $counter] # GET THE CURRENT CHECK PRINTER NAME FROM LIST ABOVE set PrinterName [lindex $PrinterNames $counter] # SET OUR TERMINAL TO THE CORRECT CHECK PRINTER NUMBER IN ADMIN22 expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } ":" { send -- "ADMIN22\r" append EmailText $expect_out(0,string)$LineBreak } } # ENTER THROUGH FIRST ADMIN22 QUESTION FOR OUR TERMINAL expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "Enter Terminal" { send -- "\r" system "echo \"\nTERMINAL PROMPT RETURNED THROUGH\n\" >> $LogFileName" append EmailText $expect_out(0,string)$LineBreak append EmailText "TERMINAL PROMPT RETURNED THROUGH$LineBreak" } } # NOW GO TO THE CHECK PRINTER FIELD # ENTER THE CHECK PRINTER NUMBER IN THE CHECK PRINTER FIELD # SAVE THE SETTINGS IN ADMIN22 expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "Enter Field Number" { send -- "3\r" send -- "$PrinterNumber\r" send -- "S\r" system "echo \"\nPRINTER NUMBER=$PrinterNumber\n\" >> $LogFileName" append EmailText $expect_out(0,string)$LineBreak append EmailText "PRINTER NUMBER=$PrinterNumber $LineBreak" } } # NOW END OUT OF ADMIN22 expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "Enter Terminal" { send -- "END\rEND\rDATE\r" append EmailText $expect_out(0,string)$LineBreak } } # RUN THE NEXT CHECK NUMBER PROGRAM expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } ":" { send -- "RUN BP NEXT.CHECK.NUM\r" append EmailText $expect_out(0,string)$LineBreak } } # ENTER CHECK PRINTER TERMINAL NUMBER OR END expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "ENTER CHECK PRINTER TERMINAL NUMBER OR END" { send -- "$TerminalNumber\r" system "echo \"\nPRINTER NUMBER=$TerminalNumber\n\" >> $LogFileName" append EmailText $expect_out(0,string)$LineBreak append EmailText "PRINTER NUMBER=$TerminalNumber $LineBreak" } } # SET THE TIMEOUT TO 30 SECONDS set timeout 30 # MATCH THE NEXT CHECK NUMBER TEXT SO WE CAN CAPTURE IT TO A VARIABLE expect { timeout { send -- "END\rEXP.ME\r" } -re "CHECK NUMBER (\[0-9]+) IS THE NEXT CHECK NUMBER" { # THIS TAKES THE OUTPUT OF THE NEXT CHECK NUMBER AND SETS IT TO A VARIABLE FOR USE LATER set NextCheckNumber $expect_out(1,string) append EmailText $expect_out(0,string)$LineBreak } } # MATCH THE LAST CHECK NUMBER TEXT SO WE CAN CAPTURE IT TO A VARIABLE expect { timeout { set LastCheckNumber [expr $NextCheckNumber+100] system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" } -re "AND NUMBER (\[0-9]+) IS THE LAST CHECK NUMBER" { # THIS TAKES THE OUTPUT OF THE LAST CHECK NUMBER AND SETS IT TO A VARIABLE FOR USE LATER set LastCheckNumber $expect_out(1,string) append EmailText $expect_out(0,string)$LineBreak } } # DO WE NEED TO RESET CHECK NUMBERS AT ANY POINT, MONTHLY, OR CAN THEY JUST KEEP INCREASING # set LastCheckNumber [expr $NextCheckNumber+100] # CALCULATE HOW MANY CHECKS ARE LEFT BEFORE LAST CHECK IS PRINTED set RemainingChecks [expr $LastCheckNumber-$NextCheckNumber] if {$RemainingChecks < 0} { append FailureNotes "Printer Name=$PrinterName, Printer Number=$PrinterNumber currently has $RemainingChecks checks remaining! The printer was skipped because the starting ($NextCheckNumber) and ending ($LastCheckNumber) check numbers are incorrect! Manually initiate the printer using FSP.\n" send -- "END\rEXP.ME\r" } elseif {$RemainingChecks < 20} { append EmailNotes "Printer Name=$PrinterName, Printer Number=$PrinterNumber currently has $RemainingChecks checks remaining!\n" } else { } # SET THE TIMEOUT TO 7 MINUTES set timeout 420 # INITIATE THE CHECK PRINTER expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "YOUR EXPECT SCRIPT HAS ESCAPED" { send -- "END\rEXP.ME\r" append EmailText $expect_out(0,string)$LineBreak } ":" { send -- "CU43\r" append EmailText $expect_out(0,string)$LineBreak } } # Enter FIRST usable CHECK expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "YOUR EXPECT SCRIPT HAS ESCAPED" { send -- "END\rEXP.ME\r" # append PrinterStringSkipped "$PrinterNumber$PrinterName " append EmailText $expect_out(0,string)$LineBreak } "This printer is currently running" { # WHAT DO YOU WANT TO DO IF THE PRINTER IS ALREADY ENABLED? send -- "END\rEXP.ME\r" # append PrinterStringSkipped "Printer Currently Running: Printer Name=$PrinterName, Printer Number=$PrinterNumber\n" append EmailText $expect_out(0,string)$LineBreak } "Enter FIRST usable CHECK" { send -- "$NextCheckNumber\r" append EmailText $expect_out(0,string)$LineBreak } } # VERIFY THAT THIS IS THE FIRST CHECK NUMBER expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "YOUR EXPECT SCRIPT HAS ESCAPED" { send -- "END\rEXP.ME\r" append EmailText $expect_out(0,string)$LineBreak } "Number OUT OF SEQUENCE" { send -- "N\rEND\rEXP.ME\r" append EmailText $expect_out(0,string)$LineBreak } "Verify $NextCheckNumber is FIRST Number" { send -- "Y\r" append EmailText $expect_out(0,string)$LineBreak } } # ENTER LAST USABLE CHECK NUMBER expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "YOUR EXPECT SCRIPT HAS ESCAPED" { send -- "END\rEXP.ME\r" append EmailText $expect_out(0,string)$LineBreak } "Enter LAST usable CHECK " { send -- "$LastCheckNumber\r" append EmailText $expect_out(0,string)$LineBreak } } # VERIFY THAT THIS IS THE LAST CHECK NUMBER expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "YOUR EXPECT SCRIPT HAS ESCAPED" { send -- "END\rEXP.ME\r" append EmailText $expect_out(0,string)$LineBreak } "WHAT " { send -- "END\rEXP.ME\r" append EmailText $expect_out(0,string)$LineBreak } "Verify $LastCheckNumber is LAST Number" { send -- "Y\r" append EmailText $expect_out(0,string)$LineBreak } } # RETURN WHEN READY TO PROCEED expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "YOUR EXPECT SCRIPT HAS ESCAPED" { send -- "\rEND\rEXP.ME\rDATE\r" # append PrinterStringSkipped "Printer Name=$PrinterName, Printer Number=$PrinterNumber\n" append PrinterStringSkipped "Printer Currently Running: Printer Name=$PrinterName, Printer Number=$PrinterNumber\n" append EmailText $expect_out(0,string)$LineBreak } "ready to proceed" { append PrinterStringEnabled "Printer Name=$PrinterName, Printer Number=$PrinterNumber, First Check=$NextCheckNumber, Last Check=$LastCheckNumber\n" send -- "\rEND\rEND\rDATE\r" append EmailText $expect_out(0,string)$LineBreak } "to continue" { # append PrinterStringEnabled "Printer Name=$PrinterName, Printer Number=$PrinterNumber, First Check=$NextCheckNumber, Last Check=$LastCheckNumber\n" send -- "\rEND\rEND\rDATE\r" append EmailText $expect_out(0,string)$LineBreak } } } # LOG OFF expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } ":" { send -- "OFF\r" append EmailText $expect_out(0,string)$LineBreak } } # FINISHED expect { timeout { set EmailCommand "echo 'Subject:Problem with $EmailSubject\\nExpect Statements:$EmailText' | sendmail AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "Logging OFF" {append EmailText $expect_out(0,string)$LineBreak} "Thank you, " {append EmailText $expect_out(0,string)$LineBreak} } # ENDING SESSION expect { timeout { exit } eof } # CALCULATE TOTAL TIME OF RUNNING SCRIPT source /udadmin/expectscripts/total_time.exp source /udadmin/expectscripts/server_time.exp if {$EmailNotes != ""} { set EmailNotes "NOTICE! The following printer(s) are low on checks\n$EmailNotes\n\n" set SubjectNotes "Done with CU43-Check: ATTN" } else { } if {$PrinterStringSkipped != ""} { set PrinterStringSkipped "\nThe following printers were skipped,\n$PrinterStringSkipped\n\n" set SubjectNotes "Done with CU43-Check: FAILURE" } else { } if {$FailureNotes != ""} { set FailureNotes "NOTICE! The following printer(s) failed to initiate\n$FailureNotes\n\n" set SubjectNotes "Done with CU43-Check: FAILURE" } else { } set InitiateEmail "UECUEmail,MSRGroup" set DoneCommand "echo 'Subject:$SubjectNotes\nCU43 completed (check initiation)!\n\nThe following printers were enabled:\n$PrinterStringEnabled\n$PrinterStringSkipped\n$EmailNotes\n$FailureNotes\nCU43 took, $TotalTime\nStart Time:$StartTimeDisplay\nEnd Time:$EndTimeDisplay\n$ServerTime' | sendmail $InitiateEmail" system $DoneCommand if {$LoginCurrentHour > 11} { system "echo SERVER REBOOT HAPPENED, $CurrentYear$CurrentMonth$CurrentDay$CurrentHour$CurrentMinute$CurrentSecond >> /udadmin/expectfiles/overnight_log.txt" } else { } # UPDATE DAILY VERIFY FILE WITH SCRIPT COMPLETED system "echo 'COMPLETED, $ScriptName, $FileDate, $TotalTime' >> /udadmin/expectlogs/DailyVerify$FileDate.txt"