Articles

Solved Running commands on remote box using Plink

May 24, 2010 at 09:57:50
Specs: Windows XP

Hi,

I am using Plink to access remote server. I want to run few commands on the remote server. Though I am able to connect to the remote machine, I am not able to run any command.


The following works:
---------------------------------------------------
plink -ssh -l %user_name% -pw %user_pwd% %machine%
---------------------------------------------------


The following doesn't:
-------------------------------------------------------------------
plink -ssh -l %user_name% -pw %user_pwd% %machine% "cd e:\data"
-------------------------------------------------------------------

Can anyone please tell me how to do 'cd' on a remote box using plink? Obviously, I want to run few other commands as well after doing 'cd' operation.


Regards,
GC


See More: Running commands on remote box using Plink

Report •


✔ Best Answer
June 30, 2010 at 10:39:24

Are you using OpenSSH for Windows? That uses an out-of-date minimal install of Cygwin. And it doesn't have mv.exe. It does have mkdir.exe, and rmdir.exe, so that's why those commands work.

I suggest either using the full version of Cygwin, or use CMD to do the moves for you:

c:\windows\system32\cmd /c move "fileA" "fileB"



#1
June 29, 2010 at 22:56:23

Hi,


I have finally been able to run command on remote machine using Plink. The problem was with the syntax. It now works both by passing the command as argument, and by using the ‘–m file’ option provided with Plink.

For example,
plink -ssh -pw xxx gaurav.chhabra@server cd c://sample//gc//

plink -ssh -pw xxx gaurav.chhabra@server –m file.txt

where ‘file.txt’ contains:
cd c://sample//gc//


I am able to run executables and commands such as cd, mkdir. The only issue that I am now having is that, I am not able to run commands such as move, rename etc.

For example,
-----------------------------------------------------------------------------------
plink -ssh -pw xxx gaurav.chhabra@server move c://sample//gc//file.txt c://test//

move: not found
-----------------------------------------------------------------------------------

Any idea what the problem might be?


Thanks,
GC


Report •

#2
June 30, 2010 at 05:18:15

What's with the multiple slashes?

chnext: Any idea what the problem might be?
Check to see what shell your SSH server is running. If it's emulating a POSIX environment, the command's probably mv.


Report •

#3
June 30, 2010 at 05:44:22

Thanks for the reply Razor!

