Solved

sftp connection error checking

Posted on 2014-12-08
10
999 Views
Last Modified: 2014-12-15
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
Comment
Question by:Peter Kuczynski
10 Comments
 
LVL 5

Expert Comment

by:Dilip Patidar
ID: 40488167
Hello Peter,

Might be below solution will help you!!!

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

Thanks,
Dilip Patel
0
 
LVL 11

Accepted Solution

by:
andreas earned 500 total points
ID: 40488295
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
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40489344
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40489557
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
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40490565
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
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40492088
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
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40493799
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
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40494147
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
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40494466
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
 
LVL 1

Author Closing Comment

by:Peter Kuczynski
ID: 40501463
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question