PuTTY -m command option failing with "unable to open ... file" on Windows 7 - batch-file

I'm trying to make an automatic PuTTY login using a batch file. I have this:
start "title" "C:\Program Files\PuTTY\putty.exe" "server_name#server_IP" -pw "password" -m "commands_to_execute.txt"
Everything works on my Windows 10 machine but in Windows 7, the command option -m does not work. The error message is:
unable to open command file:"commands_to_execute.txt"
I have tried changing all paths to "C:\Program Files\PuTTY", setting the working directory /D, working in the actual directory and I also add the path to the enviroment variables in:
Advance system settings >Enviroment Variables
I have also used plink instead of putty.
What is happening?

It is very unlikely that your problem has anything to do with Windows 7 vs Windows 10.
Most likely the working directory for your batch file execution on Windows 7 is not set to the folder, where the commands_to_execute.txt file is stored.
Possible solutions are:
Set the working directory the same way you have set it on Windows 10
Use a full path to the script file:
-m "C:\path\to\commands_to_execute.txt"
Set working directory for PuTTY explicitly using:
start "title" /D "C:\path\to" "C:\Program Files\PuTTY\putty.exe" ...
Or, if the script file is in the same folder as your batch file, you can use:
start "title" /D "%~dp0" "C:\Program Files\PuTTY\putty.exe" ...

you need to use plink.exe for this not putty.exe, just replace:
start "title" "C:\Program Files\PuTTY\plink.exe" "server_name#server_IP" -pw "password" -m "commands_to_execute.txt"
or make it even easier:
cd C:\Program Files\PuTTY\
plink.exe -ssh pi#192.168.1.166 -P 22 -pw P#SSWRD ~/script.sh
plink.exe -ssh pi#192.168.1.166 -P 22 -pw P#SSWRD -m commands.txt
pause
either of the two lines work.

Related

How to trigger shell script in Unix Server using PSCP Commands? [duplicate]

I need to execute a shell script remotely inside the Linux box from Windows
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Illegal number of parameters"
exit
fi
echo "$1"
Here is the command I ran from Windows command prompt
cmd> plink.exe -ssh username#host -pw gbG32s4D/ -m C:\myscript.sh 5
I am getting output as
"Illegal number of parameters"
Is there any way I can pass command line parameter to shell script which will execute on remote server?
You misunderstand how the -m switch works.
It is just a way to make plink load the commands to send to the server from a local file.
The file is NOT uploaded and executed on the remote server (with arguments).
It's contents is read locally and sent to the server and executed there as if you typed it on a (remote) command line. You cannot give it arguments.
A workaround is to generate the file on the fly locally before running plink from a batch file (say run.bat):
echo echo %1 > script.tmp
plink.exe -ssh username#host -pw gbG32s4D/ -m script.tmp
Then run the batch file with the argument:
run.bat 5
The above will make the script execute echo 5 on the server.
If the script is complex, instead of assembling it locally, have it ready on the server (as #MarcelKuiper suggested) and execute just the script via Plink.
plink.exe -ssh username#host -pw gbG32s4D/ "./myscript.sh %1"
In this case, as we execute just one command, you can pass it on Plink command line, including the arguments. You do not have to use the -m switch with a (temporary) file.
I triggered the Shell script in "commands.txt" from Plink which worked for me like a charm with below method I tried:
You can define your script as an one liner using && in a file (I defined in one liner)
You need to run your command in <
Note: Use first EOF in quote like <<'EOF' but not the last one. Else you will see you code will behave weirdly.
Please see below.
Example:
sudo -i <<'EOF'
<your script here>
EOF
Then, finally run it using Plink:
plink -ssh username#hostname -pw password -m commands.txt
Have you tried putting the command and argument in quotes:
i.e. -m "C:\myscript.sh 5"

Run SSH commands in Batch (Windows)

I need to login to the putty and run the below commands to complete the task.
putty.exe -ssh user#host -pw password -m c:\user\batchcommands.txt
1st Command :
cd/u01/app/oracle/user_projects/domains/COLLECT/EOD/bin_arm
2nd Command :
./FileUpload.sh
3rd Command :
cd/u01/app/oracle/user_projects/domains/COLLECT/EOD/bin_arm
4th Command :
./execute_eodarx.sh
How can I run these commands serially (completion of previous command)?
Write all commands in file with extension *.bat for e.g. auto_script.bat
Bat file is like shell scripts in windows and runs command in synchronous way.
then
putty.exe -ssh user#host -pw password -m "c: && cd path_to_file && auto_script "
Note : just use filename (of the bat file) as command in the directory. how to run bat file for cmd

Command Prompt (cmd) is not executing Plink SSH Command

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

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

Vmware commands executing batch file

net use x: "\\vmware-host\Shared Folders\ShareME">>logfile.txt 2>&1
ping -n 11 127.0.0.1 > nul
x:
cd "Firefox18"
call autorun.bat>>logfile.txt
cd ../
exit
When I execute the above batch file by double clicking, logfile.txt is generated. But when I use vmrun -T ws -gu <Username> -gp <Password> runProgramInGuest <Vm-Path> <path of Batch File to execute> The scripts runs fine, only the logfile.txt is not generated.
This happens only in windows 7. It works fine for Windows XP.
The problem solved I just needed >>"C:\Users\Ebryx 2\Desktop\logfile.txt" 2>&1
The quotation mark for the path seemed to work for windows 7.

Resources