Plink understands multiple slashes only (at least that's what I found to be working in Windows).

For ex, if i write
cd c:/sample/gc/ instead of
cd c://sample//gc//
then it doesn't work.

How can i determine the shell that it's running? I tried using 'mv' but to no avail.


Report •

Related Solutions

#4
June 30, 2010 at 05:45:50

By the way, I have OpenSSH server on the remote machine.

Report •

#5
June 30, 2010 at 07:49:26

Assuming you're doing this though Cygwin, and depending on how your environment is (not) set up, you might have to specify the path of mv. Probably /bin/mv (assuming any mounts have been done), but it's been years since I've used Cygwin.

Report •

#6
June 30, 2010 at 08:48:11

We are not using Cygwin.

Everything is on Windows. The source as well as the target server where I have to execute the commands are Windows machine.


Report •

#7
June 30, 2010 at 10:39:24
✔ Best Answer

Are you using OpenSSH for Windows? That uses an out-of-date minimal install of Cygwin. And it doesn't have mv.exe. It does have mkdir.exe, and rmdir.exe, so that's why those commands work.

I suggest either using the full version of Cygwin, or use CMD to do the moves for you:

c:\windows\system32\cmd /c move "fileA" "fileB"


Report •

#8
July 1, 2010 at 04:09:18

Thanks a ton Razor! Your suggestion is working.

There is one problem that I am facing now. If there is a file kept in the root of any drive, I am able to move it but if the file(s) are present inside sub-directory(s), then it's not working. :(

For example,
-------------------------------------
Working:
plink -ssh -pw xxx gaurav.chhabra@server c://WINDOWS//system32//cmd.exe /c move /Y G:\antlr.jar G:\sql\
-------------------------------------

-------------------------------------
Not Working:
plink -ssh -pw xxx gaurav.chhabra@server c://WINDOWS//system32//cmd.exe /c move /Y G:\root\antlr.jar G:\sql\

The system cannot find the file specified.
-------------------------------------

I tried using forward slashes, backward slashes, quotes around the path, etc. but to no avail.

Can you please guide me?


Thanks again,
GC


Report •

#9
July 3, 2010 at 19:44:31

I don't know; your commands are passing though way too many parsers for me to have a clue what odd series of slashes/backslashes are required. Have you tried using single quotes around the cmd command?

Report •

#10
July 3, 2010 at 23:19:47

Thanks again Razor! It's working.

...cmd.exe /c 'move /Y G:\root\antlr.jar G:\sql\'

This is my last query: If i have spaces in the directory name, how should I specify it?

For example, if I use the following command, it doesn't work:

... cmd.exe /c 'move /Y G:\root\antlr.jar G:\AB C'


I tried to check the syntax that works on the remote machine using RDC. As expected, it works like this:

C:\>copy g:\new\antlr.jar "g:\AB C"
1 file(s) copied.

But if I try to run the same command using Plink from my local machine, it's not working.

...cmd.exe /c 'move /Y G:\root\antlr.jar "G:\AB C"'

...cmd.exe /c move /Y G:\root\antlr.jar "G:\AB C"

...cmd.exe /c 'move /Y G:\root\antlr.jar G:\AB \C'

...cmd.exe /c 'move /Y G:\root\antlr.jar G:\AB\ C'

All the above give "The syntax of the command is incorrect."


Thanks again!
GC


Report •

#11
July 4, 2010 at 07:24:58

I'm really not sure here. You're running an out-of-date, partial install of Cygwin, calling the command prompt to do a move. You could try using ECHO instead of MOVE, if just to see how the command is passed to CMD, and build your MOVE command from that.

I still suggest you install Cygwin. It's more secure, and it has mv.exe.


Report •

#12
July 5, 2010 at 22:59:48

Thanks Razor! Using ECHO is definitely a good way. It was helpful. I tried to modify my command as per ECHO's dispaly but it did not work.

Anyways, I have been able to accomplish this particular command using 'sexec' utility. Other commands that I required are working fine with Plink.

Thank You for all your time & help.


Regards,
GC


Report •

#13
July 8, 2010 at 16:54:30

I am trying to connect to root user using plink and i get access denied message. I know little about linux.

To login to our linux box, i use the below setps.
1. username: admin and password: pass
2. username: su - and password: pass
As root user, i need to run some commands.
plink is able to establish connection to admin user and run command, but it does not run the same command as root user.
Eg:
plink -ssh -l username -pw password machineName ls ==> this works fine. But i wanto run the same command as root user.

Can somebody help?


Report •

#14
July 13, 2010 at 22:21:38

Hi,


I have setup OpenSSH on remote server. I am trying to execute commands on the remote server from my local machine using Plink. Both local and remote server are Windows XP machine.

Though I am able to run commands such as ‘move’, ‘mkdir’ but ‘del’ command is not working.

For example,

Working:
plink -ssh -pw xxx user@server cmd.exe /c move g://root//home//test g://somedir
plink -ssh -pw xxx user@server cmd.exe /c mkdir H://somedir//temp

Not working
plink -ssh -pw xxx user@server cmd.exe /c del "//sample//test//gc//a.txt"
Invalid switch - "/".


These are few other syntaxes that I tried:
-------------------------------------------------------------------------------
..... cmd.exe /c del //sample//test//gc//a.txt
Invalid switch - "/".

..... cmd.exe /c del "\\sample\\test\\gc\\a.txt"
Could Not Find C:\Documents and Settings\Gaurav.Chhabra\sampletestgca.txt

..... cmd.exe /c del "/sample/test/gc/a.txt"
Parameter format not correct - "sample".

..... cmd.exe /c del "///sample/test/gc/a.txt"
Invalid switch - "/".

..... cmd.exe /c del "////sample/test/gc/a.txt"
Invalid switch - "/".

..... cmd.exe /c del "//sample//test//gc//a.txt"
Invalid switch - "/".

..... cmd.exe /c del "//sample//test//gc//a.txt" /q
Invalid switch - "/".

..... cmd.exe /c "del //sample//test//gc//a.txt /q"
Invalid switch - "/".

..... cmd.exe /c 'del //sample//test//gc//a.txt /q'
Invalid switch - "/".

..... cmd.exe /c "del //sample//test//gc//a.txt"
Invalid switch - "/".

..... cmd.exe /c "del /q //sample//test//gc//a.txt"
Invalid switch - "/".

..... cmd.exe /c 'del //sample//test//gc//a.txt'
Invalid switch - "/".

..... cmd.exe /c 'del \\sample\\test\\gc\\a.txt'
The system cannot find the path specified.

..... cmd.exe /c 'del \sample\test\gc\a.txt'
The system cannot find the path specified.

..... cmd.exe /c 'del \\\sample\\\test\\\gc\\\a.txt'
The filename, directory name, or volume label syntax is incorrect.

..... cmd.exe /c del '\sample\test\gc\a.txt'
The system cannot find the path specified.

..... cmd.exe /c del '\\sample\\test\\gc\\a.txt'
The system cannot find the path specified.

..... cmd.exe /c del '//sample//test//gc//a.txt'
Invalid switch - "/".
-------------------------------------------------------------------------------

Can anyone help me?


Regards,
GC


Report •

#15
July 15, 2010 at 07:25:52

It's working now:

... cmd.exe /c del '\\\sample\test\gc\a.txt'

Missed this one. :)


Report •


Ask Question