plink with powershell commands not working - powershell

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!

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.

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.

Psexec is not running an EXE program remotely

Check everywhere for a reason but nothing I found matches my specific problem. I have a program in C:\somefoldername\anothersubdirectory\andanother\ of the remote pc that I try to run using PSEXEC but it does not take. PSEXEC just does not want to run anything for me. Now if I run a ping or tasklist via psexec, it works, though obviously this is in PATH.
So I am putting in:
PSEXEC \\pcname C:\somefoldername\anothersubdirectory\andanother\program.exe
and nothing happens.
I even wrote a batch script that I tried running in two different ways.
Script is just:
#ECHO OFF
C:\somefoldername\anothersubdirectory\andanother\program.exe
EXIT
and I had it copied to the remote pc's main directory (maybe I need to put it in a folder) and then I tried running C:\batch.bat using:
PSEXEC \\pcname C:\batch.bat
I also tries running:
PSEXEC \\pcname -c \\servername\batch.bat
so it copies it over to PATH.
Neither worked.
Does any program I try to run via PSEXEC have to be in the remote pc's path?
I do have to admit that I have not done running an EXE remotely, but I have written lengthier scripts using psexec that use batch file on a server without any hiccups.
What is weird to is that the program I run has parameters and I task that runs through it, so I first taskkill it remotely, then I PSEXEC the SAME EXACT EXE as:
psexec \\pcname C:\...\program.exe -a -few -parameters ODBC
and that works. When I try to open just program.exe on its own, nope doesn't take.
I also did try psexecing iexplore.exe and that didn't work either.
So gotta use the -i option. In addition to that, gotta use -p + -u or -s to load system hardware dependent gui. This is why large portions of the gui was missing, or I assume why.
Can you please try this format
psexec \\machineName -u username -p password /accepteula -h cmd /c
C://somefoldername//anothersubdirectory//andanother//program.exe >>log.txt
More Clearly
Download the PSEXEC tool zip file in your user machine and unzip it.
Open command prompt and go to the psexec path.
In my machine it is: D:\PsTools
Run following command line
Syntax:
D:\PsTools> psexec \\ip_address -u username -p Password cmd /c executablename arguments >> test.log