Solved

sftp connection error checking

Posted on 2014-12-08
10
825 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 29

Expert Comment

by:MikeOM_DBA
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:Peter Kuczynski
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
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.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now