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
Related
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"
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.
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
I have a .cmd batch file (let's call it RunSQlCmd.cmd) which pipes out output of sqlcmd to 7 zip compressor
sqlcmd -i.\table.sql -S . -E -s "," -I -h -1 -W| "c:\Program Files\7-Zip\7z.exe" a -tbzip2 -si "out.csv.bz2"
I run it from Azure Batch in a task from a C# driver program with the following command line
(1) cmd /c %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
But what seem to happen is that command line returns almost immediately and commands in RunSqlCmd.cmd are not fully executed and empty compressed archive is created. Azure Batch task exits with success code 0.
If I change task's command line to
(2) cmd /c start /wait %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
commands in a batch file run successfully but stdout and stderr is lost for Azure Batch as batch file runs in a separate cmd window and task hangs up without getting any error code.
changing tasks's command line to
(3) cmd /c start /B /wait %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
is similar to (1)
What is the correct way to do it so that Azure Batch detects correctly when task is finished and RunSqlCmd.cmd command finishes completely?
P.S. Real content of RunSqlCmd.cmd file is as
#echo Run SqlCmd
sqlcmd -i "%~dp0%1.sql" -d dbName -S serverName -U userName -P "password" -s "," -I -h -1 -W -b | "%ProgramFiles%\7-Zip\7z.exe" a -tbzip2 -si "%~dp0%1.csv.bz2"
#echo Done SqlCmd
It takes 1 parameter - name of sql file to pull data
I do not quite understand why you want to run cmd /c and start /wait to kick off a program. you do not need to call cmd.exe from start and do not need to call cmd to start a batch file.
if it is a batch file, you can just call the batch file. it will automatically wait internally for the command to complete.
%AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
That should work as is without calling start or cmd again.
The main problem is that you are sending the command with parameters in quotes to external program, so you you call cmd again and it sends the commands to this cmd window and stripping out quotes when it sends to the external. Just calling the script as is will do.
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