1

We have this setup where we have password authentication to our remote unix server. I always run the same commands every day and it is very repetitive. What I basically do is:

  1. Open putty, enter hostname
  2. input username and password
  3. sudo su - oracle
  4. input password again
  5. cd /someDirectory
  6. ./script.sh
  7. copy output of script and save to txt file

I am able to automate step 1 and 2 by writing a small bat file, but I don't know how to do the rest of the steps

putty.exe -ssh <user>@<server> -pw <password>
demiglace
  • 405
  • 1
  • 5
  • 12
  • Your question title does not correspond to your question text. While it makes sense to automate PuTTY to login with sudo, you cannot use PuTTY for the rest of your task. See [Automating command/script execution using PuTTY](https://stackoverflow.com/q/39361444/850848) and then [Executing “su” with PuTTY Plink from a batch file](https://stackoverflow.com/q/66701264/850848). – Martin Prikryl Aug 08 '21 at 11:55

1 Answers1

2

You can't with putty, but with the included plink command:

@echo off
REM Automate PUTTY use
set /P user="Enter User:"
set /P server="Enter Server:"
set /P password="Enter Password:"
set /P logfile="Enter SSH logfile:"

REM SUDO SU Requires ASKPASS and X11 forwarding to be configured
REM 

plink.exe -ssh -sshlog %logfile% %user%@%server% -pw %password% "cd temp; pwd; ./script.sh"

REM clear inputs from environment
set "server="
set "user="
set "password="
set "logfile="
echo on

Use a command like plinktest.bat | tee readable_output.log to run the batch script and place the readable output into a file called readable_output.log.

You could place all the commands in a text file and redirect it to the batch script or use the -m switch.

I hope that gets you started.