• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1870
  • Last Modified:

sftp connection error checking

I would like to add a if else statement to this script to send an email alert if it cannot connect to the sftp server,

#!/bin/sh
sftp user1@192.168.100.100 <<EOF

#if success continue on, else fail and email administrator

#a succesful connection appears below and is not part of the script...
SSH Server supporting SFTP and SCP
Connected to 192.168.100.100.
sftp>
#next the script proceeds with uploading data...

lcd /directory1
!pwd >> /var/log/sftplog/sftp_$(date '+%d').log
!ls >> /var/log/sftplog/sftp_$(date '+%d').log
mput *.xml
!mv /directory1/*.xml /directory1/archive/

#EOF

When the script fails, its usually because either the sftp server it connects to is not accepting connections, or the network port shuts down at the firewall.

I would like for the script to have an if/else statment. if success, continue on processing the reminder of the script.

else fail, and mail an alert SFTP is down!
0
Peter Kuczynski
Asked:
Peter Kuczynski
1 Solution
 
Dilip PatidarCommented:
Hello Peter,

Might be below solution will help you!!!

http://joshua14.homelinux.org/blog/?p=1764

Thanks,
Dilip Patel
0
 
andreasSystem AdminCommented:
You might try the following.

create a textfile in your filewhere the script sits with a single line. This line should contain:

bye

then use the following command as an example how to code your script.

sftp -b testfilewithbyeinside.txt user@server  >/dev/null 2>&1 && echo OK  || echo ERROR

Open in new window


or if you want if with IF THEN ELSE

if sftp -b textfilewithbye.txt user@server  >/dev/null 2>&1 ; then 
    echo OK, continue with commands to upload
else 
   echo ERROR send a mail
fi

Open in new window

0
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
Dilip, thank you, I will use this on other projects I'm sure but is more complex then I need for this.

andreas, let me see how this will work, and I'll ping you back, thank you!

#!/bin/sh
sftp user1@192.168.100.100 <<EOF

!if sftp -b textfilewithbye.txt user@server  >/dev/null 2>&1 ; then
    echo OK, continue with commands to upload
else
   echo ERROR send a mail
fi

lcd /directory1
!pwd >> /var/log/sftplog/sftp_$(date '+%d').log
!ls >> /var/log/sftplog/sftp_$(date '+%d').log
mput *.xml
!mv /directory1/*.xml /directory1/archive/

#EOF
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
MikeOM_DBACommented:
Use "expect"
Here is an example (not tested):
#!/usr/bin/expect

set timeout 600
set user   [lindex $argv 0]
set pass   [lindex $argv 1]

spawn date
expect "==>"
spawn sftp $user@myremote.server.com

expect {
        default {exit 1}
        "password:"
}
send "$pass\n"
expect {
        default {exit 1}
        -re "failed|invalid password|Permission denied" {exit 2}
        "Connection closed" {exit 1}
        Connected  {}
        "sftp>" {}
        timeout {exit 1}
}

send "lcd /directory1\n"
expect {
        default {exit 1}
        "sftp>"
}
send "pwd\n"
expect {
        default {exit 1}
        "sftp>"
}
send "mput *.xml\n"
expect {
        default {exit 1}
        "sftp>"
}

send "bye\n"
exit 0

Open in new window

Warning: Execute 'command' in local shell and/or "mput" may not be available on some sftp distributions.
0
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
I tried expect and it didnt work for me, I'd rather not use it.
This seems to be working, please check my work and thanks.
First we test the connection, if it fails we send an email. If it's ok, then we keep going.
Should I place test of the connection at the end of the script instead?

#!/bin/sh

sftp -b test.txt user1@192.168.100.100

if [[ $? -ne 0 ]]; then
  mail -s "ALERT! SIT-SFTP-01 login failure" admin@somewhere.com
fi

sftp user1@192.168.100.100 <<EOF

lcd /directory1
!pwd >> /var/log/sftplog/sftp_$(date '+%d').log
!ls >> /var/log/sftplog/sftp_$(date '+%d').log
mput *.xml
!mv /directory1/*.xml /directory1/archive/

#EOF
0
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
Also there may a simpler way to achieve this.
the error log which is tracked by the same cron script, writes if a connection was unsuccessful to a rotating log file

Below is a failed connection:
ssh: connect to host 192.168.100.100 port 22: Connection timed out
Couldn't read packet: Connection reset by peer

If I create another script which runs say every 15 minutes to tail the last line of this log for that day, and it it reads
"Couldn't read packet: Connection reset by peer" it fires off an email

The cron entry which generates the error logs looks like this:

*/10 * * * * /scripts/sftp.sh >> /error.log.$(date +\%d) 2>&1

So this script would need to look for the log file for todays date.
0
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
The script runs but it always says the SFTP server is down, so it's not error checking, not sure here.

I think the issue is with this....

if [[ $? -ne 0 ]]; then <<<It's the return code from the most recently executed command. By convention 0 is a successful exit and non-zero indicates some kind of error. Maybe I'll change 0 to a 1 ?

if [[ $? -ne 0 ]]; then
  mail -s "ALERT! SIT-SFTP-01 login failure" admin@somewhere.com
fi
0
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
Getting too many arguments line 8 ?

#!/bin/bash

STATUS="SSH Server supporting SFTP and SCP"

CURR=$(sftp -b test.txt username@192.168.100.100)

if
[ $STATUS -eq $CURR ]
then

mail -s "ALERT! SERVER-01 login failure" admin@somewhere.com

fi
0
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
Here's what I came up with, seems to work, please let me know your thoughts...

#!/bin/sh

sftp -b test.txt username@192.168.100.100  >/dev/null 2>&1

if [[ $? != 0 ]]; then
  echo "Connection failed" >>/error.log.$(date +\%d) 2>&1
  mail -s "ALERT! SFTP-01 connection failed" admin@somewhere.com
  exit 1
else
  echo "Connection complete." >>/error.log.$(date +\%d) 2>&1
fi
0
 
Peter KuczynskiLead Cloud EngineerAuthor Commented:
The solution I went with is below and working.

#!/bin/sh

sftp -b test.txt username@192.168.100.100  >/dev/null 2>&1

if [[ $? != 0 ]]; then
  echo "Connection failed" >>/error.log.$(date +\%d) 2>&1
  mail -s "ALERT! SFTP-01 connection failed" admin@somewhere.com
  exit 1
else
  echo "Connection complete." >>/error.log.$(date +\%d) 2>&1
fi
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now