Using ftp.exe to create some kind of log [duplicate] - cmd

I am writing a batch command to send data via FTP. Before sending the actual data I need to find if the FTP server is active/running. How do I check that in batch command?
The server responds with "220 server ready" message when it is connected.

Do something like this:
YourFTPCommand | find /i /v "220 server ready" && goto :ServerNotReady
Explanation:
Pipe the output of your FTP command to FIND
Do a case insensitive (/i) search for output that does not contain (/v) the string "220 server ready"
Go to someplace if such a string is found (&&)

I do not think there's a reliable way to do this with Windows ftp.exe.
It blindly keeps running the commands, no matter if connection or previous commands succeeded.
It won't even report the result via exit code.
All you can do is to parse the ftp.exe output.
You should better use a 3rd-party FTP client.
For example with WinSCP scripting you can use:
#echo off
winscp.com /log=ftp.log /command ^
"open ftp://user:password#example.com/" ^
"put c:\local\path\file.txt" ^
"exit"
If connection (the open command) fails, WinSCP won't execute the following commands (the put).
See also Converting Windows FTP script to WinSCP FTP script.
(I'm the author of WinSCP)

Related

Windows batch script to connect to SFTP server [duplicate]

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 ;

Scheduled batch file not running

I have a .bat file. If I run it manually (double click), it works. If I schedule it, it doesn't.
I tried solutions from similar questions on StackOverflow but none of them is working. Like:
Batch not scheduled
WIN 10 task scheduler
I changed general options.
Run no matter user connection
I changed the user starting the scheduled task.
Mine, BTW, has maximum permissions and when double clicked, it works.
I allowed maximum permissions.
Modified "Add argument" and "Start in"
I set no conditions in the "conditions" tab
I'm new to scripting, so it is possible I set something wrong.
That's my code:
#echo off
"C:\Program Files (x86)\WinSCP\winscp.com" /log=winscp.log /ini=nul /command ^
"open sftp://user:password#mySite.com -hostkey=""ssh-rsa 4096 xxxxxxxxx/yyyyw=""" ^
"option batch" ^
"option transfer binary" ^
"synchronize remote -filemask=""*.png>=2016-01-01<4M;*.jpg>=2016-01-01<4M "" \\FromFolder /destination/ -nopreservetime"
"exit"
If I put md C:\Temp & echo %TIME%>C:\Temp\Test.txt as 2° row, it writes the Test.txt file. If i put it in the end of the file it doesn't. No problems if i run it by double click.
I need the task to run the .bat file just as if I am double clicking it. I don't know if is it possible to open the shell or run it in background, as both of them are good for me. Let me know.
Thanks for suggestions.
Edit_1)
I removed the echo %TIME% >> C:\Temp\Test.txt and kept the "exit" >> C:\Temp\Test.txt .
If I double click my bat file, this is the log:
Host Search ...
Host connection ...
Authentication ...
Use username "xxxx".
Authentication with preset password
Authenticated.
Starting session ...
Session started.
Active session: [1] xxx#site.com
batch abort
transfer binary
Comparison...
Local 'file\MyFolder1' => remote '/ images'
Nothing to synchronize
Comparison...
Local 'H:\MyPath\MyFolder2' => remote '/ images'
Synchronization...
Local 'H:\MyPath\MyFolder2' => remote '/ images'
H:\MyPath\MyFolder2 image.JPG | 617 KB | 50.4 KB / s | binary | 100%
And it works perfectly.
If i run it from schedule (using my same user)
this is the log.
Host Search ...
Host connection ...
Authentication ...
Use username "xxxx".
Authentication with preset password
Authenticated.
Starting session ...
Session started.
Active session: [1] xxx#site.com
batch abort
transfer binary
Comparison...
Local 'file\MyFolder1' => remote '/ images'
Nothing to synchronize
Comparison...
Local 'H:\MyPath\MyFolder2' => remote '/ images'
Folder List Error Request 'H:\MyPath\MyFolder2\*.*'.
Error retrieving file list for "H:\MyPath\MyFolder2\*.*'.
System error. Code: 3.
The specified path could not be found
(I) nterrupt, (R) etry, (S) top: Stop
Folder List Error Request 'H:\MyPath\MyFolder2\*.*'
Error retrieving file list for "H:\MyPath\MyFolder2\*.*'
System error. Code: 3.
The specified path could not be found
I schedule the bat file using my user and max privileges so it should find the folder in the H:\ disk.
WinSCP most probably fails. I would guess that because the batch file working directory is not hat you think and winscp.log cannot be created there. Try using a path like for your debug test: C:\Temp\winscp.log.
The reason why the md C:\Temp & echo %TIME%>C:\Temp\Test.txt does not work at the end of your batch file is the exit command. You probably believe it's interpreted as WinSCP command. But it's not. You are missing ^ after the synchronize command. And additionally, you have a blank space after it. As a consequence, the exit is not part of WinSCP command-line, but it exits the batch file instead. Also the lines have to be indented (though I assume that it's rather due to your post formatting).
Try something like this:
#echo off
md C:\Temp
echo %TIME% > C:\Temp\Test.txt
"C:\Program Files (x86)\WinSCP\winscp.com" /log=C:\temp\winscp.log /ini=nul /command ^
"open sftp://user:password#mySite.com -hostkey=""ssh-rsa 4096 xxxxxxxxx/yyyyw=""" ^
"option batch" ^
"option transfer binary" ^
"synchronize remote -filemask=""*.png>=2016-01-01<4M;*.jpg>=2016-01-01<4M "" \\FromFolder /destination/ -nopreservetime" ^
"exit" >> C:\Temp\Test.txt
echo %TIME% >> C:\Temp\Test.txt
See also WinSCP FAQ Why are some WinSCP scripting commands specified in a batch file not executed/failing?

Change PING script to TELNET script?

I need to turn this ping script into a telnet script which I'd like to configure the script to telnet to the address (on my separate target list *.txt file) & either:
connect/disconnect - write success results to file
or
fail - write fail results to file,
& go to next record, then end...
HELP? :)
#echo off
cls
echo Ping test in progress...
for /F %%i in (iplist.txt) do ping -n 3 -a %%i >> result.txt
echo .
echo .
echo Result is ready.
You cannot simply replace ping with telnet. For one thing, the telnet shipped with Windows isn't scriptable in the first place, so you'd have to jump through some hoops to make it work in a script. You'd be better off using a telnet that's actually scriptable, like plink from the PuTTY suite. Also telnet clients can talk to arbitrary services, so you need to specify where you want to connect to (a telnet server uses a different protocol than, say, a web server or a mail server).

