Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

Automate FTP

Altera_Forum
Honored Contributor II
2,467 Views

Hello champs, 

 

 

I am trying to write a routine that automates FTP transfers. I was using ftpput in Busybox but it consumes way too much memory. I've seen some examples but they are in ksh and Perl? I am sort of a newbie in linux, I know how some things work but have still got a lot to learn.  

 

I paste the example I found from a linux forum.  

 

Example from here (http://www.unix.com/answers-frequently-asked-questions/14020-automate-ftp-scripting-ftp-transfers.html): 

 

#! /usr/bin/ksh 

 

host=remote.host.name 

user=whoever 

passwd=whatever 

 

exec 4>&1 

ftp -nv >&4 2>&4 |& 

 

print -p open $host 

print -p user $user $passwd 

print -p cd directory 

print -p binary 

print -p put tar.gz 

print -p bye 

 

wait 

exit 0 

 

However, I don't know if there is a 'print' command in msh or sash  

 

 

I think this would be a lighter option over SSH.  

 

 

Any ideas will be appreciated. 

 

Thanks in advance. 

0 Kudos
14 Replies
Altera_Forum
Honored Contributor II
1,229 Views

If you use msh (depreciated !) or hush, I suppose you already do use busybox. So why can't you use busybox based ftpput ? 

 

-Michael
0 Kudos
Altera_Forum
Honored Contributor II
1,229 Views

TO_BE_DONE

0 Kudos
Altera_Forum
Honored Contributor II
1,229 Views

<div class='quotetop'>QUOTE (franciscoheredia @ Jun 29 2009, 01:40 PM) <{post_snapback}> (index.php?act=findpost&pid=22936)</div> 

--- Quote Start ---  

I&#39;ve heard that standalone applications are much lighter that when working through busybox.[/b] 

--- Quote End ---  

 

Busybox is only "lighter" for architectures that support XIP and/or .so . But NIOS nommu supports neither of them I suppose the upcoming NIOS / MMU architecture will support XIP and .so . 

 

<div class='quotetop'>QUOTE (franciscoheredia @ Jun 29 2009, 01:40 PM) <{post_snapback}> (index.php?act=findpost&pid=22936)</div> 

--- Quote Start ---  

On the other hand, this is a recursive program that keeps calling the the buffer filling.[/b] 

--- Quote End ---  

Programs recursively calling each other ? This is rally not a good idea on an architecture that does not support XIP nor .so . 

 

-Michael
0 Kudos
Altera_Forum
Honored Contributor II
1,229 Views

Hello, 

 

 

I&#39;ve made modifications to my application. Eliminated the recursive part to a single program that fills in the buffers, and verifies whether it should send or not the log file via FTP. However, I met the same problem. I haven&#39;t figured out how to substitute the ftpput command, and after it is called a few times, the OS says it doesn&#39;t have RAM Space.  

 

The ftp client provided in make menuconfig ---> user apps ----> network applications ----> ftp works perfectly and is able to send files. but the user has to input all the data. I was wondering if there is a way to automate that one.  

 

My idea is , for example, send "ftp 192.3.1.2" to open it in the console, then wait for some time between instructions, and send the "useraccount", "password", "put /file", and "exit" as if I was writing them on the console. I don&#39;t know if this can be made by writing on stdin or something.  

 

If this is not possible, I think I would have to program the protocol in a very light way in C. Any chances anyone knows about a routine about this. I&#39;ve read some, but they are quite difficult for me to follow.  

 

Also read about tftp, butn since is is done via UDP, I think I will go along with ftp. 

 

Any help is appreciated. 

 

Thanks in advance 

 

 

Francisco
0 Kudos
Altera_Forum
Honored Contributor II
1,229 Views

I understand that the program called "ftp" is intended for interactive use by a human. I don&#39;t think that it&#39;s a good idea to try to abuse it for automation.  

 

Unless you find an appropriate uploading command line utility (such as wget for downloading), I suppose you need to write the protocol handling in C (maybe there is a decent ftrp library somewhere, that you can use).  

 

ncftp is a package for PCs that containf ncftpput which seems to do what you want. 

 

Here http://linux.wareseeker.com/free-thin-ftp-upload/ (http://linux.wareseeker.com/free-thin-ftp-upload/) there seem to be several more examples. 

 

You might try to port some of this to NIOS uClinux or extract some code to include in your application. 

 

-Michael
0 Kudos
Altera_Forum
Honored Contributor II
1,229 Views

Here&#39;s an option: on your PC, create a file named autoftp with the contents: 

 

<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>ftp -v -n <<EOF 

open $1 

user anonymous passwd 

binary 

lcd /home/jars/microvote/eclipse/firmware/bin 

put $2 

bye</div> 

 

Make this file executable using: 

chmod 777 autoftp 

 

Then you just have to type something like: 

./autoftp 192.168.2.3 myapp 

 

Hope it helps, 

 

Ricardo.
0 Kudos
Altera_Forum
Honored Contributor II
1,229 Views

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

<div class='codetop'>CODE 

--- Quote End ---  

<div class='codemain' style='height:200px;white-space:pre;overflow:auto'>ftp -v -n <<EOF 

open $1 

user anonymous passwd 

binary 

lcd /home/jars/microvote/eclipse/firmware/bin 

put $2 

bye</div> 

 

Make this file executable using: 

chmod 777 autoftp 

 

Then you just have to type something like: 

./autoftp 192.168.2.3 myapp[/b] 

--- Quote End ---  

 

 

 

Hello Ricardo, 

 

 

Thanks for your suggestion. I&#39;m not sure I understand it completely. Haven&#39;t got much experience with scripting. Do you think you could explain a little on the code? Would be really nice  

 

I&#39;ll try however to program it. I believe in line /autoftp 192.168.2.3 myapp , myapp corresponds to the file I am trying to send? What is the binary command for? 

 

Thank you very much. Seems this is a light as it can be! Will try it for sure. 

 

Regards,  

 

Francisco
0 Kudos
Altera_Forum
Honored Contributor II
1,229 Views

Hello again, 

 

 

I did try with this short script, howevr it did not work. It always said  

"Here EOF unclosed" or something like that. 

 

I was trying to do this because ftpput was not supported by the memory. The OS was not able to allocate enough memory for the process. But with the other post, the one about mmap. I was ale to do it without changing that. I just used ftpput and it&#39;s woring correctly. It&#39;s been working for over an hour now, and it seems fine.  

 

 

 

Thanks for your help. 

 

Francisco
0 Kudos
Altera_Forum
Honored Contributor II
1,229 Views

Hi, 

 

I tested again with the following script: 

 

<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>ftp -v -n <<EOF 

open $1 

user anonymous passwd 

binary 

put $2 

bye</div> 

 

Named it myftp and hit: 

./myftp 172.16.9.92 sample_file  

 

The results: 

<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'>jars@jars-desktop:~/test$ ./myftp 172.16.9.92 sample_file  

Connected to 172.16.9.92. 

220- Welcome to the uClinux ftpd! 

220 uClinux FTP server (GNU inetutils 1.4.1) ready. 

Remote system type is UNIX. 

Using binary mode to transfer files. 

331 Guest login ok, type your name as password. 

230 Guest login ok, access restrictions apply. 

Remote system type is UNIX. 

Using binary mode to transfer files. 

200 Type set to I. 

local: sample_file remote: sample_file 

200 PORT command sucessful. 

150 Opening BINARY mode data connection for &#39;sample_file&#39;. 

226 Transfer complete. 

110 bytes sent in 0.00 secs (852.6 kB/s) 

221 Goodbye. 

jars@jars-desktop:~/test$  

</div> 

 

I&#39;m using bash as my default shell: 

bash --version 

GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu) 

