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.

Hi,

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

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 ?

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.

1 Like

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.

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

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.

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:

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

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

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 :frowning:

You command:

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

yields the below output:

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:

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

Can you run the commands interactively through Plink ok?
So can you:

plink user@remotehost

and then run the commands individually?

I'm also not sure where the

sh: /export/home/ms104u/test.sh: No such file or directory

comes from unless you're using symlinks.

I have some clue if it help other resolve my error.

plink -v -ssh user1@remotehost pwd
Password:
Access granted
Opened channel for session
Started a shell/command
/home/CSDEV13-PT/user1
Server sent command exit status 0
Disconnected: All channels closed

However when i login to remotehost using putty and fire this command

user1@csdev13: /export/home/user1 $ cd /home/CSDEV13-PT/user1
ksh: /home/CSDEV13-PT/ms104u:  not found
user1@csdev13: /export/home/user1 $ hostname
csdev13

Kindly help !!

What version of *nix are you trying to connect to?

Can you use some folder outside of home, like /var/tmp/ to see if that works? It looks like it might have something to do with the way your home directory is being mounted or at least referenced.

1 Like

I think I have moved a step further.

The remote ip that i was giving to plink is NOT the actual remote ip that has the x.sh script.

The reason it asked for password twice using putty is once for the remotehostA and then it somehow automatically switches to some other RemoteServerB which then asks for the password again.

Can you help me determine where and how is the automatic hopping from remoteserverA to remoteserverB happening ? where and what to look inorder to reveal the mystery.

Because I will eventually need to incorporate this hopping mechanism in my plink windows script inorder to automate everything.

I'm afraid I can't help you with that, I simply don't know. I suspect it may have something to do with how NFS shares are mounted, but without more details of your exact network layout and OSs etc, I don't think anyone would be able to help much further.

Do the commands I suggested work if run using a fixed folder like /var/tmp/ on the first remote server?

Yes they do run.

However, my job's pending unless I get this hopping of unix server sorted out.

Any clues by any one to begin with.

Shall I open a new Thread for this or should I expect response on this thread.

Ok, first things first.

Can you describe in as much detail as you can what the network topology of your systems is?

Also, can you confirm which flavour of Unix you are running on each server?

Lastly, can you check if home directories are mounted via nfs or use some other distributed file system?

I'll take this as far as I can and hopefully get enough info to point to the right place to solve this.

We have a setup whereby we cannot login to ServerB directly by using putty.
We have to first login to ServerA by providing username and password and it somehow takes us to serverB and prompts for the password for my login id for ServerB. Providing the password again logs me onto ServerB where I have a script test.sh which I need to execute.
I wanted to automate the execution of script test.sh on ServerB from my local windows machine hence I wrote the below command.

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

This command logs me onto ServerA but does not take me to ServerB hence test.sh script is not found.
I cannot logon to serverB due to this hopping concept in place.
Kindly help me with some work around.
Note: ServerA and ServerB both are unix servers

This was basically done inorder to disable VPN connectivity from remote locations.

Linux ServerA 2.6.18-164.11.1.el5PAE #1 SMP Wed Jan 6 13:43:57 EST 2010 i686 i6
86 i386 GNU/Linux

and

SunOS ServerB 5.10 Generic_147440-13 sun4u sparc SUNW,Ultra-80

These are basically output of uname -a

How can I find this ? any command ?

Don't worry, I think that's enough info for now. Let me have a think about this.

Just two clarifications required...

Do you need to scp the file to ServerB every time or is it only once and it will live on ServerB forever after that?
Can you copy keys from one server to another to set up key-based (passwordless) logins?

I think the answer will involve essentially running a command or script on ServerA to run a command on ServerB.

The file on serverB has to be executable, when you scp the script file to serverB, even if it started out with the executable permission bits set, they will likely be gone on the remote box. Depends on your umask setting on the remote box.

The simple way to handle all this. Use sftp to move the file:

sftp snappels@serverB
Password> farkle
put myscript.sh
chmod 775 myscript.sh
exit

Do not use scp. Now the file is over on serverB and anytime you run it via ssh from serverA it will work for you.

The problem is that there is no diect access to ServerB as per the OPs previous post, if this was not the case my first solution would have worked fine.

There is 0 work on ServerA. I dont need the scripts or any file on serverA. Its just a HOP server that we are given access to. Login to this server helps auto jump to the actual client machine which is ServerB.

There is no network connection between our windows workstation and serverB. Network Timesout if we try to connect to.

I did not get the quoted workaround?

What would I need to run at windows inorder to get the script running on serverB by hopping to it via serverA.

I have a simple straight forward question.

If login to ServerA by putty helps auto jump to serverB with the need of the password to be entered the second time why doesn't or why couldn't plink or any alternate tool do the same ?

When you connect using PuTTY, are you just putting the details in and connecting, or are you using some saved session?

It could be that the saved session has the hop saved in as a command to execute on login to serverA. If it is a saved session, load it but don't initiate the connection, and go to the SSH Tab/Category. There is an entry there where you can specify a remote command such as that required to ssh from ServerA to ServerB, so you automatically get forwarded from ServerA to ServerB when you login to ServerA using the saved PuTTY profile.

I think you can direct Plink to use the same profiles, but I'm not 100% certain of that. You could look at the PuTTY documentation to see the details. PuTTY User Manual

If this is the case, we just need to figure out how to get the script from the Windows box to ServerB, this could probably be done using 2 separate scp commands within a single script.

---------- Post updated at 09:20 AM ---------- Previous update was at 08:50 AM ----------

I just tested this and if you have a saved session in PuTTY, you can use the same session in plink

plink -load <<session-name>>

If the session name has a space in it, you can put it into double quotes.

I also checked if a saved session has a "ssh user@anotherhost" command saved in the SSH category, it will lok like it directly logs into anotherhost without an internediate shell, and you should just be asked for the password for 'server' first, and then for 'anotherserver'. The good news is this works exactly the same if this session is then used for a plink connection.