Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7636259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:37:47+00:00 2026-05-31T07:37:47+00:00

Sometimes when connecting to a remote SSH server I get Connection Closed By *IP*;

  • 0

Sometimes when connecting to a remote SSH server I get Connection Closed By *IP*; Couldn't read packet: Connection reset by peer. But after trying one or two more times it connects properly.

This presents a problem with a few bash scripts I use to automatically upload my archived backups to the SSH server, like so;

export SSHPASS=$sshpassword
sshpass -e sftp -oBatchMode=no -b - root@$sshaddress << !
   cd $remotefolder
   put $backupfolder/Qt_$date.sql.gz
   bye
!

How can I have this part loop until it actually properly connects?

UPDATE: (Solution)

RETVAL=1
while [ $RETVAL -ne 0 ]
do
export SSHPASS=$sshpassword
sshpass -e sftp -oBatchMode=no -b - root@$sshaddress << !
   cd $remotefolder
   put $backupfolder/Qt_$date.tgz
   bye
!
RETVAL=$?
[ $RETVAL -eq 0 ] && echo Success
[ $RETVAL -ne 0 ] && echo Failure
done
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:37:48+00:00Added an answer on May 31, 2026 at 7:37 am

    I am not a shell scripting expert, but I would check the return value of sshpass when it exits.

    From man ssh:

    ssh exits with the exit status of the remote command or
    with 255 if an error occurred.
    

    From man sshpath:

    Return Values

    As with any other program, sshpass returns 0 on success. In case of
    failure, the following return codes are used:

    1. Invalid command line argument
    2. Conflicting arguments given
    3. General runtime error
    4. Unrecognized response from ssh (parse error)
    5. Invalid/incorrect password
    6. Host public key is unknown. sshpass exits without confirming the new key.

    In addition, ssh might be complaining about a man in the middle
    attack. This complaint does not go to the tty. In other words, even
    with sshpass, the error message from ssh is printed to standard error.
    In such a case ssh’s return code is reported back. This is typically
    an unimaginative (and non-informative) “255” for all error cases.

    So try to run the command, and check its return value. If the return value was not 0 (for SUCCESS) then try again. Repeat using a while loop until you succeed.

    Sidenote: why are you using sshpass instead of public-key (passwordless) authentication? It is more secure (you don’t have to write down your password) and makes logging in via regular ssh as easy as ssh username@host.

    There’s even an easy tool to set it up: ssh-copy-id.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm connecting to a remote sql server instance with the following connection string Network
I normally use gVim for editing, but I sometimes use vim when remotely connecting
Sometimes I get Oracle connection problems because I can't figure out which tnsnames.ora file
I am trying to get some data from remote server. To achieve this I
We use ERPConnect in our ASP.NET application but sometimes we get some error messages
So I've been using CURL to get some portions of remote images....but then depending
I get the message Launch error: Failed to connect to remote VM. Connection timed
After connecting to server, I run some commands on server and then trying to
I'm currently using JSch as the library for connecting to my remote server. In
I'm building a download manager in python for fun, and sometimes the connection to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.