Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upload multiple files with same sftp connection #13

Closed
iotgopher opened this issue Dec 28, 2020 · 4 comments · Fixed by #14
Closed

upload multiple files with same sftp connection #13

iotgopher opened this issue Dec 28, 2020 · 4 comments · Fixed by #14

Comments

@iotgopher
Copy link

I noticed that when trying to upload in sequence multiple files (calling the Upload function for one file after the other) at some point Linux hosts will kick you out with a "administratively prohibited: open failed".
Is there a way around it?

@melbahja
Copy link
Owner

Hi,

Can you put some code to reproduce the issue? that's will be helpful
Thanks

@iotgopher
Copy link
Author

iotgopher commented Dec 29, 2020

Sure! Thanks for your reply...
Here is an example trying to upload 15 files one after the other. The linux host will kick you out after the 10th one.
I think the problem lies in the fact that each Upload function opens an independent sftp session. Linux hosts have a protection against letting the same user opening so many connections in a short amount of time.
I tried putting a sleep of even 10 seconds between each call but they would still too many.
It would be nice a MultiUpload function where you pass a []strings for source files and a single string for destination folder. Everything would then happens within the same sftp session in sequence.

package main

import (
	"fmt"
	"net"
	"os"

	"github.com/melbahja/goph"
	"github.com/pkg/sftp"
	"golang.org/x/crypto/ssh"
)

var (
	err    error
	auth   goph.Auth
	client *goph.Client
	addr   string
	user   string
	port   uint
	pass   string
	cmd    string
	sftpc  *sftp.Client
)

func VerifyHost(host string, remote net.Addr, key ssh.PublicKey) error {
	hostFound, err := goph.CheckKnownHost(host, remote, key, "")

	if hostFound && err != nil {
		return err
	}

	if hostFound && err == nil {
		return nil
	}

	return goph.AddKnownHost(host, remote, key, "")
}

func main() {

	var err error

	addr = "10.149.10.10"
	user = "username"
	port = 22
	pass = "password"

	auth = goph.Password(pass)

	// open connection
	client, err = goph.NewConn(&goph.Config{
		User:     user,
		Addr:     addr,
		Port:     port,
		Auth:     auth,
		Callback: VerifyHost,
	})
	if err != nil {
		panic(err)
	}

	// close connection
	defer client.Close()

	// commands

	home, err := os.UserHomeDir()
	if err != nil {
		fmt.Println(err.Error())
	}

	err = client.Upload(home+"\\somefile.tar", "/tmp/file1.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file2.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file3.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file4.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file5.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file6.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file7.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file8.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file9.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file10.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file11.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file12.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file13.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file14.tar")
	err = client.Upload(home+"\\somefile.tar", "/tmp/file15.tar")

}

melbahja added a commit that referenced this issue Dec 29, 2020
@melbahja melbahja mentioned this issue Dec 29, 2020
melbahja added a commit that referenced this issue Dec 29, 2020
Fix for #13
@melbahja
Copy link
Owner

v1.2.1 released with fix for this try it and feel free to reopen this issue.

@iotgopher
Copy link
Author

Thanks it seems to be working fine! I'll do some more tests and reopen if needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants