how to ftp remote server through running a shell script - shell

ftp ipaddress
bin
hash
cd path
get filename
quit
i want these line to be executed in a shell script only first line is executing after entering username and password the rest line are not executing and the control is stuck at ftp> prompt

Once your script invokes the program ftp, the shell loses control until the ftp program finishes. So you need to use some new technique. One way is with the program expect, which you can get a hint about here: https://stackoverflow.com/a/12598169/4323 . Another way is to use a more "scriptable" FTP client, such as lftp on Linux, which has specific features to enable the sort of scripted use you're going for.

You need to use a non-interactive FTP client if you want to do this in a script rather than an interactive shell. ncftp is one you could check out.

Or you can use a shell "here" document:
#!/bin/bash
ftp -in <<EOS
user pswd
bin
hash
cd path
get filename
quit
EOS
The here document sends everything via the open program's stdin, just as if you typed it in at the command line. Note that ftp clients can be finicky and each one seems to have it's own set of gotchas, so some experimentation and use of man ftp will likely be required.
Looks like this has come up before on Stackoverflow, you might want to read through
how to ftp multiple file using shell script
IHTH

Related

simulate keyboard input to batch file via file and pipe on windows

the title might be slightly confusing but what I need is a method to hand over pre-configured parameters to a batchfile on windows commandline.
The batchflie executes several progams (openssl) that need interactive input. To avoid this, I wrote all necessary input parameters to a textflie and now try to do something like:
type parameters.txt | mybatchfile.bat
Unfortunately this doesn't work.
Is there a way to do this?
Passing text into stdin of interactive prompts of console programs sometimes works, but needs to be done on single line of specific program. Tested way for SSH communication in batch file:
some batch code
echo n| plink.exe -pw "somepassword" root#somehost someremotecommand
some batch code
https://the.earth.li/~sgtatham/putty/latest/w32/plink.exe

WinSCP and ASCII_RECSEP

What's the equivalent in WinSCP for this command:
quote "SITE ASCII_RECSEP=NONE"
Is it just -transfer=binary when using put command or it's something completely different?
From what I read it says like it's related to FTP streaming behavior. And it looks like this quote is used just to execute this SITE command on remote FTP server. But I'm not sure how to change my builtin FTP script (that part that I mentioned above) to work with WinSCP.
It looks like I need to use this call command?
https://winscp.net/eng/docs/scriptcommand_call
Yes, use the call command:
call SITE ASCII_RECSEP=NONE
With the FTP protocol, the WinSCP call command is an equivalent of the literal command (and its quote alias) of the Windows ftp.exe.
See a section on the literal command in the guide for converting Windows FTP script to WinSCP script.
And follow the quide for other issues you may encounter, when converting your script.

While Using PLink Only First Command Is Executing

I want to connect putty using plink and want to run some specific commands which is present in text file. But I can see only first command getting executed. Do I need to give any command separating literal?
I have this problem too. Solutions that I used.
Sending one line command with && or &
If script is big, send it with pscp and run on remote host with single command/
I beleive there is the third way. Just execute script under -m option :)

To read the output of a command in a shell script

I am running a unix command say ftp to a remote machine through a shell script.I have to pass the userid and password through the script accordingly.How can i do that?
Best way to supply username/passwords from scripts would be to use 'expect' scripting.
Here is a small example : http://www.linuxquestions.org/questions/linux-software-2/auto-ssh-login-expect-script-624047/
. It shows using expect to provide password for ssh, ftp should be very similar.
Redirect the ftp command inside your shell script
ftp ..... > ftp.log
Do you mean you want to get the result that the command (say, ftp) wrote to the terminal as a value inside the script?
Use backticks. For example:
x=`ftp ...`
will run the ftp command, and take the text of its stdout and store it in the variable x.

Recursive FTP directory listing in shell/bash with a single session (using cURL or ftp)

I am writing a little shellscript that needs to go through all folders and files on an ftp server (recursively). So far everything works fine using cURL - but it's pretty slow, becuase cURL starts a new session for every command. So for 500 directories, cURL preforms 500 logins.
Does anybody know, whether I can stay logged in using cURL (this would be my favourite solution) or how I can use ftp with only one session in a shell script?
I know how to execute a set of ftp commands and retrieve the response, but for the recursive listing, it has to be a little more dynamic...
Thanks for your help!
The command is actually ncftpls -R. It will recursively list all the files in a ftp folder.
Just to summarize what others have said so far. If you are trying to write a portable shell script which works as batch file, then you need to use the lftp solution since some FTP server may not implement ls -R. Simply replace 123.456.789.100 with the actual IP adress of the ftp server in the following examples:
$ lftp -c "open 123.456.789.100 && find -l && exit" > listing.txt
See the man page of lftp, go to the find section:
List files in the directory (current directory by default)
recursively. This can help with servers lacking ls -R support. You
can redirect output of this command.
However if you have a way to figure out whether or not the remote ftp server implements proper support for ls -lR, then a much better (=faster) solution will be:
$ echo ls -lR | ftp 123.456.789.100 > listing.txt
Just for reference if I execute the first command (lftp+find) it takes 0m55.384s to retrieve the full listing, while if I execute the second one (ftp+ls-R), it takes 0m3.225s.
If it's possible, try usign lftp script:
# lftp script "myscript.lftp"
open your-ftp-host
user username password
cd directory_with_subdirs_u_want_to_list
find
exit
Next thing u need is bash script to run this lftp command and write it to file:
#!/bin/bash
lftp -f myscript.lftp > myOutputFile
myOutputFile now contains the full dump of directories.
You could connect to the ftp server in a manner that it accepts commands from stdin and writes to stdout. Create two named pipes ("fifos", man mkfifo), redirect stdin and stdout of the ftp command each to one of them. Then you can write commands to the stdin-connected-fifo and read them (line-by-line with bash's read for example) from the stdout-fifo. Then use the results to see where you need to send another listing command (and print it or whatever you want to do)
In short: Not something bash scripting is suitable for :) (Until you find a tool that does what you want by itself of course)
if you just want to create a listing of all files and folders, you can use ssh instead. Something like this (but check the documentation on correct usage)
$ ssh user#host "ls -R /path"

Resources