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 as:pqrst
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.

You can't do so using PuTTY, but the same people who make PuTTY also make plink, which can be automated.

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

Thanks Corona688.

Can you pls give more light on the code which u hav given. like how it works wen that code is run?

That would be a line you put in a batch file. plink.exe would need to be in the same directory.

plink.exe -batch -pw password username@host sudo su - < script.sh
  • plink.exe: runs plink.exe :wink:
  • -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.

Thanks

is this possible

 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

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

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:

@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.

2 Likes

Thanks a lot Corona688

Hi, I have a problem with this...plink is running fantastic on Linux, but on Tru64 (and old Alpha server!!) I try to run a script but without success...
I try with -s -bash -ssh -2 (because don't connect with default..) and nothing works...
sometimes the commands appear running locally on DOS...
Is a very strange case...any help will be appreciatte...tnx

This helped me want I was looking for ....Big thank you to Corona688

1 Like

Hello,

Please do not ask new questions in existing threads. Unless you have new information to a particular problem, please create a new thread in the appropriate forum.

Also, please search the forums first, as it might be that someone has already posted an answer for a similar problem.

Best regards,
The UNIX and Linux Forums