Link to home
Start Free TrialLog in
Avatar of stavrama
stavramaFlag for Greece

asked on

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

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

Avatar of http:// thevpn.guru
http:// thevpn.guru
Flag of Denmark image

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
Avatar of omarfarid
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
Hi shakoush2001,

I think you have problems in your script :)
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 :)
I think you need to change the END_SCRIPT to EOF or the other way.
 
Also, yo need prompt before the mput command
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.
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.
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.  
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.
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of stavrama

ASKER

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?
Is there a ".hosts" file on the target box that you need to add the sending box hostname to?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.  
Thanks for the question and the points.  Cheers.