LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   ftp automation (https://www.linuxquestions.org/questions/linux-general-1/ftp-automation-91936/)

Mad_C 09-12-2003 04:52 AM

ftp automation
 
I want to transfer files to an embedded system which has an root account and an empty password. During development, entering the user name, the password and the file transfer command becomes tedious after a while:

ftp embedded.system
root

cd /mnt
put filename
bye

Is there a way to automate this?

TIA, Christian

Bebo 09-12-2003 05:35 AM

If you have ssh installed, you can use scp (secure copy; in any case, you shouldn't use ftp which doesn't encrypt the password). If you use scp you can just define an alias, such as

alias whatever="scp /mnt/filename root@embedded.system:<target dir>"

Of course you will have to give the command "whatever" :) and also the password. Maybe not what you wanted.

In ncftp you can define macros, which you put in ~/.ncftp/macros, or something like it, where you can put a list of commands that ncftp then issues. This still requires you to start the program, enter the macro name and password, and quit, though...

yuray 09-12-2003 07:01 AM

Like this
#!/usr/bin/python
import ftplib
ftp=ftplib.FTP(IP,'root','')
ftp.cwd('/mnt')
ftp.storbinary("STOR filename", open('filename','wb').write)
ftp.quit()

Mad_C 09-12-2003 07:34 AM

Hello Bebo, hello Yuray, and thanks for the replies.
I don't have ssh on the embedded, and the python code has problems, as well:

#!/usr/bin/python
import ftplib
ftp=ftplib.FTP('163.157.141.251','root','')
ftp.cwd('/mnt')
ftp.storbinary("STOR cscope", open('cscope','wb').write)
ftp.quit()
gives the following error:
ftp.storbinary("STOR cscope", open('cscope','wb').write)
File "/usr/lib/python2.2/ftplib.py", line 423, in storbinary
buf = fp.read(blocksize)
AttributeError: 'builtin_function_or_method' object has no attribute 'read'

I don't know python at all, so I have no clue what's wrong..

Christian

fsbooks 09-12-2003 08:18 AM

The following should work, or beable to be adapted to your needs. I modified a script I have used frequently in the past.

#!/bin/bash
ftp -n <host> <<%%
user root
cd <new dir>
prompt
bin
put <filename>
# You can also following (commented line) to use an argument.
#put $1
bye
%%


Note that if you did have a password, you could put this is a file called .netrc (though I would not keep it around too long with the password, but it is great for anonymous ftp where an "email" must be entered). Also, if you are transfering multiple files at once, you can use mput.

Mad_C 09-12-2003 09:27 AM

Thanks a lot fsbooks, works like a charm!

Now I also want to have it fully automatic, where do I put the .netrc and what do I put in there? Since I am developing on a private LAN, my root password is <CR>.

/bin/bash 09-12-2003 10:06 AM

.netrc would be in your home directory.

$ cat .netrc
machine ftp.machine.com
login username
password password

You could put the script anywhere then add a cron job.

fsbooks 09-12-2003 10:53 AM

Even in a local network or a disconnected machine, I believe it is best to have a root password, if for no other reason than it forces a person "to become" root and thus be more aware of the danger (comes with power) he/she is now in.

That said, a cronjob is certainly the place for automation. The particulars depend somewhat on the particulars of the problem, which you have not stated.

Further, a couple of notes on the script:

1) the -n option to ftp disables the use of .netrc If you want to use .netrc, leave out the "user root" part. You can also (in general, with -n option) make this "user <user> password" in the script.

2) The parts between the "<<%%" and "%%" are ftp commands. Therefore, the script listed above with comment lines between these markers would produce errors. You probably did not include them or you probably would have mentioned the errors, however I thought I would explicitly state this here in case anyone reads the archive.

Mad_C 09-15-2003 01:40 AM

I will eventually set the root password shortly before the project is ready for deployment. Thanks for mentioning that the -n disables .netrc, that explains things. I might even set a new root password now since I am not entirely sure how to include a <CR> password in .netrc or on the command line with -n.
As for the comment lines, first I got the errors, then I figured that ftp doesn't like #-comments.
I need this ftp transfer at the end of a make process, so cron is not necessary.

Thanks again for the enlightment!

yuray 09-15-2003 02:02 AM

Sorry Mad_C my mistake

ftp.storbinary("STOR cscope", open('cscope','rb').read)

By

Mad_C 09-15-2003 04:03 AM

Yuray,
Tried your version again, still the same error:
File "./trans", line 5, in ?
ftp.storbinary("STOR cscope", open('cscope','rb').read)
File "/usr/lib/python2.2/ftplib.py", line 423, in storbinary
buf = fp.read(blocksize)
AttributeError: 'builtin_function_or_method' object has no attribute 'read'
And my source file is truncated to 0 bytes!

yuray 09-15-2003 08:25 AM

Sorry again (many work)

ftp.storbinary("STOR cscope", open('cscope','rb'))

ps File truncating from first try :)

fsbooks 09-15-2003 08:57 AM

To include a passward in the script above, where it says "user root", use "user root <passwd>". For use from .netrc, use just like /bin/bash indicates. Note that of course, as for the password token, "man netrc" indicates - if this token is present in the .netrc file for any user other than anonymous, ftp will abort the auto-login process if the.netrc is readable by anyone besides the user. In otherwords, "chmod 600 ~/.netrc".


All times are GMT -5. The time now is 08:30 PM.