#!/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 # SET THE EMAIL COMMANDS FOR COMPLETION AND ERRORS set UECUEmail "bputtkammer@uecu.coop" set AlertUECUEmail "bputtkammer@uecu.coop" set EmailCommand "echo 'Subject:Problem with Experian CB Upload\nExperian CB Upload did not complete' | sendmail -f reply@domain.com AlertUECUEmail" set DoneCommand "echo 'Subject:Done with Experian CB Upload\nExperian CB Upload completed!' | sendmail -f reply@domain.com UECUEmail" # =============================================================================== # EXPERIAN CONTACT INFO - TECHNICAL ISSUES OR TO RELOAD A DELETED FILE # =============================================================================== # DATEADD FUNCTIONS AND VARIABLES INCLUDED BELOW source /udadmin/expectscripts/DateFormats.exp source /udadmin/expectscripts/ExpectConfig.exp # =============================================================================== # SET THE TIMEOUT TO 10 MINUTES set timeout 600 set EmailText "\n" set LineBreak "\n" set FileSize 0 set FileSizeMB 0 set FTPCount "0" set SFTPServer "205.174.34.41" set SFTPUser "edmx6176" set SFTPPassword "123456789" set BackupSFTPServer "stm.experian.com" set AccountDirectory "$DefaultLiveAccountPath" set ScriptName "cb-experian.exp" if {$AccountDirectory == "/eom/TEST"} { set EmailSubject "Experian CB Upload - TEST" set LoginScript "/udadmin/expectscripts/TrainUsers.exp" } else { set EmailSubject "Experian CB Upload - LIVE" set LoginScript "/udadmin/expectscripts/LiveUsers.exp" } # TURN ON LOGGING - SET LOG VARIABLE NAME set LogFileName "/udadmin/expectlogs/CB-ExperianUpload-SFTP-$FileDate.log" log_file $LogFileName # CHANGE FILE PERMISSIONS AND OWNER ON LOG FILE system "chmod 777 $LogFileName" system "chown expect $LogFileName" # 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 # =============================================================================== # SHELL DOWN expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } ":" { send -- "SH\rdate\rpwd\r" system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" append EmailText $expect_out(0,string)$LineBreak } } # SHELL DOWN expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "$AccountDirectory" { send -- "cd $AccountDirectory/CPU.CRREPORT/SEND\rls -ltr\r" system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" append EmailText $expect_out(0,string)$LineBreak } } # COPY UECUEXP AND RENAME AS TEXT FILE expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "$AccountDirectory/CPU.CRREPORT" { send -- "ls -ltr | tail\r" # AFTER DONE TESTING REMOVE COMMENTS FROM TIME STAMP FILE NAME send -- "cp -p UECUEXP_$CurrentYear$CurrentMonth$CurrentDay NANGX.txt\r" # AFTER DONE TESTING REMOVE COMMENTS FROM TIME STAMP FILE NAME send -- "ls -ltr\r" send -- "\r" send -- "du -ks $AccountDirectory/CPU.CRREPORT/SEND/NANGX.txt\r" send -- "\r" send -- "pwd\r" append EmailText $expect_out(0,string)$LineBreak } } # SET THE TIMEOUT TO 10 SECONDS set timeout 10 # GET ap.positive FILE SIZE expect { timeout { send -- "\r" send -- "pwd\r" } -re "(\[ 0-9]+) $AccountDirectory/CPU.CRREPORT" { set FileSize $expect_out(1,string) set FileSize [string trimleft $FileSize " "] set FileSize [string trimright $FileSize " "] set DUFileSize $FileSize system "echo \"\nUECUEXP FileSize0=$FileSize\n\" >> $LogFileName" send -- "\r" send -- "pwd\r" } } # SET THE TIMEOUT TO 10 MINUTES set timeout 600 # START SECURE FTP CONNECTION expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "$AccountDirectory/CPU.CRREPORT" { send -- "ls -ltr | tail\r" send -- "\r" send -- "sftp -oPort=22 $SFTPUser@$SFTPServer\r" append EmailText $expect_out(0,string)$LineBreak } } # START SECURE FTP CONNECTION expect { timeout { system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "Are you sure you want to continue connecting" { send -- "yes\r" append EmailText $expect_out(0,string)$LineBreak exp_continue } "Service not available, remote server has closed connection" { sleep 30 set FTPCount [expr $FTPCount + 1] if {$FTPCount < 5} { send -- "bye\rsftp -oPort=22 $SFTPUser@$BackupSFTPServer\r" append EmailText $expect_out(0,string)$LineBreak exp_continue } else { system "echo \"Service not available, remote server has closed connection\nExpect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand sleep 360000 } exp_continue } "Host Interface Not Available" { sleep 30 set FTPCount [expr $FTPCount + 1] if {$FTPCount < 5} { send -- "bye\rsftp -oPort=22 $SFTPUser@$BackupSFTPServer\r" append EmailText $expect_out(0,string)$LineBreak exp_continue } else { system "echo \"Host Interface Not Available\nExpect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand sleep 360000 } exp_continue } "Permission denied" { sleep 30 set FTPCount [expr $FTPCount + 1] if {$FTPCount < 5} { send -- "bye\rsftp -oPort=22 $SFTPUser@$BackupSFTPServer\r" append EmailText $expect_out(0,string)$LineBreak exp_continue } else { system "echo \"Permission denied - please try again\nExpect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand sleep 360000 } exp_continue } "Connection refused" { sleep 30 set FTPCount [expr $FTPCount + 1] if {$FTPCount < 5} { send -- "bye\rsftp -oPort=22 $SFTPUser@$BackupSFTPServer\r" append EmailText $expect_out(0,string)$LineBreak exp_continue } else { system "echo \"Connection refused\nExpect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand sleep 360000 } exp_continue } "Connection closed" { sleep 30 set FTPCount [expr $FTPCount + 1] if {$FTPCount < 5} { send -- "bye\rsftp -oPort=22 $SFTPUser@$BackupSFTPServer\r" append EmailText $expect_out(0,string)$LineBreak exp_continue } else { system "echo \"Connection closed\nExpect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand sleep 360000 } exp_continue } "Login failed" { sleep 30 set FTPCount [expr $FTPCount + 1] if {$FTPCount < 5} { send -- "bye\rsftp -oPort=22 $SFTPUser@$SFTPServer\r" append EmailText $expect_out(0,string)$LineBreak exp_continue } else { system "echo \"Login failed\nExpect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand sleep 360000 } exp_continue } "Enter passphrase for key " { send -- "nvzkdcz1\r" append EmailText $expect_out(0,string)$LineBreak exp_continue } "s password" { sleep 30 set FTPCount [expr $FTPCount + 1] if {$FTPCount < 5} { send -- "bye\rsftp -oPort=22 $SFTPUser@$SFTPServer\r" append EmailText $expect_out(0,string)$LineBreak exp_continue } else { system "echo \"Login failed - password incorrect!\nExpect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand sleep 360000 } exp_continue } "Connected to $SFTPServer" { send -- "lpwd\r" send -- "lcd $AccountDirectory/CPU.CRREPORT/SEND\r" send -- "ls -ltr\r" append EmailText $expect_out(0,string)$LineBreak } "Connected to $BackupSFTPServer" { send -- "lpwd\r" send -- "lcd $AccountDirectory/CPU.CRREPORT/SEND\r" send -- "ls -ltr\r" append EmailText $expect_out(0,string)$LineBreak } } # START SECURE FTP CONNECTION expect { timeout { system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "from_xpn" { send -- "dir\r" send -- "cd to_xpn\r" send -- "pwd\r" append EmailText $expect_out(0,string)$LineBreak } "to_xpn" { send -- "dir\r" send -- "cd to_xpn\r" send -- "pwd\r" append EmailText $expect_out(0,string)$LineBreak } } # START SECURE FTP CONNECTION expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "Remote working directory" { send -- "put NANGX.txt\r" system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" append EmailText $expect_out(0,string)$LineBreak } } sleep 10 # SET THE TIMEOUT TO 15 MINUTES set timeout 900 # READ FILE SIZE IN BYTES - READ TRANSMISSION TIME IN SECONDS expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "No such file or directory" { send -- "put NANGX.txt\r" system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" } "Uploading NANGX" { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" append EmailText $expect_out(0,string)$LineBreak sleep 75 exp_continue } "Received disconnect from " { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" send -- "exit\rEND\rOFF\r" exit } "Connection closed" { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" send -- "exit\rEND\rOFF\r" exit } -re "100% (\[ 0-9]+)MB" { set FileSize $expect_out(1,string) set FileSize [string trimleft $FileSize " "] set FileSize [string trimright $FileSize " "] set FileComplete "100%" system "echo \"\nUECUEXP FileSize1=$FileSize\n\" >> $LogFileName" exp_continue } -re "100% (\[ 0-9]+)" { set FileSize $expect_out(1,string) set FileSize [string trimleft $FileSize " "] set FileSize [string trimright $FileSize " "] set FileComplete "100%" system "echo \"\nUECUEXP FileSize2=$FileSize\n\" >> $LogFileName" exp_continue } "sftp> " { sleep 7 send -- "dir\r" send -- "\r" send -- "bye\r" send -- "\r" send -- "pwd\r" system "echo \"\nsftp expect UECUEXP\n\" >> $LogFileName" system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" append EmailText $expect_out(0,string)$LineBreak } } # SET THE TIMEOUT TO 5 MINUTES set timeout 300 # END SECURE FTP CONNECTION expect { timeout { system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } "$AccountDirectory" { send -- "cd $AccountDirectory/CPU.CRREPORT/SEND\rls -ltr\r" send -- "exit\rDATE\r" system "echo \"\n$\(date\),[timestamp]\n\" >> $LogFileName" append EmailText $expect_out(0,string)$LineBreak } } # =============================================================================== # LOG OFF expect { timeout { system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } " $CurrentYear" { send -- "OFF\r" append EmailText $expect_out(0,string)$LineBreak } } # FINISHED expect { timeout { system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress 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 { system "echo \"Expect Statements:$EmailText\nLOG file is saved in /udadmin/expectlogs directory\n\n\" > /udadmin/expectfiles/email_message.txt" set EmailCommand "( cat /udadmin/expectfiles/email_message.txt ; uuencode $LogFileName \"LOG-FILE.txt\" ) | mailx -s \"Problem with $EmailSubject\" -r $ReplyToAddress AlertUECUEmail" system $EmailCommand system "echo 'ERROR, $ScriptName, $FileDate' >> /udadmin/expectlogs/DailyVerify$FileDate.txt" sleep 360000 } eof } # if {$FileSize > 0} {set FileSizeKB [expr {round($FileSize * .0009765625)}]} if {$FileSize > 0} {set FileSizeMB [expr {round($FileSize * .00095367431640625)}]} # CALCULATE TOTAL TIME OF RUNNING SCRIPT source /udadmin/expectscripts/total_time.exp source /udadmin/expectscripts/server_time.exp set DoneCommand "echo 'Subject:Done with $EmailSubject\nExperian CB Upload completed! It ran on $AccountDirectory\n\nFile Upload Information:\n$FileSize KB - $FileSizeMB MB - uploaded (DU File Size: $DUFileSize KB)\n\n$EmailSubject took, $TotalTime\nStart Time:$StartTimeDisplay\nEnd Time:$EndTimeDisplay\n\n$ServerTime\n\n$EmailNotes' | sendmail -f reply@domain.com UECUEmail" system $DoneCommand # UPDATE DAILY VERIFY FILE WITH SCRIPT COMPLETED system "echo 'COMPLETED, $ScriptName, $FileDate, $TotalTime' >> /udadmin/expectlogs/DailyVerify$FileDate.txt"