Command Prompt (cmd) is not executing Plink SSH Command - batch-file

I am trying to execute a batch file (.bat) with the following commands in the batch file.
I have a batch file containing the following;
#ECHO OFF
cmd.exe /K "cd C:\Program Files (x86)\PuTTY && C:"
set PATH=%PATH%;C:\Program Files (x86)\PuTTY
pause
plink.exe -ssh username#firewall1 -pw PassWord! < commands.txt > c:\output_.csv"
pause
the plink.exe command works when entered in manually.
commands.txt is just a simple firewall command for now.
All I see when running the batch file is a cmd window open point at the Putty folder, and that's it.
So how can i get this to run please?

Your batch file at line 2, opens a new cmd window. That's not probably what you want. Also it changes the current directory and the current drive, read HELP CD, and then change that line to just cd /d "C:\Program Files (x86)\PuTTY"
Also, as a bonus recommendation, you are changing the PATH every time you run this command, adding the putty directory that you mage as current anyway, so this command is unnecessary and redundant.
So, I would leave your bat as simple as
#ECHO OFF
CD /D "C:\Program Files (x86)\PuTTY"
plink.exe -ssh username#firewall1 -pw PassWord! <commands.txt >c:\output_.csv

I would suggest making the batch file simpler still:
#Start "" /D "%ProgramFiles(x86)%\PuTTY" plink.exe -ssh username#firewall1 -pw PassWord! <commands.txt >C:\output_.csv
You may consider running other options such as /MIN, type Start /? at the command prompt for usage information

Related

How can I use a batch file to write to a cmd file?

