Can't run commands on other user (su) using Plink in PowerShell - powershell

I'm trying to use Plink to connect to a remote UNIX server, su to another user (without password) and finally execute some commands.
$commands = #(
"su - $UNIXUSER;",
"id"
)
echo y | plink -ssh $SERVER -l $USER -pw $PWD $commands
When I execute the code above using PowerShell I get a message saying he was able to change the user, but when I execute the command id he returns the id I logged in in the first place, not the su user.
How can I execute commands using Plink within a su user?

This cannot work.
I'm surprised that you even get the id executed.
The PowerShell effectively executes this.
plink -ssh $SERVER -l $USER -pw $PWD "su - $UNIXUSER;" id
First that's a wrong syntax.
An even it were correct, you can provide only a single command string on plink command line. While you can combine multiple shell commands using ; or & to a simple command string, that cannot work with su. The second command, the ls is not command of the main shell anymore. That's an inner command of the su, i.e. a complete different stream/input/whatever you call it.
What you need to do is to emulate user typing the commands, so that the first command gets processed by the main shell and the second command by the su executed from the main shell. You can do that via an input redirection only.
You can do:
"su - $UNIXUSER`nid`nexit`nexit`n" | plink -ssh $SERVER -l $USER -pw $PWD -T
The -T was added to disable pty allocation (i.e. to get a non-interactive shell), to get the behavior of -m (which implies the -T).
(I do not do PowerShell. There's probably a more elegant way to present the commands than using the string.)

Related

Execute command using plink.exe

I am trying to execute some command using plink.exe from powershell
below is the code which is working fine
plink.exe root#1.1.6.2 -pw $password -m "C:\command.txt" -no-antispoof
in command.txt, I have mentioned command as df -h
but when I tried to execute the command directly, it is not not returning any value.
plink.exe root#1.1.6.2 -pw $password "df -h" -no-antispoof
Please let me know what I am missing here. I have to execute multiple commands in various parts of my code. creating text file each command is getting difficult.
You cannot put plink switches after the command. This should work:
plink.exe root#1.1.6.2 -pw $password -no-antispoof "df -h"

How to parametrize a variable value in shell script?

I am a newbie to writing shell scripts. Please help me in parameterizing a variable value in my shell script.
I am taking command-line arguments for database name, server, user, and password in the following way:
database_name=$1
server=$2
user=$3
password=$4
I want to understand how I can pass these values to a variable called sqlcmd. I pass these values in the following way and then echo to see the value of sqlcmd variable:
sqlcmd=sqlcmd -S $server -U $user -P $password
echo $sqlcmd
after making the shell script executable using chmod a+x on ubuntu. I run the script and get the following error
line 37: -S: command not found. Line 37 in my shell script is a line on which sqlcmd variable is initialized
P.S I am using WSL on a remote windows machine. I am not sure if that should cause an error.
You need quotes:
sqlcmd="sqlcmd -S $server -U $user -P $password"
Note that you may run into difficulties later trying to execute the contents of sqlcmd.

plink with powershell commands not working

I having a bunch of Linux Servers on which i need to run few commands to get the host file entry of backup servers. I am using planning to make power shell script using plink to achieve this . It logs me in the server but the commands does not execute. Below is Powershell console
$switch = "172.20.19.50"
$commands = "c:\scripts\cmd.txt"
$username = root
$pw = Read-Host -Prompt "Enter password" -AsSecureString
plink -ssh $switch -l $username -pw $pw -m $commands
output
bash: Support: command not found
bash: Maintenance: command not found
my c:\scripts\cmd.txt contains following two commands :
Support
Maintenance
is there anything in command i am missing here ? Also if anyone could suggest a better way to get the output from linux servers remotely will be great help . I just need to query few commands to get the output
In 99.999% of all cases, if a computer tells you it cannot find something, the reason is, that that something is, in fact, not there. So, are you 100% sure that those commands exist on your server?
In the remaining cases, the reason is, that the thing is there, but not in the right place. Are you 100% sure that those commands are in root's $PATH?
I made a script wich does the same (connect to UNIX machine and execute command using Powershell) and I had to make this:
echo y | & $Plinkpath -P 22 -v $User#$server -pw $passw "$commands $($target)"
Where $commands are:
/usr/local/bin/sudo /usr/sbin/userdel
Because sudo and userdel was not always in mi $PATH (as said Jörg W Mittag) i've to use the full path to the command.
Hope this works for you!

Why does PowerShell ISE asks to confirm when an executable outputs to stderr?

I am debugging a powershell script in ISE and there I call the sysinternals pskill utility.
For some reason, PowerShell opens the following dialog:
Why? How can I configure it not to show the dialog, but just run the script as I intend?
EDIT 1
The problem is reproduced very easily:
Run ise -noprofile
Inside ise command prompt run pskill -h
Here is the result:
Although, there is no dialog, but there is error output, which I suppose is the cause for the dialog in my other scenario. So, the question is what is the cause for the error output?
It seems that pskill sends part of its help screen to the stderr, indeed, observe:
C:\Dayforce\utils> .\pskill.exe -h
PsKill v1.15 - Terminates processes on local or remote systems
Copyright (C) 1999-2012 Mark Russinovich
Sysinternals - www.sysinternals.com
Usage: pskill [-t] [\\computer [-u username [-p password]]] <process ID | name>
-t Kill the process and its descendants.
-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.
C:\Dayforce\utils> .\pskill.exe -h 2> $null
Usage: pskill [-t] [\\computer [-u username [-p password]]] <process ID | name>
-t Kill the process and its descendants.
-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.
C:\Dayforce\utils>
So, it seems that I only had the problem with pskill, because it outputs to stderr and ISE considers it a good reason to open a dialog. I fix it with the -nobanner command line option.
But how to fix it in general? I do not want ISE to open any dialog if a command outputs stderr, which in itself may not be a good taste, but does not mean there is an error.
In order to suppress the confirm box when you are using Write-Debug insert this at the top of your script / function :
If ($PSBoundParameters['Debug']) {
$DebugPreference = 'Continue'
}

Does psexec support input redirection?

I am trying to control a remote Python script via psexec, which reads commands from stdin, but I need to redirect psexec's input since psexec itself will be launched from another program. However, I have no luck making psexec accept redirected input. Is it supposed to work at all?
An example of what I'm trying to do, where input is a file containing input to the remote script:
psexec \\mymachine python c:\script.py < input
Here's one way I was able to kinda accomplish what you're after:
PsExec.exe -d \\\\192.168.1.1 cmd /k "echo list volume | diskpart"
This would pass the commands "list volume" to the diskpart command. Additionally you can also try using cmd like this for you example:
PsExec.exe -d \\\\192.168.1.1 cmd /k "python c:\script.py < input"