How to run a script using batch file? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to run a script using batch file?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-14-2011
gpk_newbie gpk_newbie is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 27 November 2013, 11:29 AM EST
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
How to run a script using batch file?

the manual process which we follow is login to remote unix box thro putty using the unix account and password and then sudo to root user.

server name:abc@server.com
login asqrst
password:******

$ sudo su - root

then run the script stored on remote server under root account.


But now the question here is how to run a that sciprt thro a batch file stored on local system. I want the batch file to perform the manual process as above.

idea im thinkin:

I have a shell script and batch file stored under a dir on a windows machine. I would like to execute that script thro that batch file.

C:\abcd\batch1.bat
C:\abcd\script.sh

i would like torun the script using the batch file. the script contains commands which are executable thro root or admin account.


Can some one throw some light on how to get this done.

Thanks in advance.
Sponsored Links
    #2  
Old 09-14-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 29 August 2014, 9:07 PM EDT
Location: Saskatchewan
Posts: 19,271
Thanks: 774
Thanked 3,237 Times in 3,035 Posts
You can't do so using PuTTY, but the same people who make PuTTY also make plink, which can be automated.


Code:
plink.exe -batch -pw password username@host sudo su - < script.sh

Sponsored Links
    #3  
Old 09-14-2011
gpk_newbie gpk_newbie is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 27 November 2013, 11:29 AM EST
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
Thanks Corona688.

Can you pls give more light on the code which u hav given. like how it works wen that code is run?
    #4  
Old 09-15-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 29 August 2014, 9:07 PM EDT
Location: Saskatchewan
Posts: 19,271
Thanks: 774
Thanked 3,237 Times in 3,035 Posts
That would be a line you put in a batch file. plink.exe would need to be in the same directory.


Code:
plink.exe -batch -pw password username@host sudo su - < script.sh

  • plink.exe: runs plink.exe
  • -batch: Never ask the user for anything. If the password doesn't work, don't ask for another, just fail.
  • -pw password: Supplies a password to login to the ssh account.
  • username@host: The username to login to.
  • sudo su -: The command to run on the server. It will log into a root shell.
  • < script.sh: Feed the local file script.sh into the remote root shell.

You might need to remove carriage returns from script.sh before it's safe to send to the server.
Sponsored Links
    #5  
Old 09-16-2011
gpk_newbie gpk_newbie is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 27 November 2013, 11:29 AM EST
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
Thanks

is this possible


Code:
 plink.exe -pw $password $username@host sudo su - < script.sh

where the username and password are entered by the user before the plink is executed thro a batch file.

batch file looks like this

read username
read password

Code:
 plink.exe -pw $password $username@host sudo su - < script.sh

Sponsored Links
    #6  
Old 09-16-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 29 August 2014, 9:07 PM EDT
Location: Saskatchewan
Posts: 19,271
Thanks: 774
Thanked 3,237 Times in 3,035 Posts
DOS CMD is not UNIX sh. $ doesn't give you a variable.

It only took MS 20 years, but they did finally add a method to read from keyboard:
Code:
@ECHO OFF

SET /p userin=Please enter your full name: 
SET /p pasw=Please enter your password:

plink.exe -pw %pasw% %username%@host sudo su - < script.sh

CMD being what it is, its treatment of spaces and quotes can be ambiguous. Spaces in passwords may not work and quoting may not be able to fix it.
The Following 2 Users Say Thank You to Corona688 For This Useful Post:
gpk_newbie (09-18-2011), ramana_pasula (05-02-2012)
Sponsored Links
    #7  
Old 09-16-2011
gpk_newbie gpk_newbie is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 27 November 2013, 11:29 AM EST
Posts: 24
Thanks: 3
Thanked 0 Times in 0 Posts
Thanks a lot Corona688
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Gawk Script in Windows batch file - Help 10000springs Windows & DOS: Issues & Discussions 6 01-12-2011 02:16 PM
Check the file size using Batch script in windows krackjack Windows & DOS: Issues & Discussions 0 02-03-2010 07:36 AM
Change the Windows Batch script to UNIX shell script. tomailraj Shell Programming and Scripting 5 08-27-2009 11:44 AM
Running a BATCH script from my korn script with multiparameters prash184u Shell Programming and Scripting 7 07-23-2009 11:41 AM
bash - batch script for extracting one file from multiple tar files kuliksco Shell Programming and Scripting 4 11-13-2007 01:34 AM



All times are GMT -4. The time now is 08:15 PM.