0

I am trying to setup a batch script which can connect to a set of servers and execute start script. Since there is a password getting saved in the commands.txt file. I need to delete it after the execution of start on remote servers. But that del command gets executed before everything and which is causing issues for the loop and errors out that commands.txt file is missing. Not sure how thats getting executed before the loop when its put after the loop. How can I fix this?

Below is the code I am trying.

@echo off

Echo Please enter your password in the popup window and then press enter
set tempbat="%temp%\p.cmd"

REM Create temporary batch file to make popup window for entering password 'masked'
echo mode 20,1 >%tempbat%
echo color EF >>%tempbat%
echo Title Enter Password >>%tempbat%
echo setlocal enabledelayedexpansion >>%tempbat%
echo set /p Pass= >>%tempbat%
echo echo !pass!^>"%temp%\pass.txt" >>%tempbat%
echo exit >>%tempbat%
echo exit >>%tempbat%

 start /wait "" %tempbat%

 set /p Password=<"%temp%\pass.txt"
 @echo echo %password% ^| sudo -S -u x0ats echo startup.sh start>> 
 %cd%\commands.txt
 @echo read>> %cd%\commands.txt


 for /f "delims=" %%a in (%cd%\serverlist.txt) DO ( 

 Start PuTTY username@%%a -pw %password% -m "%cd%\commands.txt"


 )

 del %cd%\commands.txt}
2
  • You seem to be running this in something else, otherwise, why woudl you have that Squirly brace at the end, is it a Powershell script block?> Commented Jul 11, 2019 at 18:01
  • The issue is yhow you are not wrapping the path for Temp Bat in Double Quiotes I have fixed that for you and will repost as an answer Commented Jul 11, 2019 at 18:03

1 Answer 1

1

There are a few issues in your CMD script, but most specifically, as I mentioned above you are not putting double quotes around paths.

Additionally you were adding spaces to the ends of all of your line sin the bat file but that will cause you to collect the wrong PW, you also didn't delete your temp bat file or password file after the fact.

It seems like a bit of trouble to go through just to pop up a separate window, you could forgo creating a second batch file and call a sub function instead by making your script support cmd line arguments.

In Any case this should work more as you expect:

@ECHO OFF

ECHO.Please enter your password in the popup window and then press enter
SET "_TempBat=%temp%\p.cmd"
SET "_PWFile=%temp%\pass.txt"
SET "_CMDs=%cd%commands.txt"

REM Create temporary batch file to make popup window for entering password 'masked'
ECHO.SETLOCAL>"%_TempBat%"
REM ECHO.ECHO OFF >"%_TempBat%"
ECHO.mode CON COLS=42 LINES=1 >"%_TempBat%"
ECHO.color CF>>"%_TempBat%"
ECHO.Title Enter Password >>"%_TempBat%"
ECHO.SET /p "_Pass=Enter Password: " >>"%_TempBat%"
ECHO.ECHO.%%_pass%%^>"%_PWFile%">>"%_TempBat%"
ECHO.exit>>"%_TempBat%"
ECHO.exit>>"%_TempBat%"


START /wait "" "%_TempBat%"
DEL /F /Q "%_TempBat%"

IF NOT EXIST "%_PWFile%" (
  "%_PWFile%" Not Created!
) ELSE (
  FOR /F "Tokens=*" %%A IN ('TYPE "%_PWFile%"') DO (
    SET "_PW=%%A"
    DEL /F /Q "%_PWFile%"
  )
)


 @ECHO.ECHO.%_PW% ^| sudo -S -u x0ats ECHO.startup.sh start>> "%_CMDs%"
 @ECHO.read>> "%_CMDs%"


for /f "delims=" %%a in ('Type "%_CMDs%"') DO ( 
  Start PuTTY username@%%a -pw %_PW% -m  "%_CMDs%"
)

Pause

DEL /F /Q "%_CMDs%"

ALSO I changed the colors you chose Bright Yellow on Bright right is hardly readable, I used Bright white on Bright Red. oh and I also added an Echo Off into your second script because again that was not helpful to have all the extra stuff in there

Also I looked at this and noticed that you were not going to get the PW saved in the PW file so I fixed that by using %% so that the first % will get stripped off.

Sign up to request clarification or add additional context in comments.

4 Comments

I have to execute some commands on the remote servers. And that's what is required in the commands.txt. and a list of server ips I have in the serverlist. I dont see this script in that way. So basically requirement is like this. login to each server listed in the serverlist file and execute the commands mentioned in commands.txt and then delete this commands.txt file. I wanted to mask the password, thats why i kept the color unreadable.
@RahulR In all honesty then you should just use a call to Powershell to collect the PW, alternatively I'm sure you could use Choice to collect each character silently with somework.
My question is still unanswered. Why cant I delete the commands.txt after the for loop execution. Why it gets executed before 'for loop' even though its kept after 'for loop' in the script. p.s sorry for my ignorance, i am trying batch very recently to automate this. So I am using the logic of shell script here. shell script gets executed line by line. I am clueless here that how come delete command gets executed before the for loop
@RahulR It does not execute before the other lines. You did not ever call the bat file and colect the data because you did not wrap your directories in double quotes and warp your setsvthat way too creating issues where you did not runany code. Bash abd CMD both work sequentially, the issue is all only due to your formarting of the code being to the wrong standard. Further, I ran the abice code and irlt worked as expected, although I ech out the commands for testing instead of actually connecting to servers. I realize I did not remove that wch, so I will do so now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.