Execute command using plink.exe - powershell

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"

Related

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!

Can't run commands on other user (su) using Plink in 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.)

powershell cannot run a command from "Run as" box

I have a one liner that I run from the windows run command :
"C:\Program Files (x86)\casper\PuTTY\putty.exe" -ssh "192.168.1.2" -l casper -pw "<password>" -m \\PROD.MSAD.casp.NET\UserData\CASPER\Home\Documents\pbauth_list.txt -t
It works great.
However I tried copying the one liner to a .ps1 file like this:
start "C:\Program Files (x86)\casper\PuTTY\putty.exe" -ssh "192.168.1.2" -l casper -pw "<password>" -m \\PROD.MSAD.casp.NET\UserData\CASPER\Home\Documents\pbauth_list.txt -t
Running it from powershell and I get his error
The system cannot find the file -ssh
Saved this as a .bat and tried to open in thorough powershell
I get a message box
Windows cannot find '-ssh'. Make sure you typed the name correctly, and then try again
I have tried all types of permutations with escaping the quotes, quoting the whole line. Can't get it to work as a saved command but it works great from the run as box. The same command worked fine from the run dialog box.
Instead of start, you should be using & "C:\Program Files (x86)\casper\PuTTY\putty.exe" <rest of args>
I would run the command like this.
Start-Process "C:\Program Files (x86)\casper\PuTTY\putty.exe" -Argumentlist "-ssh", "192.168.1.2", "-l", "casper", "-pw", "<password>", "-m", "\\PROD.MSAD.casp.NET\UserData\CASPER\Home\Documents\pbauth_list.txt", "-t"
This to make sure that arguments are sent to the putty command and not to the start command which is your issue.

How to execute Powerpoint on a remote computer using psexec

I have a remote computer I'd like to launch a powerpoint presentation remotely using psexec. I would like the powerpoint presentation to launch on the REMOTE computer.
I'm trying to execute the following command and it says "invalid file name" I have verified the file exists
psexec -u domain\user -p password \remotepc -h C:\Test\Test.pptx
Try:
psexec \\remotepc -u domain\user -p password -h -i C:\Test\Test.pptx
Notice you have 2! slashes before the remotepc name.
And I added the -i option.