Issue in executing command containing single quote using Plink - matlab

I am using plink.exe from cmd in Windows 10, to ssh to Ubuntu 16.04. In there, I am running MATLAB, to run the following command:
try, someFunction('path/to/files', 'algorithm'), exit(); catch ME, warning(ME.message), exit(); end
For this, I generate the following command to handle ssh, executing matlab and run the above command:
C:\plink.exe user#server -pw ****** "matlab -nodesktop -nosplash -noawt -r 'try, someFunction('path/to/files', 'algorithm'), exit(); catch ME, warning(ME.message), exit(); end'
Running the above command, I get the following error in MATLAB:
Warning: Undefined function or variable 'path/to/files'.
As it turns out, in matlab, the command is constructed like following:
someFunction(path/to/files, algorithm)
which is without "single quotes": thank you, plink :( .
Can you please help to generate the correct command? or if there is already a question asked with similar problem, I would be thankful to direct me to it.
Thanks,

It's not Plink fault. It's how Windows command-line interpreter works.
Adding cmd and batch-file tags, so that you may get answers from experts on the field.
Anyway, I can see two solutions:
Put your command to a file like:
matlab -nodesktop -nosplash -noawt -r "try, someFunction('path/to/files', 'algorithm'), exit(); catch ME, warning(ME.message), exit(); end"
And use the file (command.txt) with Plink like:
C:\plink.exe user#server -pw ****** -m command.txt
If you do not want to use a separate file for the command, this should work too:
echo matlab -nodesktop -nosplash -noawt -r "try, someFunction('path/to/files', 'algorithm'), exit(); catch ME, warning(ME.message), exit(); end" | C:\plink.exe user#server -pw ****** -T
(Note the -T switch).

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.

Sysinternal Close Handle working in Command Prompt But Not Powershell

I have a strange issue. I am trying to close down a handle using Powershell using this 1 liner:
(&"D:\handle.exe" -p "–c C –p 3348 -y")
I am getting the following response:
No matching handles found.
When I run the exact same command in Command Prompt
handle.exe -c C -p 3348 -y
I get:
Handle closed.
I am running Powershell and Command Prompt as Admin.
edit: Note: I can run the same command inside the Powershell Command Window and get the same expected result as I did from the normal Windows Command Prompt.
You don't need any fancy syntax. PowerShell can run command-line programs just like cmd.exe can. Just type the command you want and press Enter.
handle -c C -p 3348 -y
It is likely you need to run this from an elevated PowerShell window, but that's not different from cmd.exe.

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.

call matlab in an ssh command from a perl script

Hi i have set up passwordless ssh set up and have perl call ssh -o "BatchMode yes" user#host "runMe.pl arg1 arg2"
runMe.pl calls matlab and the function run_online with the given args.
nohup matlab -nojvm -nodisplay -r "run_online('$imgfolder/$folder/', '$ARGV[0]$folder', '/homes/rbise/results/mitosis/$ARGV[0]/$folder/')" > out.txt < /dev/null &
for some reason matlab never starts running. why is this?
thanks
This is substantially a duplicate of the [perl] question that was asked immediately previously to this one -- at least, the answer is the same. You have no controlling terminal when you connect with ssh. Try ssh -o "BatchMode yes" user#host "bash -c runMe.pl arg1 arg2".