Copyright © 2007 Free Software Foundation, Inc. 

echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007" 

 

Please make sure you&#39;ve copied the script above without any changes. This script is very sensitive regarding misplaced space, EOF and end of line characters. Other than that, I don&#39;t know what could be going wrong... 

 

Good luck, 

 

Ricardo.
0 Kudos
Altera_Forum
Honored Contributor II
1,229 Views

Hello Ricardo,  

 

Thank you very much. I will certainly try out these routine. Perhaps I was mistaken when copying the code.  

 

I&#39;ll let you know about the results. Of course you know it works :P 

 

Regards,  

 

Francisco
0 Kudos
Altera_Forum
Honored Contributor II
1,229 Views

I don&#39;t understand the "EOF" that is piped. maybe this foirst must be declared somehow to be used with the actual shell that can&#39;t be bash in a NIOS device.  

 

If you use the sahs shell as default, yopu need to activate hush in buisibox and add a line 

# !/bin/hush  

 

at the beginning of the script, as sash can&#39;t do piping. 

 

-Michael
0 Kudos
Altera_Forum
Honored Contributor II
1,229 Views

<div class='quotetop'>QUOTE (mschnell @ Jul 15 2009, 05:00 AM) <{post_snapback}> (index.php?act=findpost&pid=23128)</div> 

