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.
Related
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.
I have a Windows Client machine and I would like to invoke a powershell script on that client from my local machine i.e. Mac.
I have ssh on Windows via freesshd.
Also, i need to provide password via script hence I ended up using sshpass.
From my Mac Terminal, while the command:
sshpass -v -p xxx ssh administrator#x.x.x.x "powershell.exe dir"
runs perfectly and returns the content of whatever directory the shell lands in, i am unable the get the same result from inside a shell script.
The simple script is as below:
cmd='sshpass -v -p xxx ssh administrator#x.x.x.x "powershell.exe dir"'
echo `$cmd`
What I get is:
Unable to execute command or shell on remote system: Failed to Execute
process.
I have tried various permutations of " and ' to get the correct command string but to no avail.
What is wrong here?
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"
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.
I am trying to automate sFTP process for our project. When I login with putty tool and manully type the command pbrun su - user1, the root access is given to the user1 and I am able to do the sFTP process.
I want to perform the same process via plink(command line version of putty). But when I type plink -pw password user1#server pbrun su - user1 in the command prompt, the root access is not given to the user1 and error(chown : no file or directory found)is displayed. I am wondering when I type the same command manually via putty no error is displayed but when I send the same command via plink it does not work.
Plz tell me is there any config setup need to done at the powerbroker to automate pbrun su - command. Thanks in advance.
Note : I am able to run all other unix commands except pbrun su - via plink.