Login shell script

Latest response

This is the first time I have posted here so forgive me if this is not the correct place to post a question like this.
If I wanted a script to launch every time I SSH/logged into a server, where would I put it?
Here's the scenario:

Server1 - On secured network1, accessible by Server2 & Server3; not accessible by Workstation1
Server2 - On secured network1, accessible by Server1 & Server3; not accessible by Workstation1
Server3 - On secured network2, accessible by Server1, Server2 & Workstation1
Workstation1 - On unsecured network3

Server1 & Server2 are my destination servers, via SSH, from Workstation1. However, I have to use Server3 to get to Server1 & Server2; Server3 is kind of like a jump server to get from Workstation1. Every time I SSH to Server3, I would like the login script to launch. What I plan on putting in this script is the option to ask me, as soon as I login, if I I want to connect (SSH) to one of the servers and, depending on my reply, I would then either not SSH to one of the servers and just continue working on Server3 or SSH to one of the servers that I choose.
Thanks.

Responses

Hi Edward,

Why not make aliases like: server1="ssh -q -r Server3 ssh -q Server1" When you start working on Server3, you just ssh Server3 and for Server1 and Server2 you use the aliases and you do not need to use an interactive menu.

Regards, Siem

Hmmmm...okay. I was not aware I could do that. Currently, I have $HOME/.ssh/config configured for easy SSH connections on my workstation. Can I add that there? Here is an example of what I have:

Host admin
    HostName server3.local
    User ecrosby

 Host sate
    HostName server5.local
    User ecrosby

 Host db
        HostName server7.local
        User ecrosby

Aliases can be defined in your .bashrc. The alias should not use the -r option, but the -t option:

alias server1="ssh -q -t Server3 ssh -q Server1"

After further research and testing this is what I found in regards to my question and it seems to work pretty well. I placed a login script in my .bash_profile that launched as soon as I logged in via SSH. This is what the shell script looks like:

#!/bin/bash
#
# This script will be referenced in .bash_profile as a login script

echo
echo "=========================="
echo
echo "^^^ Do you want to SSH to a server? (yes/no) ^^^"
read REPLY
if [ "$REPLY" == "yes" ]; then
        echo
        echo "Which server? server1, server2 or server3?"
        echo
elif [ "$REPLY" == "no"  ]; then
        echo
    exit 0
else
        echo
        echo "invalid answer, type yes or no";
fi
read SRVREPLY
ssh $SRVREPLY
sleep 2

To answer your question simply, you would use (as you discovered) ~/.bash_profile (or .bashrc) as the latter is actually included by the former.

In a former job I had created a pretty sweet setup similar to what you might be looking for (unfortunately I did not bring the profile files with me so I can't give you exact code). But basically I configured [password-protected] SSH keys between the hosts. They were password protected. I set it up so that .bash_profile called ssh-agent which prompted me for my SSH key password and then loaded the key into the session. After that I would launch screen and automatically open a connection to each of my backend servers (in your case server1 and server2) in a separate window within screen.

Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.