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
|
---|
8 | if {$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 | } \
|
---|
13 | else \
|
---|
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
|
---|
24 | if {![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
|
---|
31 | log_user 0
|
---|
32 |
|
---|
33 | set 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
|
---|
40 | spawn sftp -oNumberOfPasswordPrompts=1 -oPort=$sftp_port -oStrictHostKeyChecking=no $sftp_user
|
---|
41 |
|
---|
42 | # Expect ssh password prompt
|
---|
43 | expect \
|
---|
44 | {
|
---|
45 | timeout {puts "Error: ssh failed to connect to server."; exit 3}
|
---|
46 | "password: $"
|
---|
47 | }
|
---|
48 |
|
---|
49 | # Log in
|
---|
50 | send "$sftp_pw\r"
|
---|
51 |
|
---|
52 | # Expect sftp command prompt
|
---|
53 | expect \
|
---|
54 | {
|
---|
55 | timeout {puts "Error: ssh login failed."; exit 4}
|
---|
56 | "sftp> $"
|
---|
57 | }
|
---|
58 |
|
---|
59 | # Move to desired upload path
|
---|
60 | send "cd $upload_path\r"
|
---|
61 |
|
---|
62 | # Expect sftp command prompt again
|
---|
63 | expect \
|
---|
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
|
---|
70 | send "put \"$report_file\" \"$upload_name\"\r"
|
---|
71 |
|
---|
72 | # Expect at least one progress report, e.g. "100%"
|
---|
73 | expect \
|
---|
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
|
---|
81 | expect \
|
---|
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
|
---|
93 | send "bye\r"
|
---|
94 |
|
---|
95 | # Expect EOF
|
---|
96 | expect \
|
---|
97 | {
|
---|
98 | timeout {puts "Error: sftp logout failed."; exit 8}
|
---|
99 | eof
|
---|
100 | }
|
---|
101 |
|
---|
102 | exit 0
|
---|