Verify file was sftp'd via bash script


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Verify file was sftp'd via bash script
# 1  
Old 02-11-2011
Verify file was sftp'd via bash script

Hello Experts,

I have a script that that transfers a file (via sftp) and it works fine but we ran into a snag where the target server asked for the ssh key and the script didn't know what to do. I want to add some logic to this script that at least sends an email that it didn't complete as expected or didn't transfer the file.
Code:
#!/usr/local/bin/expect --
spawn sftp support@target_server.com
expect "password: "
send "support\r"
expect "sftp>"
send "put /home/root/filename.txt . \r"
expect "sftp> "
send "quit\r"

I'm testing it now on our network and get the following results:

./test_ftp_script
spawn sftp support@target_server.com
Connecting to target_server.com...
The authenticity of host 'target_server.com (10.101.21.34)' can't be established.
RSA key fingerprint is ce:5d:35:c6:57:8d:f5:d5:45:ac:b4:a5:3e:22:e6:f1.
Are you sure you want to continue connecting (yes/no)? support
Please type 'yes' or 'no': /home/root/filename.txt .
Please type 'yes' or 'no': [server:/home/root/]#

Note: I have altered the names on the script via cutting and pasting, so there may be an extra period, etc.. here or there

Thanks!
# 2  
Old 02-11-2011
so it sounds like from what you show'd is that the IP has multiple hosts attached and you may not get the same host (ssh key) in response every time. But since the known_hosts know's part of the information it is trying to auth and fails. And now wants a response to add it to the known_hosts file. Do you have control of the target server(s)?
# 3  
Old 02-11-2011
No, I don't have control of that server. The issue shown above is an anomaly that just happened. I showed it for an example. When I sftp a file is there a return code (especially in that situation above) that I can set a flag on to send me an email that it didn't complete correctly.
It looks to me that - and I'm not sure - but the above error would return ok; it started and it finished. That script is called from another script that does a lot of other things, so that script doesn't hang on that.

So, I guess what I'm asking is -- Is there a way to verify that the file was sent to the other server?

Bonus question:
In the script above, can you put an "if" statement in the code to detect, with expect say - "(yes/no)?" if true answer "yes" if false continue with the log in?

---------- Post updated at 02:24 PM ---------- Previous update was at 02:19 PM ----------

