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.
Related
I have a shell script which I need to execute from server A which executes commands in Server B as well. But I can execute those commands only being a root user in server B.
Manually if I login to server B then I have to change the user to root and execute delete commands. To automate this I am trying to write a script and execute from server A, but it asks me for password. How do I add my password in the script? (Though it is not recommended), or please suggest if any other way to tackle this.
Add your username in /etc/sudoers with nopasswd to remove password prompt
$ visudo
user ALL=(ALL) NOPASSWD: ALL
You can use sshpass command with -p option
sshpass -p 'your_password' ssh root#your_host ls
refer manual of sshpass for more options
I have the following task for my golang code:
The command has to be run as root user on the server remotely in bash and the command output has to be fetched in a variable.
Logging over ssh as root is disabled.
sudo on the server is disabled, so I have to use 'su -' and type password
since I want to make it as automated as possible in bash, the password has to be stored inside the command
Here are the workflow actions:
Login via SSH (as unprivileged user) to remote host
Elevate to privileged 'root' user --> su -
Type the root password
run the command which root can execute
get to output to string on localhost and do some actions
I have Googled for days, but it seems that I cannot find a solution for this.
Does anyone have a solution to this?
The issue you are facing is concerning interacting with the command after it has been executing.
It is quite easy to use exec.Command for non-interactive commands.
I would recommend using Expect for interaction, or the Golang equivalent located here.
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 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'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.