GPG unattended encryption not giving output - batch-file

I have created a script which will encrypt a file using GPG and upload it to a target.
The script is as follows:
cd C:\test\outfeeds\
echo ---------- >> C:\test\log.txt
date /T >> C:\test\log.txt
echo %time% >> C:\test\log.txt
echo %USERNAME% >> C:\test\log.txt
echo ---------- >> C:\test\log.txt
echo --- Encrypting Files --- >> C:\test\log.txt
for %%a in (*.txt) do (
gpg --allow-weak-digest-algos -o %%a.pgp -e -r "testkey" %%a >> C:\test\log.txt
)
echo --- Transferring Files --- >> C:\test\log.txt
sftpc -profile="C:\test\FTP\test_profile.bscp" -hostKeyFile="C:\test\FTP\Host_Key_Export" -unat=y -cmd="put *.pgp inbound/ap/" >> C:\test\log.txt
del *.pgp
move *.txt C:\test\feedarchive\
The logs show nothing for the gpg call so when i run it unattended I can see it runs under a user named testmachine$. This results in no file being available for the sftpc call (so I assume this file isn't getting made).
When I run it manually, it seems to work fine, logging nothing for the gpg call, but creating the file and uploading it successfully.
Is there a way to return what's going wrong on this call, or something obvious I am doing wrong?

Related

Filezilla not allowing connections by batch code via FTP connection

I have a bit of code that will send some info to my FileZilla FTP server that is running on a PC of mine. When I enter the password I want and place it in the login spot in my code and run it. It keeps saying it is a Incorrect password and failing even when I KNOW that the password in there is correct. Someone have an answer?
1 I have tried to change the password many times and even simple ones like 123 and it still did not say it was correct |
2 I changed the security on the server many time as well and to nothing in return |
3 I have reinstalled Filezilla many times |
4 I changed firewall settings |
REM Setup the FTP folder
echo reverseCMD > a.dat
echo *********** >> a.dat
echo binary >> a.dat
echo mkdir %username% >> a.dat
echo cd %username% >> a.dat
echo put Info.txt >> a.dat
echo disconnect >> a.dat
echo bye >> a.dat
*** Removing IP's and passwords
Are you aware of the fact that all lines of your batch file output with echo are written with a trailing space into file a.dat because of space left to redirection operators > and >>?
See the answer on Why does ECHO command print some extra trailing space into the file? for details about how a command line with echo and a redirection operator is processed by cmd.exe.
I suggest the following code:
#echo off
REM Setup the FTP folder
(
echo reverseCMD
echo ***********
echo binary
echo mkdir %username%
echo cd %username%
echo put Info.txt
echo disconnect
echo bye
) > a.dat
And make sure the batch file does not contain trailing spaces/tabs on the lines with echo.

how to get command executed appended as output filename in batch script

I have batch script which read and executes command defined in command.txt on all the servers which ever mentioned in server.txt file using for loop.
problem is,am trying to get output/result file as "server_command.log" for each command executed.
I can easily get the server details but for commands, I need to get the last word from the command.txt file since each command has special character ie, /
command.txt content :
show /system1/firmware1
show /system1/fan1
server.txt will have ip address of target server :
10.1.1.101
10.1.1.103
10.1.1.105
output file am trying to get should be :
101.1.1.101_firmware1.log (file will contain fireware1 command output)
101.1.1.101_fan1.log (file will contain fan1 command output)
101.1.1.103_firmware1.log (file will contain fireware1 command output)
101.1.1.103_fan1.log (file will contain fan1 command output)
101.1.1.105_firmware1.log (file will contain fireware1 command output)
101.1.1.105_fan1.log (file will contain fan1 command output)
my batch file:
for /f "tokens=1" %%a in (.\config\serverlist.txt) do (
echo _
echo ----- Opening connection to: %%a at %TIME% ----- >> %LOGFILE%
for /f "tokens=*" %%c in (.\config\commands.txt) do (
set MyCmd=%%c
set Server=%%a
echo --------- %%c and %%a ------------------
set MyLog=.\Logs\!Server!_%MyDate%.!MyCmd!.log
.\plink -ssh %username%#!Server! -pw %password% "!MyCmd!" >> !MyLog!
)
echo. >> %LOGFILE%
echo ----- Closing connection to: %%a at !TIME! ----- >> %LOGFILE%
)
Thanks
replace your special symbol (/) with a space and use a simple for to get the last element:
...
set MyCmd=%%c
for %%X in (!MyCmd:/^= !) do set MyCmd=%%X
echo !MyCmd!
...

How to extract error descriptions from a batch file

I have this batch script:
#echo off
set server_list=C:\temp\servers.txt
set cab=E:\PHC\wsusscn2.cab
set sass=E:\PHC\SASS\sass.exe
erase c:\sass\error.log
for /f "tokens=1,2*" %%i in (%server_list%) do (
echo %%i
xcopy %cab% \\%%j\c$\sass /Y || echo %%i: ?error description (invalid drive specification, Access denied etc)? >> c:\sass\error.log
xcopy %sass% \\%%j\c$\sass /Y || echo %%i: ?error description (invalid drive specification, Access denied etc)? >> c:\sass\error.log
wmic /node:%%j PROCESS CALL CREATE "cmd /c c:\sass\sass.exe -i c:\sass\wsusscn2.cab -o c:\sass\%%i.csv" || echo %%i: ?error description (RPC server not available etc)? >> c:\sass\error.log
)
pause`
The script works fine, problem is, I can't get the error descriptions to the error.log file (between the question marks). It would help greatly, if I knew, why exactly the operation was unsuccessful. I tried the following:
echo %%i: cab file copy failed - %ERRORLEVEL%
But it always results in errorlevel 0 (obviously errorlevel of the echo command). I tried several ways to store the errorlevel to a variable, but always unsuccessfully. Furthermore, errorlevel only gives me partially what I want, not the descriptions, but just type of error.
The file Servers.txt contains hostnames of servers in one column, and their IPs in the second column.
How can I extract the error descriptions?
Problem was solved by using STDERR. This is working form of the questioned lines:
xcopy %cab% \\%%j\c$\sass /Y 2>> C:\SASS\error.log
xcopy %sass% \\%%j\c$\sass /Y 2>> C:\SASS\error.log
wmic /node:%%j PROCESS CALL CREATE "cmd /c c:\sass\sass.exe -i c:\sass\wsusscn2.cab -o c:\sass\%%i.csv" 2>> C:\SASS\error.log
This code types precisely what I need to the error.log file. Since this solution does not state the hostname of the server, I had to add these lines to the beginning of the loop:
echo ----------- >> C:\SASS\error.log
echo %%i >> C:\SASS\error.log

Windows batch Script to select week number when calling a file

I have considered to incorporate a batch file in my SSIS package to put a file into an SFTP site via winscp.com and therefore need to write a batch file at the end which can put the needed file into a SFTP site, for which I have written the process below but I need help to call the file which looks like Filename_Week #(eg. Filename_1,file name_2...Filename_34) and is located M:\Test\DATA OUT folder. My feeling is to add an if statement (i.e. if [%v_week%] == [] SET v_week= week #) after set v_week=%1, but I am not sure what that would be since I have never written a batch file before.
SET v_week=%1
echo option batch continue > SFTP_Filename_Put.txt
echo option confirm off >> SFTP_Filename_Put.txt
echo open target >> SFTP_Filename_Put.txt
echo lcd "M:\Test\DATA OUT" >> SFTP_Filename_Put.txt
echo cd / >> SFTP_Filename_Put.txt
echo put "M:\Test\DATA OUT\Filename_%v_week%.txt" /Filename_%v_week%.txt >> SFTP_Filename_Put.txt
echo exit >> SFTP_Filename_Put.txt
M:\temp\apps\WinSCP\winscp.com/script="M:\Development\SFTPBatchFiles\SFTP_Filename_Put.txt"
del SFTP_Filename_Put.txt
Like this it will automatically get the last created .fyi file and get the last 2 chars
and set your V_week variable.
#echo off&cls
setlocal EnableDelayedExpansion
for %%a in (*.fyi) do set $WeekNb=%%~na
set $WeekNb=%$WeekNb:~-2%
set v_week=%$WeekNB%
echo The Week Number Is : %v_week%
pause

Batch Program Problem

I am currently trying to program a batch script which allows the user to enter the name of a website, and then writes "127.0.0.1 www.website.com" at the bottom of the user's hosts file (essentially blocking that website)
Everything is working except for one line. I need to write the following line into another batch file which will be created by my program:
echo find /v "%url%" < C:\WINDOWS\System32\drivers\etc\hosts > C:\Users\%username%\desktop\temp.txt >> unblock.bat
This line is part of the code which will be able to remove the website from the hosts file if the user wishes to.
The problem appears to be the "<" and ">" signs. The program will not allow me to write these to the new batch file. I have tried to save them as variables and realized that the only way i could do it was by declaring them with inverted commas like this:
set char1="<"
set char2=">"
and then my command looks like this:
echo find /v "%url%" %char1% C:\WINDOWS\System32\drivers\etc\hosts %char2% C:\Users\%username%\desktop\temp.txt >> unblock.bat
The problem with this is that when writing to the new batch file, they both still have "" around them which makes the new batch file useless as the command does not execute properly.
Any ideas on how to fix this?
Here's the whole code for the batch file (incomplete):
#echo off
TITLE Site Blocker
SET /P url=Enter website (e.g. www.facebook.com)-
echo. >> C:\WINDOWS\System32\drivers\etc\hosts
echo 127.0.0.1 %url% >> C:\WINDOWS\System32\drivers\etc\hosts
echo find /v "%url%" < C:\WINDOWS\System32\drivers\etc\hosts > C:\Users\%username%\desktop\temp.txt >> unblock.bat
echo del C:\WINDOWS\System32\drivers\etc\hosts /Q >> unblock.bat
echo ren C:\Users\%username%\desktop\temp.txt hosts >> unblock.bat
echo copy C:\Users\%username%\desktop\hosts C:\WINDOWS\System32\drivers\etc\ >> unblock.bat
echo del C:\Users\%username%\desktop\hosts /Q >> unblock.bat
echo msg * %url% unblocked >> unblock.bat
echo del unblock.bat >> unblock.bat
echo exit >> unblock.bat
exit
Escape the greater than and less than with a caret (^)

Resources