Also, I'm pretty sure you're right about multiple servers attaching to the same Aix-guy. Fast overview, that server is used in our D.R. process and we upload current info on backup tapes etc to it everyday.
# 4  
Old 02-11-2011
hmmmm ok lets take the bonus first yes you can:
Code:
if {$count < 0} {
   puts "count is less than zero"
} elseif {$count > 0 {
   puts "count is greater than zero"
} else {
   puts "count is equal to zero"
}

Now for the result of the sftp
I would be looking to output all the results to a separate log file.
the expect call would be done from a shell script and it could call
a expect script file.
so something like
Code:
  if [ some test in the shell ]
  else
     echo "Setting password for $x"
      expect -f ./chpass.exp $x $1 $2 $3 > $gd_log
     res=`egrep "Enter the new password again:" $gd_log |wc -l`
  fi

so we call a expect script that outputs to a logger the we start
checking the log for conditions we want and make decisions.

As for sftp there is a list of commands that is allows:
Code:
cd path
lcd path
chgrp grp path
chmod mode path
chown own path
help
df [-hi] [path]
lls [ls-options [path]]
ln oldpath newpath
lmkdir path
lpwd
ls [path]
lumask umask
mkdir path
progress
put local-path [remote-path]
pwd
exit
quit
rename oldpath newpath
rmdir path
rm path
symlink oldpath newpath
version
!command
!
?

when I did a sftp here is what returned.
Code:
sftp> put test.log
Uploading test.log to /test.log
test.log                                      100%   69KB  68.9KB/s   00:01

then a second call for during the ftp for this
Code:
sftp> !sum test.log
15078    69 test.log

then parse the log for the line and check the sum size of the file you sent
and be sure they are the same.

But if windows servers then this may not work.
This User Gave Thanks to aix-guy For This Post:
# 5  
Old 02-13-2011
Perhaps ssh-keyscan could be used? It looks like ssh-keyscan is a way of quickly collecting host keys of a number of ssh servers without having to manually log into each one. You could then compare this list with the existing list of known hosts to ensure the host has not changed its key. If it has the program can die nicely before the sftp command is even executed.

Also, lftp is a script-able ftp client that talks sftp. Using this you could remove the requirement for expect.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to verify each line in input for specific pattern

In the bash below the out put of a process is written to input. What I am trying to do is read each line in the input and verify/check it for specific text (there are always 6 lines for each file and the specific text for each line is in the description). There will always be 6 lines in each... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Bash to verify and validate file header and data type

The below bash is a file validation check executed that will verify the correct header count of 10 and the correct data type in each field of the tab-delimited file. The key has the data type of each field in it. My real data has 58 headers in it but only the header and next row need to be... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Bash - sftp simple script problem

Hello, when running the scripts below I am not getting message bb2. Can you please help? #!/bin/bash TLOG=/tmp/bb/amatest.log FTPRESULTS=/tmp/bb/amlist export TLOG FTPRESULTS >$TLOG >$FTPRESULTS echo bb1 sftp -oPort=2222 XXXXXXXXXXXXX@sftp.userssedi.com <<EOF cd... (5 Replies)
Discussion started by: biljana
5 Replies

4. Shell Programming and Scripting

Bash Script: Send files to SFTP using Expect

I have to send few gzipped files from local server to SFTP server. My Server Info Distributor ID: Ubuntu Description: Ubuntu 12.04.4 LTS Release: 12.04 Codename: precise Created a bash script and could able to send files to sftp, but i want to send email if transfer is successful. ... (1 Reply)
Discussion started by: krux_rap
1 Replies

5. Homework & Coursework Questions

Bash Script for Dice Game; Issue with if...else loop to verify user guess is within range

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have written a script for a dice game that: (1) tells user that each of the 2 die are 6 sided (Spots=6); (2)... (3 Replies)
Discussion started by: LaurenRose
3 Replies

6. Shell Programming and Scripting

SFTP-how to log individual sftp command error while executing shell script

Hi, I have situation where i need to automate transferring 10000+ files using sftp. while read line do if ; then echo "-mput /home/student/Desktop/folder/$line/* /cygdrive/e/folder/$line/">>sftpCommand.txt fi done< files.txt sftp -b sftpCommand.txt stu@192.168.2.1 The above... (1 Reply)
Discussion started by: noobrobot
1 Replies

7. Shell Programming and Scripting

Bash Script verify user input is not empty and is equal to a value

I need to create a script that has a user enter a value. I want to verify that the value is either 1,2, or 3. If it is not then I want them to try entering it again. I am using a while loop to force them to retry. I am able to test the input against 1,2, and 3, but when I test agains an... (4 Replies)
Discussion started by: spartiati
4 Replies

8. UNIX for Dummies Questions & Answers

Verify a flat file with UNIX function or script.

I re-post my question to seek your help and critique. I was required to verify a very large and tab-delimited file, named 'MyFile'. 1. The each line in 'MyFile' has 7 columns, and that the values in the 5th column are integers. I need to use shell functions (and standard LINUX/UNIX filters) to... (1 Reply)
Discussion started by: duke0001
1 Replies

9. Shell Programming and Scripting

sftp mget where file doesn't exist BASH

I have a script that is working: #!/bin/bash sftp user@domain.com <<EOF cd somedir mget *.csv quit EOF but on a crontab I want to only pull newer files, so I want to do something like: while read ls current dir local file != true do mget that new file but I'm not sure the syntax... (2 Replies)
Discussion started by: unclecameron
2 Replies

10. Shell Programming and Scripting

sftp bash script

Hello everybody! I am writing a script to connect to a sftp server from a client (both are Linux machines) and pull some files from there. The script is supposed to check if the files are already put on the server and if yes take them. If not, check again in 10 minutes, let's say. Everything... (2 Replies)
Discussion started by: abcd
2 Replies
Login or Register to Ask a Question