grab script
#!/cnmsn/bin/expect
#  Paul Nyffenegger v1.1, Jun 6, 1996, all rights reserved
#  This script may be freely copied and altered so long as
#	the original source is noted. Blah, Blah, Blah, see 
#      the GNU license which holds and applies fully for
#      this software.
#
# VERSIONS
#    grab version 1.1
#    requires tcl version 6.4; not guarenteed bug free for other versions
#    requires expect version 3.x
# Thu Dec 17 07:33:06 CST 1998  ejh
#    converted to tcl8 and expect 5.28 ejh Dec 17 1998
#
# SYNTAX
#    grab [seed mask] [date1] [date2] [logfile] [ftp directory]
#
# ARGUMENTS:
#   (1) seednames mask for RETRIEVE
#   (2) date1   /*according to ultra-sheer software convention */
#   (3) date2
#   (4)	complete path name of log file
#   (5)	complete path name of directory to place file (ftp)
#
#   
# DESCRIPTION:
#   There is  more than one way to retrieve data automatically from 
#   a Quanterra system.  This script uses probably the most complicated,
#   that is to say the interactive RETRIEVE interface,
#   but the good points are that this method is fairly robust and 
#   returns diagnostic output in case of failure to retrieve data. 
#   Other users may want to explore using the finger software
#   loaded on new Quanterra systems, using different RETRIEVE commands, 
#   using ftp -n , or using an external ftp macro to finish the retrieval.
#
#   The script creates a local miniseed archive using the 'V' option,
#   rather than using kermit or UUCP to encode and transfer the file.
#   The file is then retrieved (and deleted from the DP's harddisk)
#   using standard ftp. Users may want to check to make sure archive and
#   ftp directories are the same as for their system. 
#
#   The current version of tcl/expect has problems with the ftp command,
#   especially on SUN systems running Solaris.  Therefore, I have
#   created a .netrc file instead to get around this problem. 
#   The original expect code for handling ftp prompts and messages
#   is commented out. On  SUN systems you can find information on 
#   .netrc using man netrc  . Search on 'spawn ftp' to find the 
#   pertinent section in this script.
#
#   Other users may wish to pass the station name as an argument.
#   For this version, the station name is hardcoded, and I have 
#   replaced the name and password with '%%%%%%%'
#
#   This script has been tested for 4 months on a Sun SPARC20.
#   The Quanterra DP it usually connects to is only about 100 ft
#   down the hallway, and is in the same internet domain. 
#
# BUGS:
#   No known bugs.
#   Bug reports may be sent to :
#         pauln@utig.ig.utexas.edu
#
#   The biggest problem with this script is that timeouts can occur 
#      depending on how you use it. For our use, the script is kicked off
#	in a shell wrapper by CRON, and sometimes timeouts occur
#	because the host machine does a lot of I/O, which has a
#	higher priority than the shell wrapper. Also timeouts
#	can occur if the network is very busy. Therefore, users
#	may wish to adjust the timeout values to more appropriate
#	values for their situations.
#
# THINGS TO DO:
#	check out debug options:
#	write a basic man page:
#      expand (combine) with finger scripts:
#	tk front end for optional interactive use
# debug set 0 to 1 below and debug info will appear in file

set debug_enable 0
set error_exit  0
set verbose_logging 0

proc debug_on {} {
	global debug_enable
	if {$debug_enable} { exp_internal -f grab_debug_log 1 }
}

proc debug_off {} {
	global debug_enable
	if {$debug_enable} { exp_internal 0 }
}

# error1 - procedure to output a flag upon bad user name or password
proc error1 {uxpid} {
	 trap exit SIGINT
	 close
	 send_user "GRAB: Terminated: ERROR 1 \n"
	 exit 1
}

# error2 - procedure to terminate connection if machines get stuck
proc error2 {} {
	 trap exit SIGINT
	 close
	 send_user "GRAB: Terminated: ERROR 2 \n"
	 exit 2
}

# error3 - procedure to terminate if problem with building seed volume
proc error3 {} {
	 trap exit SIGINT
	 close
	 send_user "GRAB: Terminated: ERROR 3 \n"
	 exit 3
}

# error4 - procedure to terminate ftp if no login prompt received befor timeout
proc error4 {} {
	 trap exit SIGINT
	 close
	 send_user "GRAB: Terminated: ERROR 4 \n"
	 exit 4
}

# error5 - procedure to terminate if timeout in ftp session
proc error5 {} {
	 trap exit SIGINT
	 close
	 send_user "GRAB: Terminated: ERROR 5 \n"
	 exit 5
}

#Kill logging to tty
log_user 0


#set whoami to enviromental variable $USER
#catch {set whoami [exec whoami]}
#catch {set whoami [$env(user)]}
set whoami "seismic\@wiggles"


if [llength $argv]!=6 {
	send_user "usage: grab \[station FQDN] \[channel mask] \[date1] \[date2] \[logfile] \[ftp directory]\n"
	exit 5
}

# set variables from arg list date1, date2 
set station [lindex $argv 0]
set smask [lindex $argv 1]
set date1 [lindex $argv 2]
set date2 [lindex $argv 3]
set logfile [lindex $argv 4]
set destdir [lindex $argv 5]

#Start up logging to file
if {[string match $logfile "user"] == 1} then { 
	send_user "GRAB logging to tty\n"
   } else {
	log_file $logfile
   }

set tstamp [exec date]
send_user "\n\nGRAB: $tstamp  BY:  $whoami \n"
send_user "GRAB: $smask $date1 $date2 \n"

# telnet to $station
trap exit SIGINT
send_user "GRAB: telnet $station \n"
set def_timeout 180
set scn_timeout	600
set ftp_timeout	4800

set timeout $def_timeout

set uxpid [spawn telnet $station ]

expect {
	timeout {
		send_user "GRAB: Never saw prompt for user name \n"
		error1 $uxpid 
	}
	"User name?: " {}
}
send "seed\r"

expect {
	timeout {
		send_user "GRAB: Never saw prompt for password \n"
		error1 $uxpid 
	}
	"Who? " {
		send_user "GRAB: User Name not accepted \n"
		error2 
	}    
	"Password: " {}
}
send "data\r"

expect {
	timeout {
		send_user "GRAB: Communications stuck \n"
		error2 
	}
	"Welcome!" {
		if { $verbose_logging } { send_user "GRAB: User logged in \n" }
	}
	"Better luck next time" {
		send_user "GRAB: Connection closed by foreign host \n"
		error2 
	}
}
expect {
	"TOO MANY NETWORK USERS" {
		send_user "GRAB: TOO MANY NETWORK USERS Connection closed by foreign host \n"
		error2 
	}
	"*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*" {
		 send "GRAB automated retrieval by $whoami\r"
	}
}         
debug_on
expect {
	timeout {
		send_user "GRAB: Stuck in telnet waiting to input my name\n"
		error2 
	}
	"Command? " {
		send "v $smask $date1 $date2 \r"
		if {$verbose_logging} {send_user "GRAB: sent v $smask $date1 $date2 to Q80\n"}
	}
}
debug_off
set timeout $scn_timeout
debug_on

while {1} {
	expect {
		timeout {
			send_user "GRAB: Stuck in telnet while creating archive file\n"
			set error_exit 1
			break
		}
		"search interval" {
			if {$verbose_logging} {send_user "GRAB: Q80 searching for data\n"}
		}
		"scanning available channels" {
			if {$verbose_logging} {send_user "GRAB: Q80 scanning for data\n"}
		}
		"entire request not available. search stops:" {
			if {$verbose_logging} {send_user "GRAB: entire request not available\n"}
		}
		"total records selected." {
			if {$verbose_logging} {send_user "GRAB: some data available\n"}
		}
		"Time window not available*Command?" {
			send "q\r"
			send_user "GRAB: Time window not available \n"
			set error_exit 1
			break
		}
		"mask (e.g. B??)" { #need to reinput channel
			set error_exit 1
			break
		}
		"UTC starting date for VBB data:\r\n  yy/mm/dd hh:mm:ss\r\n? " { # need to reinput starting date
			send_user "GRAB: Begin date ($date1) is wrong\n"
			set error_exit 1
			break
		}
		"UTC ending date for VBB data:\r\n  yy/mm/dd hh:mm:ss\r\n? " { # need to reinput starting date
			send_user "GRAB: End date ($date2) is wrong\n"
			set error_exit 1
			break
		}
		"...going once" {
			send_user "GRAB: error somewhere got going once\n"
			set error_exit 1
			break
		}
		"\"*.*\"" {
			set arcfile [string range $expect_out(0,string) 1 12]
			#send_user "GRAB: Q80 sent the string $expect_out(0,string)\n"
			send_user "GRAB: Seed Archive File: $arcfile \n"
		}
		"closing local file" {
			if {$verbose_logging} {send_user "GRAB: Seed Archive File closed\n"}
			break;
		}
	}
}
if { $error_exit != 0 } {
	error3
}
# end of while
debug_off
expect {
	timeout {
		send_user "GRAB: Stuck in telnet \n"
		error2 
	}
	"Command? " {
		send "q\r"
	}
}
set timeout $def_timeout
expect {
	timeout {
		send_user "GRAB: Stuck in telnet \n"
		error2 
	}
	"...normal termination*foreign host." {
		send_user "GRAB: Telnet terminated \n"
	}
}

close
	 
# ftp to $station, retrieve document, set to lowest common denominator ftp mode
#	could use ftp -n hkt < " {
	   send "cd /h0/dat\n"
	}
}

expect { 
	timeout {
		send_user "GRAB: ftp stuck during cd to data directory; abort \n"
		error5
	}
#	"^\[45]"
	"ftp> " {
	   send "lcd $destdir \n"
	   send_user "GRAB: lcd $destdir\n"
	}
}

expect { 
	timeout {
		send_user "GRAB: ftp stuck after local chande directory; abort \n"
		error5
	}
	"ftp> " {
	   send "binary\n"
	}
}

set timeout $ftp_timeout
expect { 
	timeout {
		send_user "GRAB: ftp stuck after binary mode change; abort \n"
		error5
	}
	"ftp> " {
		send_user "GRAB: sending ftp get $arcfile \n"
		send "get $arcfile\n"
		# send_error "$arcfile\n"
	}
}

expect { 
	timeout {
		send_user "GRAB: ftp stuck during data transfer; abort \n"
		error5
	}
	"ftp> " {
		send "del $arcfile\n"
		send_user "GRAB: del $arcfile\n"
	}
}

expect { 
	timeout {
		send_user "GRAB: ftp stuck deleting remote data file; abort \n"
		error5
	}
	"ftp> " {
		send "bye\n"
		if {$verbose_logging} {send_user "GRAB: ftp session closed \n"}
	}
}

send_user "GRAB: succeeded \n"

close 
exit 0

Back