LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-11-2017, 08:48 AM   #1
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Rep: Reputation: Disabled
Cannot sftp into Windows sftp


I am trying to sftp into a Windows sftp to transfer files. I am currently using Expect but I want to have some security. In a bash script I simply have these commands:

sftp username@hostname
cd dir3
lchdir /people/dir/occupation
put *.DAT

I commented each line, one by one to see why it will not execute correctly. What am I doing wrong?
 
Old 01-11-2017, 08:55 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
as you have not stated how you have your set up setup I can only guess.


where are your DAT files located in respect to where your script is located? Does not the sender have to be inside of the directory that has the files to be transferred to another system via ftp when logging in? if your script is not in that same directory then perhaps it is not able to put any DAT files to the other system due to that fact.

Your security comes from the user login process. The who can and cannot login and what they can and cannot access and do within that system they logged into via how the receiving system is setup to allow or not allow whatever user it allowed to log into it via ftp.

lchdir command

Expect ~ a help post for expect on logging in and sending using Expect. It might help.

Last edited by BW-userx; 01-11-2017 at 09:00 AM.
 
1 members found this post helpful.
Old 01-11-2017, 09:06 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,318
Blog Entries: 13

Rep: Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372Reputation: 4372
You say this is a bash script, therefore you can have the start of it be:
Code:
#!/bin/bash

set -xv
This enables verbose output from your script and will show you the results of these commands.

Also, I always state that what you can type in a command line, you can code in a bash script. The reverse is true, which is whatever you code in a bash script you can type in the command line. Therefore you should be able to type those commands cited in your first post, verbatim in the command line. Similarly you can type "set -xv" into the command line to enable verbose output, and then type "set +xv" to turn that back off.

This should help you to debug your script and commands.

If there is more to your script, consider posting it within [code][/code] tags.
 
1 members found this post helpful.
Old 01-11-2017, 09:16 AM   #4
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
I did run the script in verbose mode and I noticed that when the script is ran, it just logs in and the 'sftp>' prompt shows. So I would need to pass commands from the 'sftp' prompt in a script. Is this possible?

Last edited by trickydba; 01-11-2017 at 09:19 AM.
 
Old 01-11-2017, 09:16 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,500

Rep: Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110
Have you tried modifying the command from your other thread?

http://www.linuxquestions.org/questi...ml#post5652538
 
1 members found this post helpful.
Old 01-11-2017, 09:27 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by trickydba View Post
I did run the script in verbose mode and I noticed that when the script is ran, it just logs in and the 'sftp>' prompt shows. So I would need to pass commands from the 'sftp' prompt in a script. Is this possible?
I gave you a link to a script that logs in and sends files then exit ftp when done using Except like you stated you are using.

excerpt of that link post
Quote:
test.sh script in which sftp is used which is working fine is as follows

Code:

#!/usr/bin/expect
spawn sftp ftp_dl@10.132.249.50
expect "ftp_dl@10.132.249.50's password:"
send "database123#\n"
expect "sftp>"
send "put ZTE_*201505*\n"
expect "sftp>"
send "bye\n"
exit
~

Last edited by BW-userx; 01-11-2017 at 09:30 AM.
 
1 members found this post helpful.
Old 01-11-2017, 09:37 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,500

Rep: Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110
I believe the OP wants to switch from expect to bash only since they are using keys. As explained in the previously thread you can create a batch file or by piping your commands.
 
1 members found this post helpful.
Old 01-11-2017, 09:45 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by michaelk View Post
I believe the OP wants to switch from expect to bash only since they are using keys. As explained in the previously thread you can create a batch file or by piping your commands.
OIC

in that case in another excerpt from that post I linked to it says this:
Quote:
Note that sftp is specifically designed to prevent you from doing what you are trying to do because it is an extremely bad and insecure method of transferring files. That is why you are forced to use the expect brute-force password injection tool to inject passwords into it brute-force. It would be better to cooperate with the way sftp is designed to work, and use passwordless keys.
@ OP

or

Secure FTP using Windows batch script

Last edited by BW-userx; 01-11-2017 at 09:51 AM.
 
1 members found this post helpful.
Old 01-11-2017, 10:07 AM   #9
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
That previous thread I created does work bu this morning I ran into an issue. This is my code:

echo -e "cd DESTFOLDER\nlchdir /location/of/file/*.DAT\n" | sftp -b - username@hostname

I get an error that no such file or directory exists. The code is right, which is why I marked that thread as resolved but I think it might be because I am trying to use a wildcard instead of an exact filename. The reason for this is because the date is appended to the filename daily.
 
Old 01-11-2017, 10:15 AM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952

Rep: Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148Reputation: 2148
Quote:
Originally Posted by trickydba View Post
That previous thread I created does work bu this morning I ran into an issue. This is my code:

echo -e "cd DESTFOLDER\nlchdir /location/of/file/*.DAT\n" | sftp -b - username@hostname

I get an error that no such file or directory exists. The code is right, which is why I marked that thread as resolved but I think it might be because I am trying to use a wildcard instead of an exact filename. The reason for this is because the date is appended to the filename daily.
is it just one file?
run a find me command before hand
Code:
userx@voider~/testme:>> newfile="$(find ~/testme/ -type f -name "*.dat")"
userx@voider~/testme:>> echo $newfile
/home/userx/testme/3-3-2323.dat


newfile="$(find ~/testme/ -type f -name "*.dat")"

echo -e "cd DESTFOLDER\nlchdir "$newfile"\n" | sftp -b - username@hostname
but is that command needs to be tested.
 
1 members found this post helpful.
Old 01-11-2017, 10:21 AM   #11
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,500

Rep: Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110Reputation: 4110
In addition, your first post commands are not the same as your #9
Code:
echo -e "cd dir3\nlchdir /people/dir/occupation\nput *.DAT" | ...
 
1 members found this post helpful.
Old 01-11-2017, 12:12 PM   #12
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
My issue has been resolved using this code:

echo -e "cd PROD\nlchdir /dir1/dir2/dir3/reports\n put *.DAT" | sftp -b - username@hostname

Thank you everybody for your help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SFTP logging for Chroot on CentOS 6.2 with openssh-5.3 not working (internal-sftp) RatherBFishin Linux - Server 1 08-30-2012 06:45 PM
LXer: Restricting Users To SFTP Plus Setting Up Chrooted SSH/SFTP (Debian Squeeze) LXer Syndicated Linux News 0 09-06-2011 07:10 AM
How do I use sftp to upload my web site? (no sftp tar command) johnMG Linux - Networking 6 06-21-2005 09:14 PM
Files truncated by sftp/sftp-server at 65kb gato Linux - Networking 1 12-18-2003 10:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration