Creating Ftp txt file in Batch Script - batch-file

I am running a batch script and trying to create a txt file with the file information. Below is what I have written. The PROJECT_PATH AND REMOTE_FTP_OUTPUT_PATH can change depending on what is assigned to the variable in the script. I am getting errors. Here is the part of the script to create the file:
echo **** PROJECT PATH IS *** %PROJECT_PATH%
echo **** directory location is ***** dir
REM ***** Creat ftp.txt dynamically *********************************
echo open clientftp.accuweather.com > !PROJECT_PATH!\ftp.txt
echo user>> %PROJECT_PATH%\ftp.txt
echo accubot >>%PROJECT_PATH%\ftp.txt
echo HpD7f1sTymq2 >>%PROJECT_PATH%\ftp.txt
echo cd download\mobile_builds\android\v3 >>%PROJECT_PATH%\ftp.txt
echo if not exist %REMOTE_FTP_OUTPUT_PATH% mkdir %REMOTE_FTP_OUTPUT_PATH% >>%PROJECT_PATH%\ftp.txt
echo cd download\mobile_builds\android\v3\%REMOTE_FTP_OUTPUT_PATH% >>%PROJECT_PATH%\ftp.txt
echo put %APK_FULL_OUTPUT% >>%PROJECT_PATH%\ftp.txt
echo quit >>%PROJECT_PATH%\ftp.txt
and here is my output when I run for those lines:
**** PROJECT PATH IS *** temp_builds\6228-10.06
**** directory location is ***** dir
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
Error opening script file temp_builds\6228-10.06\ftp.txt.
Transfers files to and from a computer running an FTP server service
(sometimes called a daemon). Ftp can be used interactively.
FTP [-v] [-d] [-i] [-n] [-g] [-s:filename] [-a] [-A] [-x:sendbuffer] [-r:recvbuffer] [-b:asyncbuffers] [-w:windowsize] [host]
-v Suppresses display of remote server responses.
-n Suppresses auto-login upon initial connection.
-i Turns off interactive prompting during multiple file
transfers.
-d Enables debugging.
-g Disables filename globbing (see GLOB command).
-s:filename Specifies a text file containing FTP commands; the
commands will automatically run after FTP starts.
-a Use any local interface when binding data connection.
-A login as anonymous.
-x:send sockbuf Overrides the default SO_SNDBUF size of 8192.
-r:recv sockbuf Overrides the default SO_RCVBUF size of 8192.
-b:async count Overrides the default async count of 3
-w:windowsize Overrides the default transfer buffer size of 65535.
host Specifies the host name or IP address of the remote
host to connect to.
Notes:
- mget and mput commands take y/n/q for yes/no/quit.
- Use Control-C to abort commands.

Related

set /p failing to read sftp mapped file

I'm trying to write a batch file to act as an interface between Source-Insight, and Git running on a Linux server, but I'm running into an issue where the set /p does not seem to be working as advertised.
The batch file is supposed to run a linux script (via plink), which will check out the appropriate files into two directories, and then invoke Beyond Compare to compare the directories (note, these are on an sftp mounted drive so that dos can see them). The directory names are dynamic, so I need the batch file to read the generated directory name from a file before passing it to Beyond Compare. I can't seem to get this working...
I have the following lines in my batch script:
#echo on
plink server1234 -l %user% -i %ppk_file% "cd %root%; ~/bin/compare_git_all.sh --debug --ref"
echo "set /p dir=<%dosroot%\.comparegit.dosdir"
set /p dir=<%dosroot%\.comparegit.dosdir
echo dir="%dir%" (from %dosroot%\.comparegit.dosdir)
"C:\Program Files\Beyond Compare 4\BCompare.exe" %dir%.refpt %dir%
#echo off
My output ends up being:
"set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir"
dir="" (from z:\builddir\pd2\wt1\.comparegit.dosdir)
So first issue (annoyance really), is that #echo on is not causing the commands to be echoed (which according to the pages I've google it's supposed to do...)
But what's killing me is that %dir% seems to be blank. I've verified that the file contains the data I am looking for:
C:\>more z:\builddir\pd2\wt1\.comparegit.dosdir
z:\builddir\pd2\wt1\.comparegit.zmP8BK
if I run the same command from a Command Prompt, I get:
C:\>set dir=blank
C:\>echo %dir%
blank
C:\>set /p dir=<z:\builddir\pd2\wt1\.comparegit.dosdir
C:\>echo %dir%
z:\builddir\pd2\wt1\.comparegit.zmP8BK
So, I'm missing something, but I'm not sure what it is. Any help would be appreciated. (note, if it makes any difference, the batch file is being invoked from a keymapping within Source Insight)

Using %USERNAME% environment variable in Windows batch file with command-line ftp

ftp
open ftp.drivehq.com
username
password
cd \wwwhome\Logs\
put "C:\Users\Cody\Desktop\ISO's\mini.iso"
bye
exit
How do you use %USERNAME% instead of hard-coding Cody, when used with ftp?
Here is another batch file solution with code similar to code written by Martin Prikryl with three enhancements.
%USERPROFILE% is used instead of C:\Users\%username% which makes this batch file solution work also on Windows XP and on machines on which the user's profile directory is not on drive C: or in a different directory than C:\Users which is of course possible too.
%SystemRoot%\System32\ftp.exe is used in the batch file instead of just ftp to make this batch file work also if by chance there is an ftp.* file with a file extension listed in environment variable PATHEXT in current directory or any other directory in environment variable PATH and not being the ftp executable in Windows system directory.
The ISO file name is renamed before upload with including a random decimal number between 0 and 32767 as asked for with a comment.
The command lines of enhanced batch file:
:RandomIsoName
set "RandomName=mini_%RANDOM%.iso"
if exist "%USERPROFILE%\Desktop\ISO's\%RandomName%" goto RandomIsoName
ren "%USERPROFILE%\Desktop\ISO's\mini.iso" "%RandomName%"
(
echo open hostname
echo username
echo password
echo cd \wwwhome\Logs\
echo put "%USERPROFILE%\Desktop\ISO's\%RandomName%"
echo bye
)>ftp.txt
%SystemRoot%\System32\ftp.exe -s:ftp.txt
You have to generate the ftp script using that variable:
echo open hostname>ftp.txt
echo username>>ftp.txt
echo password>>ftp.txt
echo cd \wwwhome\Logs\>>ftp.txt
echo put "C:\Users\%username%\Desktop\ISO's\mini.iso">>ftp.txt
echo bye>>ftp.txt
ftp -s:ftp.txt

Unable to add self signed certificate (.pfx file) to Local store

I am able to create .pfx file from my script but unable to add it to local store (under Certificates/Personal).
Although, I am able to double click on .pfx and install it manually.
But when using this script, it shows up in certmanager with Name X and under a different folder.
Script (batch file):
#ECHO OFF
ECHO.
echo %*
cd C:\Program Files (x86)\Windows Kits\8.1\bin\x64
makecert -sv SimulatorCertificate.pvk -n "CN=X" SimulatorCertificate.cer -r
pvk2pfx -pvk SimulatorCertificate.pvk -spc SimulatorCertificate.cer -pfx SimulatorCertificate.pfx -pi password
//THIS IS THE LINE RESPONSIBLE FOR ADDING IT TO STORE
CERTUTIL -f -p password -importpfx "SimulatorCertificate.pfx"
echo "done"
ECHO.
PAUSE
CLS
EXIT
Command prompt says:
When I check in certmgr (here):
There is no certificate added inside personal. Instead, the new certificate gets added in other folder as shown, with name X.
Why is this happening? Where am I going wrong?

Batch file for PuTTY/PSFTP file transfer automation

I have a batch file for moving file from my local PC to server through SFTP. I have PuTTY installed in my system and the batch file code follows.
cd C:\Program Files (x86)\PuTTY
psftp
open <IP>
<user>
<PW>
cd /home/irisuser/iris/integration/dls_dlsblr_dlschnn_in_msg/in
lcd d:\
put log.sh
bye
The above code perfectly works when I type it in command prompt. But when I double click the .bat file and run it, it's not running and asking for username and password to be entered. My aim was to automate the whole thing and I need to run it by simply clicking the .bat file. But am not able to achieve it. Any ideas or snippets will help me.
You need to store the psftp script (lines from open to bye) into a separate file and pass that to psftp using -b switch:
cd "C:\Program Files (x86)\PuTTY"
psftp -b "C:\path\to\script\script.txt"
Reference:
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter6.html#psftp-option-b
EDIT: For username+password: As you cannot use psftp commands in a batch file, for the same reason, you cannot specify the username and the password as psftp commands. These are inputs to the open command. While you can specify the username with the open command (open <user>#<IP>), you cannot specify the password this way. This can be done on a psftp command line only. Then it's probably cleaner to do all on the command-line:
cd "C:\Program Files (x86)\PuTTY"
psftp -b script.txt <user>#<IP> -pw <PW>
And remove the open, <user> and <PW> lines from your script.txt.
Reference:
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter6.html#psftp-starting
https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-pw
What you are doing atm is that you run psftp without any parameter or commands. Once you exit it (like by typing bye), your batch file continues trying to run open command (and others), what Windows shell obviously does not understand.
If you really want to keep everything in one file (the batch file), you can write commands to psftp standard input, like:
(
echo cd ...
echo lcd ...
echo put log.sh
) | psftp <user>#<IP> -pw <PW>
Though this has side effects. For example, if the host is not known to plink (like if you run it first time on a new machine or under another local account, for example under Task Scheduler), the first line of input will be taken as a response to the host key prompt. Anything except for y/i/Enter is interpreted as as n (connect just once, without adding the key to the cache), so even the cd command. And the rest of the script will fail as the cd does not happen.
set DSKTOPDIR="D:\test"
set IPADDRESS="23.23.3.23"
>%DSKTOPDIR%\script.ftp ECHO cd %PAY_REP%
>>%DSKTOPDIR%\script.ftp ECHO mget *.report
>>%DSKTOPDIR%\script.ftp ECHO bye
:: run PSFTP Commands
psftp <domain>#%IPADDRESS% -b %DSKTOPDIR%\script.ftp
Set values using set commands before above lines.
I believe this helps you.
Referre psfpt setup for below link https://www.ssh.com/ssh/putty/putty-manuals/0.68/Chapter6.html

Problems with batch script zip file to ftp

I am having problems with a batch script that should zip some pdf files and upload them to a FTP server.
My script looks like this:
#echo off
set d=%date:~0,2%%date:~3,2%%date:~6,4%
set d=%d: =_%
set t=%time:~0,2%%time:~3,2%%time:~6,2%
set t=%t: =0%
set file="C:\Program Files (x86)\7-Zip\7z.exe" a -r 46730_%d%_%t%.zip *.pdf
echo open my.host.name>> temp
echo myusername>> temp
echo mypassword>> temp
echo ascii>> temp
echo cd /
echo binary
echo put %file%>> temp
echo close>> temp
echo quit>> temp
ftp -s:temp
del temp
TIMEOUT /T 5
The zip files is created OK with the correct name, and I am able to open it afterwards. I'm pretty sure the problem(s) lies in the upload part of the script, but I can't find any working solution on google :-( The reciever is not able to open the zip file. He gets this error: Could'nt open the file as an archieve.
What am I doing wrong ??
You are missing to redirect the "CD /" and "Binary" instruction to the temp file...
And you are trying to set a command instruction in the "file" variable...
try this:
#echo off
set d=%date:~0,2%%date:~3,2%%date:~6,4%
set d=%d: =_%
set t=%time:~0,2%%time:~3,2%%time:~6,2%
set t=%t: =0%
Start /W "C:\Program Files (x86)\7-Zip\7z.exe" a -r "46730_%d%_%t%.zip" "*.pdf"
set "file=46730_%d%_%t%.zip"
(
echo open my.host.name
echo myusername
echo mypassword
REM Use a correct dir command, "cd /" isn't openning any folder
REM echo cd ".\folder\"
echo binary
echo put "%file%"
echo close
echo quit
)> "%TEMP%\ftp.txt"
ftp -s:"%TEMP%\ftp.txt"
TIMEOUT /T 5
If you have problems you better can use the WPUT command:
https://sourceforge.net/projects/wput/files/wput/pre0.6/wput-pre0.6.zip/download?use_mirror=freefr&download=
C:\Users\Administrador\Desktop>wput --help
Usage: wput [options] [file]... [url]...
url ftp://[username[:password]#]hostname[:port][/[path/][file]]
Startup:
-V, --version Display the version of wput and exit.
-h, --help Print this help-screen
Logging and input file:
-o, --output-file=FILE log messages to FILE
-a, --append-output=FILE append log messages to FILE
-q, --quiet quiet (no output)
-v, --verbose be verbose
-d, --debug debug output
-nv, --less-verbose be less verbose
-i, --input-file=FILE read the URLs from FILE
-s, --sort sorts all input URLs by server-ip and path
--basename=PATH snip PATH off each file when appendig to an URL
-I, --input-pipe=COMMAND take the output of COMMAND as data-source
-R, --remove-source-files unlink files upon successful upload
Upload:
--bind-address=ADDR bind to ADDR (hostname or IP) on local host
-t, --tries=NUMBER set retry count to NUMBER (-1 means infinite)
-nc, --dont-continue do not resume partially-uploaded files
-u, --reupload do not skip already completed files
--skip-larger do not upload files if remote size is larger
--skip-existing do not upload files that exist remotely
-N, --timestamping don't re-upload files unless newer than remote
-T, --timeout=10th-SECONDS set various timeouts to 10th-SECONDS
-w, --wait=10th-SECONDS wait 10th-SECONDS between uploads. (default: 0)
--random-wait wait from 0...2*WAIT secs between uploads.
--waitretry=SECONDS wait SECONDS between retries of an upload
-l, --limit-rate=RATE limit upload rate to RATE
-nd, --no-directories do not create any directories
-Y, --proxy=http/socks/off set proxy type or turn off
--proxy-user=NAME set the proxy-username to NAME
--proxy-pass=PASS set the proxy-password to PASS
FTP-Options:
-p, --port-mode no-passive, turn on port mode ftp (def. pasv)
-A, --ascii force ASCII mode-transfer
-B, --binary force BINARY mode-transfer
--force-tls force the useage of TLS
See the wput(1) for more detailed descriptions of the options.
Mail bug reports and suggestions to <itooktheredpill#gmx.de>

Resources