Solved

How to create an ftp script to transfer files from linux to windows

Posted on 2008-10-09
16
2,829 Views
Last Modified: 2013-12-02
Hello experts,

I am trying to create a script in order to automate the ftp transfer i do by hand from linux to a windows server. The server belongs to a domain and the user i need to authenticate belongs to an Active Directory (user name: test, password: test for example)
The script i am using is the following. I dunno much about scripting and not so much yet concerning linux so it might be totally wrong.

When i run it, i get the following errors:

'AUTH GSSAPI': command not understood
'AUTH KERBEROS_V4': command not understood
KERBEROS_V4 rejected as an authentication type
User domaintest cannot log in.
Login failed.
Please login with USER and PASS.
Local directory now /u01/app/oracle/scripts/SIDE_DB_Backup/imports
Please login with USER and PASS.
Please login with USER and PASS.
Passive mode refused.  Turning off passive mode.
Please login with USER and PASS.
ftp: bind: Address already in use
Please login with USER and PASS.
Please login with USER and PASS.

Any help for this issue would be more than welcome.

#!/usr/bin/env ksh
 

SERVER=10.250.x.x

LOCALDIR=/u01/app/oracle/scripts/SIDE_DB_Backup/imports
 

# transfer the file
 

ftp -in << EOF

open $SERVER

user domain\test test

binary

lcd $LOCALDIR

cd /Oracle

mput *.dmp

close

EOF

Open in new window

0
Comment
Question by:stavrama
  • 5
  • 5
  • 2
  • +2
16 Comments
 
LVL 19

Expert Comment

by:http:// thevpn.guru
ID: 22676287
Try this




ftp -inv $FTPS <<END_SCRIPT
quote USER $FTPU
quote PASS $FTPP
cd $FTPD
lcd $BACKUP
mput *
quit
EOF


Of course you have to setup the vars first.

Also check out


http://bash.cyberciti.biz/backup/wizard-ftp-script.php
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 22676292
Step for ftp script

- You should have a script call it myscript that will ftp file to remote server:

cd /localdir
ftp remoteserver <<END
cd /dir
binary
prompt
mput *.dmp
bye
END

- make this script executable:

chmod +x myscript

- For ftp to work without providing any username / password,  use .netrc file in the user's home directory (the one who will run the script). This file should not be readable by others i.e. use
chmod 400 .netrc

The entry in .netrc should be as below:

machine remoteserver
login remoteusername
password mypassword

remoteserver is the server which is the ftp server where you want to sent the file(s)
remoteusername is the remote user login name on the ftp server
mypassword is the password of the remote user on the ftp server

for more info about .netrc, please use man netrc
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 22676297
Hi shakoush2001,

I think you have problems in your script :)
0
 
LVL 19

Expert Comment

by:http:// thevpn.guru
ID: 22676335
Hi Omar
Long time no see how it everything going, the script works fine :) But I need to put in the Vars I think to make it show how it should :)
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 22676350
I think you need to change the END_SCRIPT to EOF or the other way.
 
Also, yo need prompt before the mput command
0
 
LVL 8

Expert Comment

by:eager
ID: 22687155
There may be timing problems using a shell script to connect with a remote ftp system. It looks like your script (as well as the other suggestions) will send the user id and password before the remote ftp system requests it.

Scripts are not robust and cannot tolerate any changes in remote system behavior, such as server not available or file overwrite questions.  Your script may hang or it may appear to complete without actually doing anything.  

I recommend that you take a look at expect,  (Do 'man expect' for details.)

Expect allows you to program a conversation with a remote system (or program) and make decisions based on the responses received.  You can retry commands, set timeouts, and so forth; essentially anything which you would do yourself can be automated.  With expect you can check to see that the files were correctly transfered and handle problems like server unavailable.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 22687227
Hi eager,

How you concluded that there is a timing problem at  least to the one I recommended? In my case the ftp client itself will read username and password details from the .netrc file and do the exchange of  username and password properly with the ftp server. This is a tested solution.
0
 
LVL 8

Expert Comment

by:eager
ID: 22687552
Omar --

Yes, reading userid/password from .netrc will bypass timing problems for that data.

There may be other timing issues.  But the most significant problem is that a bash script cannot respond (or even recognize) when it is out of sync with the remote ftp server.  
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 16

Expert Comment

by:Bryan Butler
ID: 22687666
I have tackled this 10 years ago and had to use Expect then.   I thought for sure there would come a better way to handle this, but I guess not.  TCL/Expect was the only way to handshake.
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 300 total points
ID: 22687766
I just noted that there is a problem with the script which is the space before EOF. Also please use bye to exit the ftp connection, try

#!/usr/bin/env ksh
 
SERVER=10.250.x.x
LOCALDIR=/u01/app/oracle/scripts/SIDE_DB_Backup/imports
 
# transfer the file
 
ftp -in <<EOF
open $SERVER
user domain\test test
binary
lcd $LOCALDIR
cd /Oracle
mput *.dmp
bye
EOF

You need to be careful as well in not having space before EOF in the last line other wise use <<-EOF

The script I posted earlier is being used for years now and I personally did not face problems with this script and it is also considered safe since you are not including the username and password in the script itself (where in the asker scriot and in using expect, you have to include it part of the script) but kept in a file which is protected and only owner can read.
0
 
LVL 16

Expert Comment

by:Bryan Butler
ID: 22689982
http://mail.python.org/pipermail/python-list/2000-September/052191.html

This talks about using tcl/expect.  The problem I see with doing it the way omarfarid details is error handling.  If this isn't an issue, then adding a wrapper that does a verification (using "ls" or something) and trying again if it doesn't show up might work for you.  The problem here is the session would have to timeout or you would have to manage the max time to complete, before you could start the next one.  One of the problems with the expect script was different versions of FTP have slightly or significantly different working on responses, so the 'expect' script had to try to handle all of them.  I think they best bet would be using a "module" as suggested in the link above.  I believe there are ftp modules for perl and many other shells too.
0
 
LVL 2

Author Comment

by:stavrama
ID: 22765248
Thank you all for your replies. The problem though is, that i still cant login with the user. The user belongs in a domain so whenever i try to login without a script i type on user name :domain\user and i pass the authentication without any problem. If i add this though to the script the authentication fails telling me that User domainuser (without the \) cannot log in.
Is there any way to specify the domain within the script differently, so to pass the authentication process?
0
 
LVL 16

Expert Comment

by:Bryan Butler
ID: 22769261
Is there a ".hosts" file on the target box that you need to add the sending box hostname to?
0
 
LVL 16

Assisted Solution

by:Bryan Butler
Bryan Butler earned 200 total points
ID: 22769293
Oh, wait, I see, it does work manually, but not in a script.  I believe the "\" is being "interpreted out" as a special character, so you may need to add double or single quotes around that part of the command:

user "domain\test test"
OR
user `domain\test test`
OR maybe escaping the excape:
user domain\\test test
OR even using a backslash:
user domain/test test


0
 
LVL 2

Author Comment

by:stavrama
ID: 22810564
Now it is working perfectly. In order to accept the domain user i typed user domain/test test in the script instead of domain\test i was using when i was connecting manually. Quite starnge isn't it?
Thanx a lot for your help.  
0
 
LVL 16

Expert Comment

by:Bryan Butler
ID: 22901415
Thanks for the question and the points.  Cheers.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now