source: Validate External Links/val_expect_sftp.txt@ 1158

Last change on this file since 1158 was 1064, checked in by iritscen, 7 years ago

Committing my wiki link validation script, as it is reasonably mature now.

File size: 2.4 KB
Line 
1#!/usr/bin/expect -f
2
3# val_expect_sftp.txt by Iritscen
4# A script for driving "sftp" to upload the HTML report that is produced by the
5# Validate External Links script.
6
7# Check arguments
8if {$argc != 6} \
9{
10 puts "Error: You need to pass me the path to the file to upload, the SFTP user name, the SFTP password, the SFTP port, the upload destination, and the name of the uploaded file, in that order."
11 exit 1
12} \
13else \
14{
15 set report_file "[lindex $argv 0]"
16 set sftp_user "[lindex $argv 1]"
17 set sftp_pw "[lindex $argv 2]"
18 set sftp_port "[lindex $argv 3]"
19 set upload_path "[lindex $argv 4]"
20 set upload_name "[lindex $argv 5]"
21}
22
23# Check that file to be uploaded exists
24if {![info exists report_file]} \
25{
26 puts "Error: There isn't a file at path $report_file."
27 exit 2
28} \
29
30# Turn off output except for 'puts' commands
31log_user 0
32
33set timeout 10
34
35# Spawn sftp with:
36# - a single password try, so that ssh is less likely to get hung up at login
37# - the custom port used by oni2.net for SFTP
38# - a disabled ssh_knownhosts file, so sftp doesn't ask us whether we want to
39# accept the certificate
40spawn sftp -oNumberOfPasswordPrompts=1 -oPort=$sftp_port -oStrictHostKeyChecking=no $sftp_user
41
42# Expect ssh password prompt
43expect \
44{
45 timeout {puts "Error: ssh failed to connect to server."; exit 3}
46 "password: $"
47}
48
49# Log in
50send "$sftp_pw\r"
51
52# Expect sftp command prompt
53expect \
54{
55 timeout {puts "Error: ssh login failed."; exit 4}
56 "sftp> $"
57}
58
59# Move to desired upload path
60send "cd $upload_path\r"
61
62# Expect sftp command prompt again
63expect \
64{
65 timeout {puts "Error: Could not enter directory of upload path $upload_path."; exit 5}
66 "sftp> $"
67}
68
69# Send the desired file with the supplied new name for it
70send "put \"$report_file\" \"$upload_name\"\r"
71
72# Expect at least one progress report, e.g. "100%"
73expect \
74{
75 timeout {puts "Error: sftp 'put' failed to run."; exit 6}
76 -re "\[0-9\]%"
77}
78
79# Expect possible additional progress reports and expect the sftp command
80# prompt when done uploading
81expect \
82{
83 # Continue waiting while progress reports are happening
84 -re "\[0-9\]%" exp_continue
85 # Otherwise, if nothing happens for three seconds, there must be a
86 # problem
87 timeout {puts "Error: sftp 'put' failed to complete."; exit 7}
88 # We're done when we get the sftp command line again
89 "sftp> $"
90}
91
92# Log out
93send "bye\r"
94
95# Expect EOF
96expect \
97{
98 timeout {puts "Error: sftp logout failed."; exit 8}
99 eof
100}
101
102exit 0
Note: See TracBrowser for help on using the repository browser.