Encrypt username and password on batch script - batch-file

I'm trying to load flat file into server through FTP using batch files. I'm aware of the scripts and I'm able to transfer the file to server. Currently I'm following this below method.
#echo off
title File Transfer
color 3F
cd "C:\Users\username\Desktop\Access\"
echo -
echo - Transferring File to Server. Please be patient...
echo -
echo - Closing this window will Terminate the entire process
ftp -n -s:"C:\Users\username\Desktop\Access\ftp.txt" server_name >"C:\Users\username\Desktop\Access\ftp.log"
Script File Code: (ftp.txt)
User Userid password
mkdir App
put "C:\Users\username\Desktop\Access\File.txt" "App/File.txt"
quit
My requirement is that I should not display username and password on the script file. These credential information dynamically coming from the variables and I'm generating this batch file and script file dynamically.
Kindly any experts suggest me solution which makes more sense to accomplish this requirement. Thanks in advance.

You generally cannot encrypt anything while still allowing it to be decrypted automatically.
See for example BASH: allow users to FTP files to my server without revealing FTP login credentials to them
All you can typically do is to obfuscate the credentials. But the Windows ftp.exe does not allow even that.
With ftp.exe, you can use input redirection to somewhat reduce the risk:
(
echo user %USERNAME% %PASSWORD%
echo mkdir ...
echo put ...
echo quit
) | ftp.exe -n example.com

Related

Run DB2 command from a windows batch script

I am trying to run a DB2 command (Import) from a windows batch script.
My approach is..
Invoke DB2 command prompt: DB2CMD.exe DB2SETCP.BAT DB2.EXE
Connecting to the database: connect to DBNAME USER USERNAME USING PWD
Executing DB2 command: IMPORT FROM ...
quit
The batch script looks like this..
DB2CMD.exe DB2.EXE "connect to dbname USER username USING pwd" "IMPORT FROM D:\File.txt .... INSERT_UPDATE INTO tablename"
quit
It is working till the second step and the bat script is exiting without running the third step.
Can anyone guide me on this.. Thanks!
To run Db2 commands in a CMD/BAT file on Microsoft Windows, you need to have a Db2 client installed, and you can begin all of the BAT or CMD files
with the pattern below. This lets your script open the DB2CMD.EXE if that is not already opened (so you don't need to do it manually). You also need to have the database catalogued so that you can access the database from the Windows CMD.EXE or DB2CMD.EXE command line. Always verify your commands at the command-line before putting them into a script.
When you get any error, you must specify the exact error when asking for help, either by using copy/paste from the db2cmd window or by attaching a screenshot.
Your second step cannot just exit without first showing some error message, although you may need to ensure that the window does not close before you can see that message. There are many reasons for IMPORT to fail, but you have given no information about which reason Db2 gave. If you edit your question to specify the missing error detail, you will get more help on here.
If you run the script on the same hostname where the database is running then you don't need to specify a userid/password and then it connects as the currently logged on User.
If you need to connect to the database with a specific userid (different from the user logged in to Windows) or if you need to connect to a remote database then you must specify a password to the connect command. You can ask the user to enter a password, for interactive scripts. If you need to have an unattended script you should avoid hardcoding the password in plain text in the script, or use runas.
The example below shows both a connect to a local database, and a remote database (choose only one of the methods).
#if ""%DB2CLP%""=="""" db2cmd /c /i /w ""%0"" %* && goto :EOF
#rem for connecting to a local database as current logged-in Windows account (no password required)
db2 -v connect to dbname
#if errorlevel 1 ( #echo ""FAILED to connect"" && #goto :EOF )
#rem for connecting to a remote database, or connecting with a different account
db2 -v connect to dbname user YOURUSER using THEPASSWORD
#if errorlevel 1 ( #echo ""FAILED to connect"" && #pause ... && #goto :EOF )
db2 -v "IMPORT FROM TO ... "
#if errorlevel 2 ( #echo "FAILED to export..." && #pause ... && #goto :EOF )
db2 -v connect reset

Using FTP.exe to get unknown contents from a directory [duplicate]

I want to create a batch file in Windows Server, including the following functions:
Connection to a FTP server
Copying the files from there (directory called "out") to a local directory
if success, then deleting the files from the FTP server
repeating those steps every 15 minutes
I haven't done that much with batch files so far, so it would be great if you could help me. I know there is the ftp command, and I know how to connect (ftp open), but unfortunately I don't know how to copy those files from there every 15 minutes.
Thanks a lot for your help!
To program ftp from a batch file, see http://support.microsoft.com/kb/96269. You need to call ftp like this
ftp -i -s:ftpcommands.txt
where ftpcommands.txt looks something like this:
open ftp.myftpsite.com
username
password
bin
cd out
mget *
del *
bye
For running this every 15 minutes, see other replies (at or Command Scheduler).
(The -i parameter is to turn off interactive prompting - the other way to do this is to add a prompt off command to the commands text file before the mget. Without this, mget will stop and ask you to confirm before getting each file. [Thanks to Adriano for pointing this out!])
The accepted answer by #AAT suggests using Windows built-in ftp.exe command-line client. While that can work, more often it won't, because this client does support FTP active mode only, which does not play nicely with today's ubiquitous firewalls and NATs. It also does not support encrypted FTPS (FTP over TLS/SSL).
If you have a problem with the above, you need to use a 3rd party FTP client. Most of them do support both the passive mode and encryption.
For example with WinSCP FTP client, you can use the following batch file (.bat):
WinSCP.com /command ^
"open ftp://username:password#ftp.example.com/" ^
"get /out/* c:\local\path\" ^
"exit"
In case you already have an ftp.exe script, there's a guide for converting it to WinSCP script.
For the scheduling part, see the guide to scheduling transfers to FTP server.
(I'm the author of WinSCP)
Windows has the at utility as well as the Windows task scheduler. Either one can run your program at a specified interval.
Using only one (1) .bat file script. Create the FTP script in a temporary file, run it, then delete the temporary file.
SET "FTPFILE=%TEMP%\myftpscript_%RANDOM%.txt"
ECHO>>"%FTPFILE%" open ftp.myftpsite.com
ECHO>>"%FTPFILE%" username
ECHO>>"%FTPFILE%" password
ECHO>>"%FTPFILE%" bin
ECHO>>"%FTPFILE%" cd out
ECHO>>"%FTPFILE%" mget *
ECHO>>"%FTPFILE%" del *
ECHO>>"%FTPFILE%" bye
ftp -i -s:"%FTPFILE%"
IF EXIST "%FTPFILE%" (DEL "%FTPFILE%")
EXIT /B 0

Automated Bat File Not Working For Service Account

We have a file that we send to the bank and it worked via bat file when sending through FTPS.exe but not we must use PSFTP.exe which works for the desktop user but not the service account. We have tried logging in as the service account user and running the process which works when we are logged in as but not when scheduled through a Windows Server. Have also tried putting the Y in the bat file to answer the validate question whcih we have to answer only once when using desktop user without success.
I have read a lot of hits online pointing to authentication, using two files, etc... but not sure how to implement them. I would really appreciate assistance with this.
here is my bat file which is called by Gentran as an executeable, server is Windows 2008 R2, Service Pack1:
SET MOVEitIP=FTX-SERVSH.bnymellon.com
SET MOVEitUser=******
SET MOVEitPassword=******
echo cd /inbound/trp/ >> frommoveit.txt
echo mput D:\GENSRVNT\PositivePay\*.txt >> frommoveit.txt
echo quit >> frommoveit.txt
psftp -batch %MOVEitIP% -l %MOVEitUser% -pw %MOVEitPassword% -b frommoveit.txt
del frommoveit.txt
Basically took script from bat file for FTPS.exe and updated it for PSFTP.exe to use. Again, this works as desktop user and service account user (when signed into the server) but not when automated.
Thank you in advance for your assistance.
Krise
The service account must have the "Log On Locally" rights otherwise the account will not be able to logon to execute the sheduled task. I think you will see the logon error in the eventvwr at the time the task was run.

Need Help to open ftp server and login with batch

I need to make a batch file which opens my ftp server automatically,How do i automatically input user name and password to the ftp username and password prompt.I have tried to use
echo username|ftp ipadress,but it is taking the input for both user name and password. So I need help to create a batch file, which automatically logins as "user" and password "pass"
Before you use the following script, please remember that the FTP protocol
doesn't allow encryptions, the authentication process and the data transitions.
Create a new text file with the following text:
open "myservername/serverip"
"%username%"
"password"
cd /upload
bin
hash
put/get "filename.cab"
bye
Note: Please choose the correct ftp commands to allow the script to work.
Save the file as runftp.txt (For example).
Run the command: c:\windows\system32\ftp.exe -s:runftp.txt
SOURCE: http://support.microsoft.com/kb/555976

Check existance of file on ftp

I want to check existence of file on ftp server using batch script.
Right now I have this code which saves it to ftp server:
#echo off
echo user prayagimages> d:\Images\ftpcmd1.dat
echo Pass123#>> d:\Images\ftpcmd1.dat
echo put d:\Images\%~1>> d:\Images\ftpcmd1.dat
echo quit>> d:\Images\ftpcmd1.dat
ftp -n -s:d:\Images\ftpcmd1.dat 118.139.173.227
del d:\Images\ftpcmd1.dat
Lets say if I post image.jpeg to server.
How do I check whether it is on the server or not?
Thanks.
This questions seem to request similar things:
To search for a file at FTP
Windows XP Batch- IF EXIST FTP with Date Variable
Mainly the first one, but the second link has some useful info too.

Resources