LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How to execute a ssh script on Linux server from Windows through a bat script? (http://www.linuxquestions.org/questions/programming-9/how-to-execute-a-ssh-script-on-linux-server-from-windows-through-a-bat-script-581171/)

wanna13e 08-31-2007 05:18 AM

How to execute a ssh script on Linux server from Windows through a bat script?
 
Hi All,

I have a situation here, I need a bat script for the windows user to do remote execution of shell script on Linux which will automate the rsync on the Linux server to the rest of the webservers. Can I know how do I go about doing that?


Thanks all in advance for ideas, suggestion to do it.

theYinYeti 08-31-2007 05:27 AM

One way to do this:
- Install Cygwin, as well as the following handy "C:\cygwin\launch.bat" script:
Code:

@echo off
C:
chdir C:\cygwin\bin
bash --login -i -c "%*"

- Run from your own .bat:
Code:

C:\cygwin\launch.bat ssh user@host "remotecommand"
Of course, you'll have to setup a passphrase-less key-based authentication to the host.

Yves.

wanna13e 09-04-2007 07:37 AM

Quote:

Originally Posted by theYinYeti (Post 2876590)
One way to do this:
- Install Cygwin, as well as the following handy "C:\cygwin\launch.bat" script:
Code:

@echo off
C:
chdir C:\_cygwin\bin
bash --login -i -c "%*"

- Run from your own .bat:
Code:

C:\cygwin\launch.bat ssh user@host "remotecommand"
Of course, you'll have to setup a passphrase-less key-based authentication to the host.

Yves.

Hi theYinYeti,

Thanks alot for your help and response. Can I know how do I do a passphrase-less key-based authentication? I'm new to this cygwin.

ghostdog74 09-04-2007 08:38 AM

another way, using Net::Telnet from Perl

theYinYeti 09-04-2007 09:01 AM

On the windows machine, open a Cygwin terminal (a command window). In it, follow these steps (copy/paste of my test):
Code:

You@PC ~
$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/You/.ssh/id_dsa): <type ENTER here>
Enter passphrase (empty for no passphrase): <type ENTER here>
Enter same passphrase again: <type ENTER here>
Your identification has been saved in /home/You/.ssh/id_dsa.
Your public key has been saved in /home/You/.ssh/id_dsa.pub.
The key fingerprint is:
e0:6a:23:1b:9b:40:bd:73:76:94:c0:e5:35:21:cb:a5 You@PC

You@PC ~
$ cat .ssh/id_dsa.pub | ssh remoteUser@remoteHost 'cat >>.ssh/authorized_keys'
remoteUser@remoteHost's password: <type remoteUser's password here + ENTER>

Along the way, it may ask if you definitely accept the remote host signature. Say yes.

Yves.

zerocool22 04-07-2008 03:16 AM

is it possible to provide a password in the bat file, so you don't need to enter the password each time?? Cause i want to make a .bat file with all users in that all login thrue ssh. So i want to add the password in the bat file for each user.

grtz

blacky_5251 04-07-2008 03:42 AM

You can also use "PuTTY", which may be easier for "Windows" users to get their heads around instead of Cygwin, and it may be something already install for access to the Linux servers.

plink Is part of PuTTY, and lets you issue commands through a SSH tunnel - just like ssh from Linux to Linux. Here's an example:-
Code:

C:\Program Files\Putty\plink.exe -ssh -2 -P 222 User@server ls -l /tmp
Just my 2c worth :twocents:;)

sangitajc 10-22-2009 03:08 AM

Cygwin ssh command in batch script
 
Hi,

I am using batch script. In that I am using cygwin ssh command to connect linux server from windows server. When i run that batch script form command prompt on windows server it gets connected to linux server without prompting for password. While when i try to run same command using scheduler it is prompting for password of linux server.

We have implemented key based authentication and saved key of windows server in linux server so it doesnt ask for password.

KIndly let me know if there is any way of providing password of linux server in batch script using Cygwin ssh command?

Thanks and Regards,
Sangita

blacky_5251 10-22-2009 03:37 AM

Download and look at Putty. One of the programs it comes with is called PuttyAgent and it can deliver keys when requested. Useful when using Windows programs to connect to Linux servers.

sangitajc 10-22-2009 04:23 AM

Hi,
I have to use cygwin only.
I cant use any other tool.
My client has server setup like that.
Plz advice.

Regards,
Sangita

chrism01 10-22-2009 09:49 PM

Sounds to me like you haven't setup pub-keys for the scheduler; that's different user. Each user needs their own key; remember, its a substitution for a passwd, so the same security rules apply.

sangitajc 10-23-2009 01:09 AM

But like i told, i have keys set up and I can connect to linux server without any password if i execute same batch script from command prompt, but if i use windows task scheduler, it prompts for linux server password.

chrism01 10-23-2009 03:14 AM

Like I said, you are userA, scheduler is UserB. Each 'userX' normally has their own separate different key, stored in a separate different dir.

Note also (separate issue) that sshd has an AllowUsers directive, so only specified users can login, regardless of key 'validity'...

sangitajc 10-23-2009 03:41 AM

I will explain you more in detail.

I ahve written one batch script which uses cygwin ssh command to connetc to one linux server.
When i run that batch script from my windows server command prompt, it gets login to linux server without asking password since we have implemented public key authentication. This means there is no sshd configuration issue for that user.
While when i schedule the task to run that batch script it prompts for linux user password.
I am using UserA for the scheduled task to run under it and I am using UserB mentioned in the batch script to connetc linux server.

Difference is same thing work on command line but in scheduled task it prompts for linux server password.
I think now scenario should be clear to you.


All times are GMT -5. The time now is 01:05 PM.