i want to write some codes in cmd by batch file
{CreateObject("WScript.Shell").Run "%comspec% /c start /wait cmd.exe", 0, True }
this code opens cmd.exe ..
How can I edit this file with my own dos commands ?
i dont want use it in txt file?
my question is to write some command lines to cmd.exe
not to txt file by "echo > & echo >> "
i need to open cmd and start write instructions with a batch file
and how i get some information from the cmd and place it in another script to use it later
Wscript.shell.run uses shell32 to start files. You ask it to specifically start cmd.exe (%comspec%), which you ask that cmd.exe to ask shell32 (the start command) to start cmd.exe. Put the commands you want to run in a batch file (that's a text file).
Wscript.shell.run "c:\folder\batchfile.bat"
or if you prefer
Wscript.shell.run "cmd /c c:\folder\batchfile.bat"
or if only one command
Wscript.shell.run "cmd /k dir"
see
cmd /?
I wanted to start cmd, then start write code which was saved in batch file
The code will be like this:
{ START cmd.exe /k "netsh wlan show profiles" }
This code will open cmd and show you the saved profiles

How to run a cmd command from bat file?

I need to run this command in the command prompt automatically:
msiexec /i "My application"/qn
How can I write a batch file to do this?
Batch files are, for all intents and purposes, just lists of cmd commands. Just take that command, paste it into notepad, and save it with a .bat extension.
It's generally considered good practice to start the batch script with the line #echo off so that the lines of the script don't display as they're being executed.
#ECHO OFF
msiexec/i "My application"/qn
To run the batch file when the VM user logs in:
Drag batch file itself to Start - All Programs - Startup. Now when you login as that user, it will launch the batch file.
you may find the Batch HowTos helpful.
Examples: see Link
Install:
msiexec /i "C:\Install\ss64app.msi"
UnInstall:
msiexec /uninstall "{5AFF6499-63BA-4A36-83B2-8D256404AC3D}" /log "C:\install\ss64app.txt"
Autorun
1. create text file
2. edit with Notepad
[autorun]
icon=drive.ico
open=launch.bat
open=launch.bat
[autorun]
open=\folder1\runinstallers.bat
open=\folder2\PLSetup_2.exe
3. save the file as Autorun.inf
4. copy the Autorun.inf to the root folder of your CD-ROM

Batch file command not working

I am trying to run this batch file remotely It will kill the IE process's but when I try to open a .lnk file it won't do it. When I go onto that machine, open up the command prompt and type in the command to run the .lnk file it works with no issues.. please help!
Code to remotely execute batch file:
psexec -u Administrator -p password -i -d \\hostname "c:\Emergency_POD\test.bat"
Code on machine to run: (Only the taskill command works.. not the for command)
cd/
taskkill /im iexplore.exe /f
for %a in ("C:\Emergency_POD\*.lnk") do #start "" "%a"
Command to run on cmd (This command works with no issues:
for %a in ("C:\Emergency_POD\*.lnk") do #start "" "%a"
You'd probably be better off with %%a in place of %a in the batch file.

How do I execute cmd commands through a batch file?

I want to write a batch file that will do following things in given order:
Open cmd
Run cmd command cd c:\Program files\IIS Express
Run cmd command iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
Open Internet Explorer 8 with URL= http://localhost:8088/default.aspx
Note: The cmd window should not be closed after executing the commands.
I tried start cmd.exe /k "cd\ & cd ProgramFiles\IIS Express", but it is not solving my purpose.
So, make an actual batch file: open up notepad, type the commands you want to run, and save as a .bat file. Then double click the .bat file to run it.
Try something like this for a start:
c:\
cd c:\Program files\IIS Express
start iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
start http://localhost:8088/default.aspx
pause
I think the correct syntax is:
cmd /k "cd c:\<folder name>"
This fixes some issues with Blorgbeard's answer (but is untested):
#echo off
cd /d "c:\Program files\IIS Express"
start "" iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
timeout 10
start http://localhost:8088/default.aspx
pause
cmd /c "command" syntax works well. Also, if you want to include an executable that contains a space in the path, you will need two sets of quotes.
cmd /c ""path to executable""
and if your executable needs a file input with a space in the path a another set
cmd /c ""path to executable" -f "path to file""
#echo off
title Command Executer
color 1b
echo Command Executer by: YourNameHere
echo #################################
: execute
echo Please Type A Command Here:
set /p cmd=Command:
%cmd%
goto execute
start cmd /k "your cmd command1"
start cmd /k "your cmd command2"
It works in Windows server2012 while I use these command in one batch file.
cmd /k cd c:\
is the right answer
I was trying to run a couple of batch files parallely at startup, if a condition was true.
For this I made a parent batch file which should have checked for the condition and invoke the other child batch files if the condition was true.
I tried to achieve it via START but it gave me an empty black command prompt running in the directory of children batch files, instead of running the children batch files themselves
The thing which worked for me was by using a combination of START and CALL
As an example
condition ...
start call "C:\Users\Amd\conn\wsl_setup - conn1.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn2.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn3.bat"
I know DOS and cmd prompt DOES NOT LIKE spaces in folder names. Your code starts with
cd c:\Program files\IIS Express
and it's trying to go to c:\Program in stead of C:\"Program Files"
Change the folder name and *.exe name. Hope this helps

Execute multi command in batch

I tried to run multi-windows commands inside one opened window from a batch file.
I want the opened command window to perform two things in sequence:
Switch volume
Direct to a directory in that volume.
Here's what I wrote:
start cmd /k C: && cd 'C:\Program Files (x86)\aaa\'
However, this only switches volume. The second thing is not executed.
Can anyone please show me the way?
Well, you have at least 2 options...:
1st, make sure your && is passed to new cmd...
start cmd /k "C: && CD c:\temp"
2nd, use /d switch on cd to "get there" in one step...
start cmd /k cd /d c:\temp
KR
Bartek
What don't you just open your cmd at the needed directory? Like^
start /dc:\temp cmd
If you want to change directory to another drive you can use
cd /d C:\
but if your changing directory within the same drive you shouldn't need to switch drives, just change to that directory:
cd "C:\Program Files (x86)\aaa"
Remember to put quotes around paths with spaces, possibly why your command didn't work earlier.
Also, you shouldn't really need start and cmd. What you doing doesn't really need to be threaded as such. If it's a batch file you can just use pause at the end instead of using cmd /k.
Your complete batch file would then look like this:
cd "C:\Program Files (x86)\aaa"
pause >nul
or using cmd /k for one line (in case of command line use):
cmd /k cd "C:\Program Files (x86)\aaa"
Hope this helps!

Resources