Hiding putty terminal while running in Batch script - batch-file

I am running my shell script connecting to remote server through putty, in a batch script as:
putty.exe -ssh -2 user#sever.com -pw password -m command.cmd
where, command.cmd contains
cd /path/to/the/script
./name.ksh
It is running 100% correct, as I needed.But here, the putty terminal is appearing while the batch script is running, which I donot want.
Is there any way to hide the putty terminal?

Not possible using batch, use vbscript:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "putty.exe -ssh -2 user#sever.com -pw password -m command.cmd", 0
' 0 => hide

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.

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"

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

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.

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.

vbs execute from network with credentials

I need to run a setup.exe from the local network with credentials.
I was able to do this with a bat file, but the problem is, but i don't want the cmd window showing up when this is executing.
So from what i've read, a good option is to use a vbs script like this, except i can't find a way to use it with credentials.
Set shell = WScript.CreateObject("Wscript.Shell")
shell.Run("\\192.168.0.100\Setup\setup.exe"), 0 , True
Basically what i need is the equivalent of this batch script in vbs:
net use Z: \\192.168.0.100\sh /user:user password /persistent:yes
start /d "Z:\Setup" setup.exe
you may do this:
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "Z:", "\\192.168.0.100\sh", True, user, password
Set shell = WScript.CreateObject("Wscript.Shell")
shell.CurrentDirectory = "Z:\Setup"
shell.Run("setup.exe"), 0 , True
Note: MapNetworkDrive will fail if Z: is already mapped. You will need to handle that.
PSExec is what you want:
https://technet.microsoft.com/en-us/sysinternals/pstools.aspx
Like this:
Set shell = WScript.CreateObject("Wscript.Shell")
shell.Run("psexec.exe -u User -p Password -accepteula \\192.168.0.100\Setup\setup.exe"), 0 , True
Documentation:
https://technet.microsoft.com/en-us/sysinternals/pxexec
Using PsExec
Usage: psexec [\computer[,computer2[,...] | #file]][-u user [-p
psswd][-n s][-r servicename][-h][-l][-s|-e][-x][-i [session]][-c
[-f|-v]][-w directory][-d][-][-a n,n,...] cmd [arguments]
-u Specifies optional user name for login to remote computer.
-p Specifies optional password for user name. If you omit this
you will be prompted to enter a hidden password.
-accepteula This flag suppresses the display of the license dialog.

Resources