?
Solved

sftp connection error checking

Posted on 2014-12-08
10
Medium Priority
?
1,500 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…
Suggested Courses

777 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