automating telnet session in windows is not working

Hi I have created a file temp.txt with the the content as below
root
root123
ls -lrt
exit
and then I am excuting the below command in dos
c:\>telnet machine_ip < temp.txt
Output is
Welcome to the Microsoft Telnet client
Escape character is 'ctrl+]'
C:\> Telnet>
How to automate the telnet session in windows with this method. I cant use any other exe(putty.exe) or tool to do this. I have to use the available things in windows xp. Suggest me also if any other way is there.
I am not sure if you can pass the username and password to telnet program from a text file.
You can create a vbs script and use sendkeys to pass username and password.
Create a batch script which starts a telnet session of a box and then call the vbs script from the batch script using
CSCRIPT //NoLogo //B "C:\Path\to\Script>script.vbs"
Hope this helps!

how can I save the output of windows internal FTP client?

UPDATED: on win2k it seems it works OK. Sorry for the confusion.
MS Windows Vista internal ftp client has a strange behavior.
When redirecting its output and error to a file, the errors do not show up there:
ftp -n -s:commands.txt host >output.log 2>&1
When running it from Task Scheduler inside a batch file, I don't get any error messages if connection refused. Even if echo is on or with the -d option.
Do you have a workaround for it?
Not sure what's happening to you. I'd check the batch file that's running it, or maybe how you're scheduling the job.
The output is confusing as the error seems out of order, but redirecting stderr seems to work on my XP machine:
C:\Temp>ftp -s:ftpcmds.txt ftp.microsxoft.com >ftplog.txt 2>&1
C:\Temp>type ftplog.txt
ftp> Not connected.
ftp> USER sconners
Invalid command.
ftp> PASS skynet.com
Not connected.
ftp> PUT test.txt test1.txt
BYE
> ftp: connect :Unknown error number
C:\Temp>
have you tried it without the "2>&1"?
we do this all the time but in this format
ftp -s:FTPCMD.FTP > R:\foo\bar\FTPGET.LOG

Resources