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

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"

Related

Executing "su" with PuTTY Plink from a batch file

I'm having trouble trying to get my batch file to also give me super user access in PuTTY/Plink. I can auto-input the ip and password but I want to also automate it to where it will also give me su access when I run this batch file. So far it runs up to the point where I need to manually enter su but I'm trying to automate that so I tried -m commands.txt and now it just crashes.. Looking for any help here thanks.
this is what I have so far
#echo off
color b
cls
echo What is the IP for the unit?:
set /p IP= :
echo What is the SSH Password for this unit?:
set /p Passw0rd= :
echo What is the Root Password for this unit?:
set /p ro0t= :
start plink.exe -ssh user#%IP% -pw %Passw0rd% -m commands.txt
exit/b
what in the commands.txt:
su
The su gives you a hint what's wrong, for sure. But you cannot see it because you start plink using start in a new console window that immediately disappears. Remove the start, at least when debugging the problem.
Anyway, you most likely get "standard in must be a tty" or a similar message. The su by default needs terminal for the password prompt. The Plink is meant an for an automation, not an interactive use. So it by default does not allocate a pseudo terminal for the session. To force use of the terminal, add -t switch:
plink.exe -t -ssh user#%IP% -pw %Passw0rd% -m commands.txt
A similar question: "Sudo" fails with "sudo requires a tty" when executed from PuTTY command line
As a next step, you will probably want to execute further commands within su. For that see Executing (sudo) subcommands using Plink.
Though note that in general automating su, particularly su with a password, is usually a bad idea: Allowing automatic command execution as root on Linux using SSH.

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

Piping output in cmd batch file on Azure Batch service error codes not detected

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.

How to ping a server only once from within a batch file?

I want to learn how to write batch scripts and tried to create a script which automatically runs this command in the command line once:
ping www.google.de -t
and displays the ping, so it would look like this:
Reply from XXX.XXX.X.XX: time=30ms
Reply from XXX.XXX.X.XX: time=31ms
Reply from XXX.XXX.X.XX: time=29ms
My problem is, that this will result in this when I execute this command as script:
My problem is that it will not execute the ping command at all, but just insert the command unlimited times in the console window as its shown in the screenshot.
I just created a new file, wrote ping www.google.de -t in it, saved it as ping.bat file and executed it with double clicking on it.
So how to write the batch file to start this command only once and display the ping result?
I am sure you must have named the resultant bat file as "ping.bat". If you rename your file to something else say pingXXX.bat. It will definitely work. Try it out.
my batch file contains below code only
ping 172.31.29.1 -t
with file name as ping.bat
with file name abc.bat
Enter in a command prompt window ping /? and read the short help output after pressing RETURN. Or take a look on:
ping - latest Microsoft documentation for this Windows command
ping - Windows XP documentation for this Windows command
Explanation for option -t given by Microsoft:
Specifies ping continue sending echo Request messages to the destination until interrupted. To interrupt and display statistics, press CTRL+ENTER. To interrupt and quit this command, press CTRL+C.
You may want to use:
#%SystemRoot%\system32\ping.exe -n 1 www.google.de
Or to check first if a server is available:
#echo off
set MyServer=Server.MyDomain.de
%SystemRoot%\system32\ping.exe -n 1 %MyServer% >nul
if errorlevel 1 goto NoServer
echo %MyServer% is available.
rem Insert commands here, for example one or more net use to connect network drives.
goto :EOF
:NoServer
echo %MyServer% is not available yet.
pause
goto :EOF
For bash (OSX) ping google.com -c 1 (incase search brought you here)
if you want to use the name "ping.bat", a small trick is to use this code:
#echo off
cd\
ping google.com -t
Just add that "cd\" and you are fine... ;)
Not sure exactly what you are trying but your posted code should work just fine. in case you don't want the command to be displayed, add #echo off at starting of your script. If i have the below code in a file named as test.bat and run it command prompt as test.bat it will work just fine.
#echo off
ping www.google.de -t
To address your EDIT: where the main concern is ping command was not recognizable. ping command generally will be located under C:\Windows\System32\ where C:\ being the root directory. In case, the root directory is different you can get the root directory using %SystemRoot% environment variable and can say like
%SystemRoot%\Windows\System32\PING.EXE www.google.de -t
Another way to see if the command you are trying to run is recognizable or not is using WHERE command like below
where ping
If the command is recognizable; it will output the path like
C:\Windows\System32\PING.EXE
Else will result in error
I know why, you are using the file name "ping" and you are using the code "ping", it just keeps trying to run itself because its selected directory in where that file is, if you want it to actually ping, put this before the ping command: "cd C:\Windows\system32", the actual file that pings the server is in there!
From Batch file, ping a ip only once using the following command:
Ping 192.168.199.10 -n 1
i used Mofi sample, and change some parameters, no you can do -t
#%SystemRoot%\system32\ping.exe -n -1 4.2.2.4
The only thing you need to think about in this case is, in which directory you are on your computer.
Your command line window shows C:\users\rei0d\desktop\ as your current directory.
So the only thing you really need to do is:
Remove the desktop by "going up" with the command cd ...
So the complete command would be:
cd ..
ping XXX.XXX.XXX.XXX -t
Having 2 scripts called test.bat and ping.bat in same folder:
Script test.bat contains one line:
ping google.com
Script ping.bat contains below lines:
#echo off
echo Hello!
pause
Executing "test.bat" the result on CMD will be:
Hello!
Press any key to continue . . .
Why? Because "test.bat" is calling the "ping.bat" ("ping google.com" is interpreted as calling the "ping.bat" script).
Same is happening if script "ping.bat" contains "ping google.com". The script will execute himself in a loop.
Easy ways to avoid this:
Do not name your script "ping.bat".
You can name the script as "ping.bat" but inside the script use "ping.exe google.com" instead of "ping google.com".
Create a text file with text "#%SystemRoot%\system32\ping.exe -t www.google.com" and save it with extension ".bat".
Just click and run it and you will get the result.
So basically what happens is that we run ping.exe application with parameters '-t' and 'www.google.com' (web-address).
The answer to your question is this
Ping -n 1 0.0.0.0
But if you want it to be faster than this, this will be your answer
Ping -n 1 -l 1 0.0.0.0
Note: Replace 0.0.0.0 with your desired IP address
Just
write the command "ping your server IP" without the double quote. save file name as filename.bat and then run the batch file as administrator

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

Resources