Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sftp connection error checking

Posted on 2014-12-08
10
Medium Priority
?
1,712 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 12

Accepted Solution

by:
andreas earned 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

636 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