The UNIX and Linux Forums Execute script located on a remote machine
 Forums Search Forums Register Forum Rules Man Pages Albums FAQ Members Calendar Search Today's Posts Mark Forums Read

 IP Networking Learn TCP/IP, Internet Protocol, Routing, Routers, Network protocols in this UNIX and Linux forum.

#1
07-05-2012
 Registered User Join Date: Jun 2011 Posts: 217 Thanks: 43 Thanked 0 Times in 0 Posts
Execute script located on a remote machine

So, is there way of automating this ?

My ultimate goal is to run some cmd script in windows and it should connect to a remote unix host and run a script x.sh located on the remote unix host.

I was wanting to achieve this by using WinSCP and Putty only. If possible let me know how and if not then what is the minimum number of tools I can use to achieve this and how.
#2
07-05-2012
 Registered User Join Date: Dec 2004 Location: Isle-of-Skye Posts: 231 Thanks: 7 Thanked 26 Times in 26 Posts
Hi,

You could do this a number of ways, rsh, ssh are just a couple pf examples.

Code:
davem@darkstar--> rsh polaris ls -l
[davem@polaris shm]# ls -l
total 281392
-rw-rw-rw-. 1 root  root    5668387 2006-12-29 20:04 ACFTREF
-rw-rw-rw-. 1 root  root      85970 2001-08-08 13:47 ardata.pdf
-rw-rw-rw-. 1 root  root      44619 2006-11-17 09:58 ARData.pdf
-rw-rw-rw-. 1 root  root    6937444 2006-12-29 20:05 DEALER
-rw-rw-rw-. 1 root  root   33477137 2006-12-29 20:11 DEREG
-rw-r--r--. 1 root  root    9939544 2012-07-04 21:01 dereg_wrk01
-rw-r--r--. 1 root  root    9619084 2012-07-04 21:01 dereg_wrk02
-rw-r--r--. 1 root  root     240392 2012-07-04 21:01 dereg_wrk03
-rw-r--r--. 1 root  root     240392 2012-07-04 21:01 dereg_wrk04
-rw-r--r--. 1 root  root   10099868 2012-07-04 21:01 dereg_wrk05
-rw-r--r--. 1 root  root    4370327 2012-07-04 21:01 dereg_wrk06
-rw-rw-rw-. 1 root  root    5247997 2006-12-29 17:30 DOCINDEX
-rw-rw-rw-. 1 root  root     183409 2006-12-29 20:05 ENGINE
-rw-rw-rw-. 1 root  root      39799 2000-07-06 14:19 format.pdf
-rw-r--r--. 1 root  root       1946 2012-07-05 07:52 input_file_list
-rw-rw-rw-. 1 root  root  100457929 2006-12-29 20:10 MASTER
-rw-r--r--. 1 root  root   12132352 2012-07-05 08:21 mast_wrk00
-rw-r--r--. 1 root  root          0 2012-07-05 08:20 mast_wrk01
-rw-r--r--. 1 root  root   18716339 2012-07-05 08:20 mast_wrk02
-rw-r--r--. 1 root  root   43883632 2012-07-05 08:21 mast_wrk03
-rw-rw-r--. 1 davem davem  12771975 2012-07-04 16:21 new.csv
-rw-rw-r--. 1 davem davem   1093330 2012-07-04 16:52 nomatch
-rw-r--r--. 1 root  root    2101362 2012-07-05 08:21 ref_wrk00
-rw-r--r--. 1 root  root          0 2012-07-05 07:52 ref_wrk01
-rw-r--r--. 1 root  root    2099001 2012-07-05 08:20 ref_wrk02
-rw-rw-rw-. 1 root  root    7453821 2006-12-29 20:04 RESERVED
-rw-rw-r--. 1 davem davem    775151 2012-07-04 16:19 wrong.csv
[davem@polaris shm]

You just have to make sure the correct configs are set up - and that there is some reasonable degree of security in place.

Regards

Dave
#3
07-05-2012
 Registered User Join Date: Jun 2011 Posts: 217 Thanks: 43 Thanked 0 Times in 0 Posts

Quote:
 Originally Posted by gull04 Hi, You could do this a number of ways, rsh, ssh are just a couple pf examples. Code: davem@darkstar--> rsh polaris ls -l [davem@polaris shm]# ls -l total 281392 -rw-rw-rw-. 1 root root 5668387 2006-12-29 20:04 ACFTREF -rw-rw-rw-. 1 root root 85970 2001-08-08 13:47 ardata.pdf -rw-rw-rw-. 1 root root 44619 2006-11-17 09:58 ARData.pdf -rw-rw-rw-. 1 root root 6937444 2006-12-29 20:05 DEALER -rw-rw-rw-. 1 root root 33477137 2006-12-29 20:11 DEREG -rw-r--r--. 1 root root 9939544 2012-07-04 21:01 dereg_wrk01 -rw-r--r--. 1 root root 9619084 2012-07-04 21:01 dereg_wrk02 -rw-r--r--. 1 root root 240392 2012-07-04 21:01 dereg_wrk03 -rw-r--r--. 1 root root 240392 2012-07-04 21:01 dereg_wrk04 -rw-r--r--. 1 root root 10099868 2012-07-04 21:01 dereg_wrk05 -rw-r--r--. 1 root root 4370327 2012-07-04 21:01 dereg_wrk06 -rw-rw-rw-. 1 root root 5247997 2006-12-29 17:30 DOCINDEX -rw-rw-rw-. 1 root root 183409 2006-12-29 20:05 ENGINE -rw-rw-rw-. 1 root root 39799 2000-07-06 14:19 format.pdf -rw-r--r--. 1 root root 1946 2012-07-05 07:52 input_file_list -rw-rw-rw-. 1 root root 100457929 2006-12-29 20:10 MASTER -rw-r--r--. 1 root root 12132352 2012-07-05 08:21 mast_wrk00 -rw-r--r--. 1 root root 0 2012-07-05 08:20 mast_wrk01 -rw-r--r--. 1 root root 18716339 2012-07-05 08:20 mast_wrk02 -rw-r--r--. 1 root root 43883632 2012-07-05 08:21 mast_wrk03 -rw-rw-r--. 1 davem davem 12771975 2012-07-04 16:21 new.csv -rw-rw-r--. 1 davem davem 1093330 2012-07-04 16:52 nomatch -rw-r--r--. 1 root root 2101362 2012-07-05 08:21 ref_wrk00 -rw-r--r--. 1 root root 0 2012-07-05 07:52 ref_wrk01 -rw-r--r--. 1 root root 2099001 2012-07-05 08:20 ref_wrk02 -rw-rw-rw-. 1 root root 7453821 2006-12-29 20:04 RESERVED -rw-rw-r--. 1 davem davem 775151 2012-07-04 16:19 wrong.csv [davem@polaris shm] You just have to make sure the correct configs are set up - and that there is some reasonable degree of security in place. Regards Dave
