When working with SSH (Secure Shell) sessions, it is often necessary to execute remote commands and then exit the session automatically. This can be achieved by creating a shell script that combines the necessary commands to execute the remote commands and exit the session.
In this article, we will guide you through the process of creating a shell script that will help you exit an SSH session after executing remote commands. This script can be particularly useful for automating tasks or running commands on multiple remote servers.
Step 1: Create a New Shell Script
The first step is to create a new shell script file. You can use any text editor to create the script. For example, you can use the vi editor by running the following command:
$ vi exit_ssh_script.sh
This command will create a new file named exit_ssh_script.sh. You can replace exit_ssh_script with any name you prefer.
Step 2: Add Shebang and Initial Variables
The next step is to add the shebang at the beginning of the script file. The shebang specifies the interpreter that will be used to execute the script. In this case, we will use the /bin/bash interpreter.
Below the shebang, you can define any initial variables that you may need for your script. For example, you can define the SSH username, password, and the IP address of the remote server. You can use the following code as a starting point:
#!/bin/bash
# Define initial variables
USERNAME="your_username"
PASSWORD="your_password"
REMOTE_IP="remote_server_ip"
Make sure to replace your_username, your_password, and remote_server_ip with your actual SSH credentials and the IP address of the remote server.
Step 3: Add SSH Command
In this step, you will add the SSH command that will establish the connection to the remote server and execute the desired remote commands.
Below the initial variables, add the following code:
# Execute remote commands via SSH
sshpass -p $PASSWORD ssh -o StrictHostKeyChecking=no $USERNAME@$REMOTE_IP << EOF
# Remote commands go here
echo "Hello, world!"
echo "This is a remote command."
EOF
In this code, we use the sshpass command to provide the SSH password non-interactively. The -p option is followed by the password stored in the PASSWORD variable.
The ssh command is used to establish the SSH connection to the remote server. We use the -o StrictHostKeyChecking=no option to disable strict host key checking, which can cause issues when connecting to a new remote server.
Between the ssh command and the EOF keyword, you can add any desired remote commands. In this example, we simply echo a couple of messages. You can replace these commands with the actual commands you want to execute on the remote server.
Step 4: Add Exit Command
Finally, we need to add the command that will exit the SSH session after executing the remote commands. You can add the following code at the end of your script:
# Exit SSH session
exit
The exit command will terminate the SSH session and return you to your local shell.
Step 5: Save and Make Script Executable
After you have finished adding the necessary code to your script, save the file and exit the text editor.
Before you can run the script, you need to make it executable. You can do this by running the following command:
$ chmod +x exit_ssh_script.sh
This command grants execute permissions to the script file.
Step 6: Run the Script
Now that the script is executable, you can run it by executing the following command:
$ ./exit_ssh_script.sh
Replace exit_ssh_script with the actual name of your script file, if you used a different name.
When you run the script, it will establish an SSH connection to the remote server, execute the specified remote commands, and then exit the session automatically.
Congratulations! You have successfully created a shell script to exit an SSH session after executing remote commands.
Conclusion
Automating SSH sessions can greatly simplify and streamline your workflow. By creating a shell script that combines the necessary commands, you can easily execute remote commands and exit the session automatically. This can be particularly useful for tasks that need to be performed on multiple remote servers or for automating repetitive tasks.
| Reference | Link |
|---|---|
| SSH (Secure Shell) | https://www.ssh.com/ssh/ |
| sshpass command | https://manpages.debian.org/stretch/sshpass/sshpass.1.en.html |