LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-12-2003, 04:52 AM   #1
Mad_C
Member
 
Registered: Dec 2002
Location: Austria
Distribution: Slax, ccux-Linux, coLinux
Posts: 49

Rep: Reputation: 15
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
 
Old 09-12-2003, 05:35 AM   #2
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 31
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...
 
Old 09-12-2003, 07:01 AM   #3
yuray
Member
 
Registered: Apr 2003
Location: Russia, Khotkovo
Distribution: Debian
Posts: 146

Rep: Reputation: 15
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()
 
Old 09-12-2003, 07:34 AM   #4
Mad_C
Member
 
Registered: Dec 2002
Location: Austria
Distribution: Slax, ccux-Linux, coLinux
Posts: 49

Original Poster
Rep: Reputation: 15
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
 
Old 09-12-2003, 08:18 AM   #5
fsbooks
Member
 
Registered: Jan 2002
Location: Missoula. Montana, USA
Distribution: Slackware (various)
Posts: 460

Rep: Reputation: 42
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.
 
Old 09-12-2003, 09:27 AM   #6
Mad_C
Member
 
Registered: Dec 2002
Location: Austria
Distribution: Slax, ccux-Linux, coLinux
Posts: 49

Original Poster
Rep: Reputation: 15
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>.
 
Old 09-12-2003, 10:06 AM   #7
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
.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.
 
Old 09-12-2003, 10:53 AM   #8
fsbooks
Member
 
Registered: Jan 2002
Location: Missoula. Montana, USA
Distribution: Slackware (various)
Posts: 460

Rep: Reputation: 42
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.
 
Old 09-15-2003, 01:40 AM   #9
Mad_C
Member
 
Registered: Dec 2002
Location: Austria
Distribution: Slax, ccux-Linux, coLinux
Posts: 49

Original Poster
Rep: Reputation: 15
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!
 
Old 09-15-2003, 02:02 AM   #10
yuray
Member
 
Registered: Apr 2003
Location: Russia, Khotkovo
Distribution: Debian
Posts: 146

Rep: Reputation: 15
Sorry Mad_C my mistake

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

By
 
Old 09-15-2003, 04:03 AM   #11
Mad_C
Member
 
Registered: Dec 2002
Location: Austria
Distribution: Slax, ccux-Linux, coLinux
Posts: 49

Original Poster
Rep: Reputation: 15
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!
 
Old 09-15-2003, 08:25 AM   #12
yuray
Member
 
Registered: Apr 2003
Location: Russia, Khotkovo
Distribution: Debian
Posts: 146

Rep: Reputation: 15
Sorry again (many work)

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

ps File truncating from first try
 
Old 09-15-2003, 08:57 AM   #13
fsbooks
Member
 
Registered: Jan 2002
Location: Missoula. Montana, USA
Distribution: Slackware (various)
Posts: 460

Rep: Reputation: 42
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".
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
automation bong.mau Programming 2 09-06-2005 03:26 PM
Cdogg Automation goofyheadedpunk Linux - General 1 01-03-2005 06:56 PM
OpenOffice automation with C++ Paulo Góes Linux - Software 2 10-22-2004 08:55 AM
Permission Automation 0.o Linux - General 3 10-04-2004 01:32 PM
Pan automation - how? technobeast Linux - Software 0 07-12-2004 03:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:21 PM.

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