What is this ?? On top of my heard. How is it related to my question ? Can you detail the software i need to install and the configuration specifications ?
#4
07-05-2012
 Registered User Join Date: Sep 2011 Posts: 83 Thanks: 2 Thanked 10 Times in 10 Posts
As you want to do this from a Windows box using PuTTY, you need to look at Plink. This is a tool called from the Windows shell (DOS Prompt) and can be used like ssh. It is especially useful for running a ssh command on a remote *nix server from a Windows PowerShell or batch script.

It should already be installed as part of the default PuTTY installation, or it can be downloaded from the PuTTY website as a standalone executable.
 The Following User Says Thank You to spynappels For This Useful Post: mohtashims (07-05-2012)
#5
07-05-2012
 Registered User Join Date: Jun 2011 Posts: 217 Thanks: 43 Thanked 0 Times in 0 Posts

Quote:
 Originally Posted by spynappels As you want to do this from a Windows box using PuTTY, you need to look at Plink. This is a tool called from the Windows shell (DOS Prompt) and can be used like ssh. It is especially useful for running a ssh command on a remote *nix server from a Windows PowerShell or batch script. It should already be installed as part of the default PuTTY installation, or it can be downloaded from the PuTTY website as a standalone executable.
Can you help me with the setup / syntax of the same ? I have Plink on my windows box. I need to upload a file to unix server X using Winscp command line and the use Plink to execute a script x.sh on the same unix server X.

So I am just looking for having to execute a single command .cmd from windows to do the above jobs for me.

---------- Post updated at 04:18 AM ---------- Previous update was at 03:53 AM ----------

I tried the below command but strangely it fails.

Code:
plink user1@remotehostip -m local_script.sh -v
....
....
Access granted
Opened channel for session
Started a shell/command
mv: cannot stat /tmp/folderwow1'Server sent command exit status 1
Disconnected: All channels closed
: No such file or directory

When i execute the same mv command from Putty after login in it succeeds.

the command in local_script.sh is

Code:
mv /tmp/folderwow1 /tmp/foldermohtashim

The only difference i saw which may not be concerning here is that loggin throught putty asked me for the password of user1 twice while plink command via windows asked for the password just once.

Kindly help.
#6
07-05-2012
 Registered User Join Date: Sep 2011 Posts: 83 Thanks: 2 Thanked 10 Times in 10 Posts
I don't have enough Windows scripting experience to give a definitive answer, but I would suggest that first of all, you use Pageant to manage keys to access the remote *nix servers. Also make sure the PuTTY directory is added to your PATH.

you then use PSCP to copy the script to the remote host:

Code:
pscp -agent \path\to\source\file user@remotehost:/path/to/target

Then you use Plink to make it executable and run the script:

Code:
plink -agent user@remotehost "chmod +x /path/to/script; sh /path/to/script"

You could probably wrap this in a Windows batch script, but I'm not sure how to make that completely robust.
#7
07-05-2012
 Registered User Join Date: Jun 2011 Posts: 217 Thanks: 43 Thanked 0 Times in 0 Posts

Quote:
 Originally Posted by spynappels I don't have enough Windows scripting experience to give a definitive answer, but I would suggest that first of all, you use Pageant to manage keys to access the remote *nix servers. Also make sure the PuTTY directory is added to your PATH. you then use PSCP to copy the script to the remote host: Code: pscp -agent \path\to\source\file user@remotehost:/path/to/target Then you use Plink to make it executable and run the script: Code: plink -agent user@remotehost "chmod +x /path/to/script; sh /path/to/script" You could probably wrap this in a Windows batch script, but I'm not sure how to make that completely robust.
I am slowly losing hope

You command:

Code:

C:\Program Files\PuTTY>plink -agent user1@remorthost "chmod +x /export/home/test.sh; sh /export/home/test.sh" -v

yields the below output:

Code:
Using keyboard-interactive authentication.
chmod: cannot access /export/home/test.sh': No such file or directory
sh: /export/home/ms104u/test.sh: No such file or directory

But the file exists:

Code:
ls -ltr /export/home/test.sh
-rwxr-xr-x   1 user1   vts           22 Jul  4 09:50 /export/home/test.sh