Psexec , cmd and batch file - scripting

I have a batch file named a.bat on a winserver2008 Desktop.
That batch file only write the SessionID (from environment variable) to a local eventlog.
I want to execute it remotely using cmd (otherwise the SessionName doesn't appear).
so I have tried
c:\PsTools\psexec.exe \\<Server> -u test2 -p <Password> -i 2 cmd "c:\Users\test-2\Desktop\a"
or
c:\PsTools\psexec.exe \\<server> -u test2 -p <Password> -i 2 "cmd \"c:\Users\test-2\Desktop\a\"";exit
all of these just open a terminal on the remote machine but don't execute the batch.
Any ides?
Best Regards,

Use a /c on the command line after cmd.
So, your first line would look like:
c:\PsTools\psexec.exe \\<Server> -u test2 -p <Password> -i 2 cmd /c "c:\Users\test-2\Desktop\a"

psexec \\<server> -s cmd.exe & whatever.bat
If you're trying to run a batch remotely then when cmd is open on the remote through your terminal connection, you have two options:
xcopy \\your_computer\filepath c:\wherever something.bat
run \\computername\c$\wherever_it_is_located

Related

Using Plink to run a command

I am trying to create a script to login to a remote server and run a command. The script is as follows
plink.exe UserName#ServerIP -pw PASSWORD -batch -m PATHTOFILEWITHSCRIPT.txt
PATHTOFILEWITHSCRIPT.txt has this line
debug software restart process user-id
The script when run logs into the server but does not run the command.
Any ideas?
The format that is required is
echo COMMAND | plink.exe USER#SERVER -pwd PASSWORD -batch
where COMMAND is the command that you want to run after you log in

Use SSH commands in putty and/or psftp script for sftp server [duplicate]

I am looking to script something in batch which will need to run remote ssh commands on Linux. I would want the output returned so I can either display it on the screen or log it.
I tried putty.exe -ssh user#host -pw password -m command_run but it doesn't return anything on my screen.
Anyone done this before?
The -m switch of PuTTY takes a path to a script file as an argument, not a command.
Reference: https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-m
So you have to save your command (command_run) to a plain text file (e.g. c:\path\command.txt) and pass that to PuTTY:
putty.exe -ssh user#host -pw password -m c:\path\command.txt
Though note that you should use Plink (a command-line connection tool from PuTTY suite). It's a console application, so you can redirect its output to a file (what you cannot do with PuTTY).
A command-line syntax is identical, an output redirection added:
plink.exe -ssh user#host -pw password -m c:\path\command.txt > output.txt
See Using the command-line connection tool Plink.
And with Plink, you can actually provide the command directly on its command-line:
plink.exe -ssh user#host -pw password command > output.txt
Similar questions:
Automating running command on Linux from Windows using PuTTY
Executing command in Plink from a batch file
You can also use Bash on Ubuntu on Windows directly. E.g.,
bash -c "ssh -t user#computer 'cd /; sudo my-command'"
Per Martin Prikryl's comment below:
The -t enables terminal emulation. Whether you need the terminal emulation for sudo depends on configuration (and by default you do no need it, while many distributions override the default). On the contrary, many other commands need terminal emulation.
As an alternative option you could install OpenSSH http://www.mls-software.com/opensshd.html and then simply ssh user#host -pw password -m command_run
Edit: After a response from user2687375 when installing, select client only. Once this is done you should be able to initiate SSH from command.
Then you can create an ssh batch script such as
ECHO OFF
CLS
:MENU
ECHO.
ECHO ........................
ECHO SSH servers
ECHO ........................
ECHO.
ECHO 1 - Web Server 1
ECHO 2 - Web Server 2
ECHO E - EXIT
ECHO.
SET /P M=Type 1 - 2 then press ENTER:
IF %M%==1 GOTO WEB1
IF %M%==2 GOTO WEB2
IF %M%==E GOTO EOF
REM ------------------------------
REM SSH Server details
REM ------------------------------
:WEB1
CLS
call ssh user#xxx.xxx.xxx.xxx
cmd /k
:WEB2
CLS
call ssh user#xxx.xxx.xxx.xxx
cmd /k

Run .sql file from batch file

I'm new to batch files and would like to execute sql script from the batch file. Please can someone assist.Thanks in advance.
You can use sqlcmd.exe from the DOS prompt. If you run sqlcmd -? from the DOS prompt the program will tell you the command line parameters. Here's an example
sqlcmd -S MyDbServer -d DatabaseName -E -i "MyScript.sql"
The -E tells sqlcmd to use "trusted authentication" meaning your Windows login. If you're not using Windows Authentication then you'll want to use the -U and -P parameters.

Execute Command putty ssh

I connect with my Windows cmd to another machine with this command:
putty.exe -ssh bob#172.16.17.50 -i C:\Users\Administrator\Desktop\bobPrivate.ppk -pw mypass
And which option I have to add to run a single command and disconnect after that?
To execute a command you have to use the -m option.
putty.exe -ssh bob#172.16.17.50 -i C:\Users\Administrator\Desktop\bobPrivate.ppk -pw mypass -m C:\Path\to\file\with\commands
You have to replace the path with yours

tail -f using PuTTY hangs

I am using PuTTY command line to connect to a server and tail a log file. On local machine I've created a file "tail-exec" which contains following text:
tail -f /var/log/test.log
I am starting putty through command line as:
putty -ssh -t -pw -m tail-exec user#server
This opens up the terminal window with log tail. But the problem is that this terminal hangs after there are few hundred lines added to the log.
If I open putty manually, and then run the tail command from the bash prompt, then it is not hanging for thousands of lines also.
I've tried using following text in tail-exec file, but same issue is happening:
bash -i tail -f /var/log/test.log
Any idea what could be the issue?
Try to use a saved session where you set the Option "Keepalive". The use the session like this:
putty -ssh -t -pw -m tail-exec -load 'session-name'