I'm using windows batch programming for creting a ftp connection to a ftp host an automatically upload file to that host. However, ftp command does not have parameter to get username and password in the command and I must input it at the subsequent command. How could the batch file wait for the ftp connection establish then echo the username and password for the subsequent ftp command?
Thanks for helping.
There's actually a different way to do this.
Windows ftp accepts a "-s:filename" argument containing commands to be run. You could store the username/password there in the format "USER username\nPASSWD" pass, and they'll be run first thing when the connection is established.
Make sure to delete the file afterward, or better yet use a temp file that doesn't hit the disk ;)
EDIT: Clarifying for coolkid. This really is your answer (thanks for the backup Joey).
To be explicit, here you are.
Contents of upload.cmd:
#echo off
set /p name= Username?
set /p pass= Password?
REM Give dummy data to the human-friendly FTP prompts
REM We will pass actual FTP commands in the next stanza
echo dummy > ftp_commands.txt
echo dummy >> ftp_commands.txt
echo USER %name% >> ftp_commands.txt
echo PASS %pass% >> ftp_commands.txt
echo PUT ftp_commands.txt >> ftp_commands.txt
ftp -s:ftp_commands.txt ftp.mydomain.com
Running upload.cmd:
C:\>upload.cmd
Username?anonymous
Password?foobar
Connected to ftp.mydomain.com.
220 2k-32-71-e Microsoft FTP Service (Version 5.0).
User (ftp.mydomain.com:(none)):
331 Password required for dummy .
530 User dummy cannot log in.
Login failed.
ftp> USER anonymous
331 Anonymous access allowed, send identity (e-mail name) as password.
230 Anonymous user logged in.
ftp> PUT ftp_commands.txt
200 PORT command successful.
You can echo some data to a temporary file and then redirect that file into the standard input of the ftp command. For example:
>file echo USERNAME
>>file echo.
>>file echo PASSWORD
>>file echo.
ftp < file
rem Now we can delete the file
del file
If you did not need to have newlines (necessary to simulate the ENTER keypress when the ftp program asks for credentials) then you could have managed without a temporary file:
echo USERNAME | ftp
Related
I currently have batch scripts on different servers that transfer a csv file to an FTP server at a different location. My script looks similar to this:
echo user ftp_user> ftpcmd.dat
echo password>> ftpcmd.dat
echo put c:\directory\%1-export-%date%.csv>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat ftp.MyFTPSite.com
del ftpcmd.dat
If I wanted to require a secure transmission, is how would my script be updated?
Thanks.
First, make sure you understand, if you need to use Secure FTP (=FTPS, as per your text) or SFTP (as per tag you have used).
Neither is supported by Windows command-line ftp.exe. As you have suggested, you can use WinSCP. It supports both FTPS and SFTP.
Using WinSCP, your batch file would look like (for SFTP):
echo open sftp://ftp_user:password#ftp.MyFTPSite.com -hostkey="..." >> ftpcmd.dat
echo put c:\directory\%1-export-%date%.csv >> ftpcmd.dat
echo exit >> ftpcmd.dat
winscp.com /script=ftpcmd.dat
del ftpcmd.dat
And the batch file:
winscp.com /log=ftpcmd.log /script=ftpcmd.dat /parameter %1 %date%
Though using all capabilities of WinSCP (particularly providing commands directly on command-line and the %TIMESTAMP% syntax), the batch file simplifies to:
winscp.com /log=ftpcmd.log /command ^
"open sftp://ftp_user:password#ftp.MyFTPSite.com -hostkey=""...""" ^
"put c:\directory\%1-export-%%TIMESTAMP#yyyymmdd%%.csv" ^
"exit"
For the purpose of -hostkey switch, see verifying the host key in script.
Easier than assembling the script/batch file manually is to setup and test the connection settings in WinSCP GUI and then have it generate the script or batch file for you:
All you need to tweak is the source file name (use the %TIMESTAMP% syntax as shown previously) and the path to the log file.
For FTPS, replace the sftp:// in the open command with ftpes:// (explicit TLS/SSL) or ftps:// (implicit TLS/SSL). And remove the -hostkey switch.
winscp.com /log=ftpcmd.log /command ^
"open ftps://ftp_user:password#ftp.MyFTPSite.com -explicit" ^
"put c:\directory\%1-export-%%TIMESTAMP#yyyymmdd%%.csv" ^
"exit"
You may need to add the -certificate switch, if your server's certificate is not issued by a trusted authority.
Again, as with the SFTP, easier is to setup and test the connection settings in WinSCP GUI and then have it generate the script or batch file for you.
See a complete conversion guide from ftp.exe to WinSCP.
You should also read the Guide to automating file transfers to FTP server or SFTP server.
Note to using %TIMESTAMP#yyyymmdd% instead of %date%: A format of %date% variable value is locale-specific. So make sure you test the script on the same locale you are actually going to use the script on. For example on my Czech locale the %date% resolves to čt 06. 11. 2014, what might be problematic when used as a part of a file name.
For this reason WinSCP supports (locale-neutral) timestamp formatting natively. For example %TIMESTAMP#yyyymmdd% resolves to 20170515 on any locale.
(I'm the author of WinSCP)
The built in FTP command doesn't have a facility for security. Use cUrl instead. It's scriptable, far more robust and has FTP security.
ftps -a -z -e:on -pfxfile:"S-PID.p12" -pfxpwfile:"S-PID.p12.pwd" -user:<S-PID number> -s:script <RemoteServerName> 2121
S-PID.p12 => certificate file name ;
S-PID.p12.pwd => certificate password file name ;
RemoteServerName => abcd123 ;
2121 => port number ;
ftps => command is part of ftps client software ;
I need to submit a JCL via FTP.
Wrote below code for it:
open server.com
uname
password
quote site LRECL=80 BLKSIZE=27920 RECFM=FB
literal SITE FILETYPE=JES
GET 'PDS.NAME(JCLNAME)' 'LOCAL\PATH\file.txt'
disconnect
bye
The problem is that even after the job gets completed in spool, it takes around 10 minutes before this script gets completed.
It seems to get stuck at 125 When Job is done, Will retrieve its output.
Maybe i am missing some pre initialization. Please advise.
It looks like the JES system doesn't really know how to process the request. If you take a read of the Steps for submitting a job and automatically receiving output article it explains that in order to get the output of the job automatically, the JCL JOBNAME must be USERIDx. So if my userid is ABC123, then my JOBNAME should be ABC123A. They also recommend a slightly different set up than you. Try this:
open server.com
USERID
password
SITE FILEtype=JES NOJESGETBYDSN
GET 'PDS.NAME(USERIDx)' 'LOCAL\PATH\file.txt'
disconnect
bye
When I tried your FTP commands, I got the same results (waiting for 10 minutes). I think it has to do with the JES interface level and how long the different files are held for. Using the commands above (and using the proper naming), the SYSOUTS will comeback when the job has completed as long as they are in HELD status. If there are some outputs that are not in HELD status, it will skip them.
Confused why you're using 'GET'.
I have always used 'PUT' to submit a JCL via FTP.
Here's a snippet of a batch job I'd use to submit JCL via FTP:
::: -- FTP Compress DOC --
:ftp_COMPRESS
echo.
echo " ----------------------------------- "
echo " COMPRESSING dataset "
echo " --------------------------------------- "
IF EXIST ftptemp.txt del ftptemp.txt
echo user %FTPUserID%>> ftptemp.txt
echo %FTPPwd%>> ftptemp.txt
echo cd ..>> ftptemp.txt
echo cd DATASET>> ftptemp.txt
echo del %filename%>> ftptemp.txt
echo quote site file=jes>> ftptemp.txt
echo put compit.jcl>> ftptemp.txt
echo quote site file=seq
echo quit>> ftptemp.txt
ftp -n -s:ftptemp.txt %host%
pause
Here's the JCL job on my local machine saved as compit.jcl, same directory as .bat:
//COMPIT JOB CARD
//*-------------------------------------------------------------
//COMPRESS EXEC PGM=IEBCOPY
//SYSPRINT DD SYSOUT=*
//LIB DD DSN=DATASET,DISP=OLD
//SYSIN DD *
COPY INDD=LIB,OUTDD=LIB
/*
The job itself was a quick way I'd compress a dataset before uploading more members to it.
I am running simulations in parallel using mpich2. I've got rather stringent security on my workstation, and must register using a new password each time I run a simulation. I have to enter:
mpiexec -register
which then prompt me for a username, and then prompt me for a password. Unfortunately, there seem to be no way to pass the user/pass to mpiexec on a single line, e.g.
mpiexec -register user:pass
does not work.
I'm trying to prepare a batch file that can automatically pass the username and password to the mpiexec prompts, but I cannot seem to get it to work. I've tried various things like timeout /t 5 but that doesn't work.
Can anyone tell me how to pass these inputs to the mpiexec program prompts in a batch file?
Thanks!
EDIT: I think I am getting closer. I've tried
(
echo username
echo password
echo password
) | mpiexec -register
which appears to be passing the username and password inputs to the mpiexec prompts. Program is still hanging at the next step however - not sure if that's a problem with the way I'm passing these or not.
You could redirect or pipe into mpiexec.
With redirection it's gets a bit nasty for user/password entries, as there are often unwanted (and unvisible) spaces at the line ends.
(
echo user
echo pwd
) | more > fetch.txt
Creates in fetch.txt
user<space>
pwd<space>
When you want to suppress the spaces use a file redirection instead
(
echo user
echo pwd
) > file.tmp
< file.tmp mpiexec -register
In both cases (redirection or pipe), you need to serve all inputs for the program, not only username and password.
You can't enter inputs from keyboard anymore.
I have a batch file I want to be able to call from the command line like:
myBatch.bat testParam
That batch file is the following:
set sid=%1
C:\Windows\System32\ftp.exe -s:%0
goto done
open servername
username
password
get 'FilePath%sid%restoffilepath' targetPath\%sid%MyName.txt
bye
:done
However, I cannot seem to get the FilePath%sid%restoffilepath part to work right - I believe it is because the %0 is treating the input as literal, but I'm not 100% sure. The %sid% variable is not expanded.
I basically want to have FilePath%sid%restoffilepath be FilePathtestParamrestoffilepath in this case.
Think carefully about what you are doing here--the ftp.exe is reading the file. Your batch script, which knows what %1 is, is not feeding the data to ftp.exe.
What you need to do is to shoot the script out to a file, and then run the ftp command:
set sid=%1
echo open servername >> myftp.txt
echo username >> myftp.txt
echo password >> myftp.txt
echo get 'FilePath%sid%restoffilepath' targetPath\%sid%MyName.txt >> myftp.txt
echo bye >> myftp.txt
C:\Windows\System32\ftp.exe -s:myftp.txt
I am using bash a script to connect to an FTP server for deleting a file.
I would like to store the output message and code of the delete command executed on the FTP server into a variable of my script.
How could I do this ?
Here is my snippet :
...
function delete_on_ftp{
ftp -i -n $ftp_host $ftp_port <<EOF
quote USER $ftp_login
quote PASS $ftp_pass
delete $1
quit
EOF
}
output_cmd=$(delete_on_ftp $myfile)
...
By the way I do above I only get the message, no way to get the returned code. Is there another way allowing to get the code and the message, in 1 or 2 variables ?
Thanks, Cheers
I just tested the following curl command, which make your task easy.
curl --ftp-ssl -vX "DELE oldfile.pdf" ftp://$user:$pass#$server/public_html/downloads/
Please do not forget the slash at the end of your directory, it is necessary.
curl: (19) RETR response: 550
550 oldfile.pdf: No such file or directory
curl: (19) RETR response: 250
250 DELE command successful
curl is available at http://curl.haxx.se/.
One of the ways to get FTP to act automatically is to use a Netrc file. By default, FTP will use $HOME/.netrc, but you can override that via the -N parameter. The format of a netrc file is fairly straight forward. A line is either a Macrodef or a line that contains login information. Here's an example below:
Netrc File
mysystem login renard password swordfish
another login renard password 123456
default login renard password foofighter
macdef init
binary
cd foo
get bar
delete bar
quit
macdef fubar
...
The three first lines are the logins for various systems. The default is a login for any system which you don't define a particular login for. The lines that start with marcodef are macros you define to do a series of steps for you. The init macro automatically runs right after login. If the last line is quit, it will quit out of FTP for you. There should be a blank line to end the macro, (although most systems will take an End of the File as the end of the macrodef too).
You can create a Netrc file on the fly, enter your FTP command in that, and then, run your FTP command with that Netrc file:
cat > $netrc_file <<<EOF
$ftp_host login $ftp_login password $ftp_password
macdef init
delete $my_file
quit
EOF
ftp -N $netrc_file
You can capture the output via STDOUT, or in a variable and then parse that for what you need:
ftp -N $netrc_file | tee $ftp_output
Other answers on this question should provide you what you want.
However, if you are keen on specifically using ftp command, you can use expect command for the same...
Note, that this is not the best way to achieve what you are trying.
expect -c "log_user 0;
spawn ftp -i -n $ftp_host $ftp_port;
expect \"<add ftp login username: prompt details here>\"
send \"quote USER $ftp_login\r\n\"
expect \"<add ftp login password: prompt details here>\"
send \"quote PASS $ftp_pass\r\n\"
expect \"<add ftp shell prompt details here>\"
log_user 1; send \"delete $1\r\n\"
log_user 0;
expect \"<add ftp shell prompt details here>\"
send \"quit\r\n\";
interact"
You may need to add some more lines in the above for the login & shell prompts returned by the ftp command.