--- Quote Start ---  

I don&#39;t understand the "EOF" that is piped. maybe this foirst must be declared somehow to be used with the actual shell that can&#39;t be bash in a NIOS device.[/b] 

--- Quote End ---  

 

Michael, I don&#39;t understand it 100% either, but in my understanding the <<EOF is what splits the script into two parts: the first (before <<EOF) goes to your default shell (bash, in my Ubuntu) and the second part is used as input commands for the ftp client that is started. So, after the EOF, it is the same as you started ftp manually and typed the lines one by one - but that wouldn&#39;t be automated, of course. 

 

<div class='quotetop'>QUOTE (mschnell @ Jul 15 2009, 05:00 AM) <{post_snapback}> (index.php?act=findpost&pid=23128)</div> 

--- Quote Start ---  

If you use the sahs shell as default, yopu need to activate hush in buisibox and add a line[/b] 

--- Quote End ---  

 

The script is run in my desktop PC/Ubuntu machine, so it has nothing to do with the uClinux shell. In my Nios box, I use sash as the default shell. 

 

Cheers, 

 

Ricardo.
0 Kudos
Altera_Forum
Honored Contributor II
1,229 Views

This is something I wrote to the forum before it crashed that I found on google that may be of help. 

 

"I found a few things on the internet and adapted them today to make life easier. Try this shell script. 

# !/bin/sh 

 

echo "open uClinux" 

echo "user root uClinux" 

echo "put ./test /bin/test" 

echo "bye" 

} | ftp -vin 

 

 

echo "open uClinux" 

sleep 1 

echo "root" 

sleep .2 

echo "uClinux" 

sleep .5 

echo "chmod 777 /bin/test" 

sleep .5 

echo "gdbserver localhost:10000 /bin/test" 

sleep 1 

} | telnet 

# end of file# 

 

This is just an alternate way, but if it helps anyone........
0 Kudos
Altera_Forum
Honored Contributor II
1,229 Views

<div class='quotetop'>QUOTE (Nails @ Jul 21 2009, 08:12 PM) <{post_snapback}> (index.php?act=findpost&pid=23236)</div> 

--- Quote Start ---  

#!/bin/sh 

echo "open uClinux" 

echo "user root uClinux" 

echo "put ./test /bin/test" 

echo "bye" 

} | ftp -vin[/b] 

--- Quote End ---  

 

 

With this, "sh" needs to be hush (or msh), as sash, which had been the default shell "/bin/sh" for quite a time, does not support piping ("|").  

 

The very new version of uClinux dist (provided on the blackfin server) does use hush as default shell, though.  

 

But you can actiate hush (e.g. in busybox) additionally to sash, and start the script with# !/bin/sash.  

 

-Michael 

0 Kudos
Reply