Plink working directory - windows

MY SERVER: BitVise SSH server for windows
My Client: plink.exe
I cannot for the life of me figure out how to change a directory when using plink.exe and execute a script in that directory.
I am doing something like this to try and send a command to switch a directory and execute a script:
C:\plink.exe -ssh 10.10.10.10 -P 22 -l user -pw password cd C:\sample && install.bat
However, my command fails each time I run this, stating that install.bat does not exist. If I use putty, connect with the GUI, and run the cd C:\sample && install.bat command, everything works as expected.
Is it possible to tell plink what directory to connect to?

Since & is a command separator in cmd, did you put your command in quotes? I would bet that it is trying to run cd C:\sample on the server and install.bat locally.
C:\plink.exe -ssh 10.10.10.10 -P 22 -l user -pw password "cd C:\sample && install.bat"

Related

Connect to ssh using git bash on a batch file

I'm trying to create a .bat file in windows that will open a git bash terminal and run a command to connect using ssh.
For example:
start "" "%PROGRAMFILES%\Git\bin\bash.exe" -ssh -i "C:\app.pem" centos#172.**.*.**

ssh batch script windows : unable to send my commands

I wrote a little script, to automate some actions on a server
#echo off
SET /P host= enter host adress :
#echo off
plink.exe -ssh user#%host% -P 22 -pw password -batch -m "C:\path\to\my_commands.txt"
pause
I can connect with this script, no problem with that. But when I run it, it's not sending what I wrote in my_commands.txt
I've tried to wrote my commands like this :
plink.exe -ssh user#%host% -P 22 -pw password -batch mkdir /my/path
I've tried with or without quotes for the path to my_commands.txt
In both ways, cmd keep telling "bash : mkdir : command not found"
It look like cmd is trying to take the "my_commands.txt" as parameter.

Using Plink, how to auto-input password into Ubuntu ssh?

I'm currently setting up a batch file to ssh from a Windows machine into a Ubuntu machine and issue a series of commands. I'm using plink, and I'm using the -m argument to pass a .txt file with a list of commands.
The batch file code that runs through cmd:
set PATH=c:\path\to\plink.exe
plink.exe -ssh -t user#ipaddress -pw <psw> -m c:\path\to\textFile\commands.txt
The commands.txt code:
sudo -s #access the root login
<root psw> #enter the password for the root login
command-1 #issue a command in linux as root
command-2 #issue a command in linux as root
command-3 #issue a command in linux as root
The issue I'm running into is that when I run this batch file, the output within command prompt still prompts the user to manually enter the password. Is there a means to input the password form the next line of the commands.txt file? Or does this process require something else?
As even your question says, the file commands.txt specified by -m switch should contain commands. A password is not a command.
Moreover, the commands in the file are executed one-by-one. The sudo (had it worked) would execute an interactive shell session and wait for a user input (commands). Only once the sudo exits, the following commands (command-1, etc) are executed.
Automating password input for sudo is generally a bad idea. If you need to run some commands that require root privileges, a better solution is to associate a dedicated private key with the commands in sudoers file. And then use sudo and the private key in Plink.
Anyway, to automate an input (of a password in this case) to a command, you need to use an input redirection. The same is true for commands to be executed within (not after) sudo.
Like:
(
echo passwod
echo command-1
echo command-2
) | plink.exe -ssh -t user#ipaddress -pw <psw> sudo -s
As now there's only one real top-level command - sudo, I'm specifying it directly on Plink command-line, instead of using -m switch. Functionally, it's identical.

Automatic login using PUTTY.EXE with Sudo command

I am using below command to open putty through windows command prompt:
PUTTY.EXE -ssh -pw "mypass" user#IP -m C:/my.sh -t
Where my.sh mentioned in above command file contains:
sudo su - rootuser
After executing the command, putty console is opened and it prompts for password.
Is there any way where I can provide this password automatically without typing it?
There's a bit of a horrible workaround using Expect and embedding a password.
This is a bad idea.
As an alternative:
Configure sudo to allow NOPASSWD.
Login directly as root using public-private key auth.
Both these introduce a degree of vulnerability, so should be used with caution - but any passwordless auth has this flaw.
Finally, after struggling for almost whole day, I got the way to get this working.
Below command can be executed from windows machine:
PLINK.EXE -t -ssh -pw "password" user#IP /home/mydir/master.sh
master.sh file is located on remote machine. And this file contains below command to execute script with sudo command without prompting password.
echo password | sudo u user -S script.sh
Here, password should be replaced with your password. user should be replaced with your actual user and script.sh is the script on remote machine that you want to fire after sudo login.

Getting error with: pbsu & pbrun commands via Plink (command not found)

I'm doing automation for run commands via Plink. Below is required step to run command in my environment:
Login to unix machine:
host: myhost
user/pass: myuser/Mypass
Use command: pbsu - uatwrk1
(Or command: pbrun –u uatwrk1 pbksh)
->it will redirect to other machine and some commands to be executed there
Run some commands
******Issue*******
(I) When I execute the Plink command line below in CMD, I'll get error:
The content of script.txt:
pwd
pbsu - uatwrk1
pwd
runcommand.ksh
plink -ssh myuser#myhost -pw Mypass -m "C:\script.txt" > "C:\log.txt"
Error: "sh: pbrun: command not found"
(II) But it works with command by command:
cmd>plink -ssh myuser#myhost -pw Mypass
$pwd
$pbsu - uatwrk1
$pwd
$runcommand.ksh
Please help to to figure out why the command pbrun failed on (I) while it worked on (II) ?
I need (I) works as it provides the capture log to check further in my automated application (using VBA).
Thanks a ton for any help!
It is possible if in the interactive session (II) you have another environment than in the batch session (I). You must check what PATH and current directory you have in the both cases.

Resources