Write batch script which logs onto a server with a putty session and then executes su…
- User
Write batch script which logs onto a server with a putty session and then executes su testadmin and ls -la as sudoer testadmin
- Assistant
Unfortunately, as an AI language model, I do not have access to a server or Putty to test the commands you have provided. However, the following is an example batch script that shows how to log in to a remote server using the PuTTY and execute two commands as the
testadminuser using thesudocommand:@echo off set SERVER_IP=192.168.0.100 set USERNAME=administrator set PASSWORD=secret echo Starting PuTTY... start putty.exe %USERNAME%@%SERVER_IP% -pw %PASSWORD% -m commands.txt echo Done.In the above script, we first specify the IP address of the remote server, the username and password of the login credentials. Then we start the PuTTY using
start putty.execommand where we use the%USERNAME%,%SERVER_IP%, and%PASSWORD%variables.The
-moption is used to specify a file containing commands to be executed in the remote session. We will create the file that contains the two commands we wish to execute on the server: - User
I want to store the commands in the batch script. Also I want the batch script to ask me for the password after executing su testadmin. Also I want to use a putty session name
- Assistant
Sure, here's an updated batch script that meets your requirements:
@echo off REM Define variables set SERVER_IP=192.168.0.100 set USERNAME=administrator set PUTTY_SESSION_NAME=my_session set PASSWORD= REM Ask for password set /p PASSWORD=Enter the password: REM Define the commands set COMMANDS=( "su testadmin" "echo %PASSWORD% | sudo -S ls -la" ) REM Write the commands to a file echo %COMMANDS% > commands.txt REM Start the PuTTY session putty.exe -load %PUTTY_SESSION_NAME% -l %USERNAME% -pw %PASSWORD% -m commands.txt REM Clean up the commands file del commands.txt echo Done.In this script, we define the
SERVER_IP,USERNAME,PUTTY_SESSION_NAME, andPASSWORDvariables. We then prompt the user to enter the password using theset /pcommand.Next, we define the commands as an array variable called . It contains two commands: first, we execute to switch to the user, and then we execute the command with the option to read the password from standard input.