Execute script located on a remote machine


 
Thread Tools Search this Thread
Special Forums IP Networking Execute script located on a remote machine
# 1  
Old 07-05-2012
Question 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  
Old 07-05-2012
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  
Old 07-05-2012
Error

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  
Old 07-05-2012
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.
This User Gave Thanks to spynappels For This Post:
# 5  
Old 07-05-2012
Error

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
....
....
Password:
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  
Old 07-05-2012
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  
Old 07-05-2012
Error

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 Smilie

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.
Password:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can I execute local script on remote machine and include arguments?

I have a script in local server cd /home/dell/work/BOP/testdir ./processchk po (here processchk is a script & po is passed as an argument) Now I want to execute this script from remote server ssh $username@$hostname "cd /home/dell/work/BOP/testdir; ./processchk po" But Its getting error... (9 Replies)
Discussion started by: manohar2013
9 Replies

2. Shell Programming and Scripting

Execute shell script on remote machine

I want to execute a shell script(set of commands) on remote machine and that script takes input from text file(local machine). Please refer below: ssh user@hostname 'bash -s'< ./test.sh file.txt But i got the error file.txt doesn't exist. Can anyone help me on this. Content of test.sh: ... (2 Replies)
Discussion started by: manishtri88
2 Replies

3. UNIX for Dummies Questions & Answers

Execute shell script in remote machine

Hi All, We have 2 servers A and B. B is having a sctipt called b.sh in path /home/dev/scripts. Now my requirement is i want to execute b.sh from server A. Kindly help me. (3 Replies)
Discussion started by: Girish19
3 Replies

4. Shell Programming and Scripting

How to execute the same script on another machine

Recently, I need to generate a lot of files from a few master files. In fact, if I relogin to the server machine, my operating would be much faster. My question is this: Can I write a script in which the current path can be automatically saved and relogin to the server to execute my command in the... (0 Replies)
Discussion started by: jiao
0 Replies

5. Shell Programming and Scripting

To execute the same script in another machine

We have a few machines which share the same directory. How can I execute the same script under that directory in different machine while I am using mine? (1 Reply)
Discussion started by: jiao
1 Replies

6. Shell Programming and Scripting

unix shell script which inserts some records into a file located in remote servers...

All, I need to write an unix shell script which inserts some records into a file located in remote servers. * Get the input from the user and insert according the first row. It should be in ascending order. 123451,XA,ABA 123452,XB,ABB 123453,XC,ABC 123455,XE,ABE 123456,XF,ABF 123458,XG,ABG... (2 Replies)
Discussion started by: techychap
2 Replies

7. Shell Programming and Scripting

Change user on remote machine and execute script!

Hi, I need to login into remote server and execute a shell script over there. As of now i am making use of ssh command ssh primUser@135.254.242.2 sh /poll.sh I am logging in as primUser but unless i change the user to root the script execution on the remote machine is not possible. ... (5 Replies)
Discussion started by: goutham4u
5 Replies

8. Shell Programming and Scripting

How to execute a script hosted on a machine from a different machine

Hi everyone I intend to trigger a script from one machine say mc1 that actually excutes on different machine say mc2 and redirect the logs to that machine mc2. I tried to use nohup <nfs location of machine >/script.sh > <nfs location of machine >/script.log 2>&1 & nfs location of... (2 Replies)
Discussion started by: harneetmakol
2 Replies

9. Shell Programming and Scripting

Execute command from terminal on remote machine

Hi All, I want to execute some commands on unix machine from the mac machne. I have two options for doing so, I am confused which is the best way of doing. Here are two options. Requirement: Execute command on the remote server machine. Commands to be executed itself contain arguments to be... (0 Replies)
Discussion started by: Ranu
0 Replies

10. Shell Programming and Scripting

how to execute a script on remote machine

hi unix guru's i am new to unix shell programming. i found a trouble in executing a script(bali.ksh) which is available on serverA with username xyza, this script contains sqlplus command to retrive the data from the database available on other serverC. Now i need to run the above script... (4 Replies)
Discussion started by: balireddy_77
4 Replies
Login or Register to Ask a Question