Execute commands from script in current bash session

I have a file as follows:

cat /etc/mxg/ssh-hosts
mx.example1.com.au:2225
mx2.example2.com.au:2225
mx.example3.com.au:2225
mail.example4.com.au:2225
mail.example5.org.au:2225
mail.example6.com.au:2225

I want to dynamically create aliases for quick access to these servers from bash. I wrote the following script as a starting point before I figure out how to have the aliases generated when I logon to bash.

cat /usr/local/bin/sshaliases.sh
#!/bin/bash

SSHHOSTS=/etc/mxg/ssh-hosts

for sshhost in $(cat $SSHHOSTS); do
        SSHALIAS=$(echo $sshhost | awk -F"." '{print $2}')
        SSHHOST=$(echo $sshhost | awk -F":" '{print $1}')
        PORTNO=$(echo $sshhost | awk -F":" '{print $2}')
        alias $SSHALIAS="ssh -p $PORTNO $SSHHOST"
done

I cannot understand or find the answer on how I export the alias line to my current shell. i.e. I need the result of the alias command to be run in my shell so the aliases are generated, e.g.

run

sshaliases.sh

(or have it run when I logon to a bash session) and then I can run...

example1 

which will actually run...

ssh -p 2225 mx.example1.com.au

Additionally if anyone knows the most logical way to integrate this so it executes when I logon to bash that would also be helpful. I believe putting it in a function would work?

[house@leonov] cat .ssh_hosts
mx.example1.com.au:2225
mx2.example2.com.au:2225
mx.example3.com.au:2225
mail.example4.com.au:2225
mail.example5.org.au:2225
mail.example6.com.au:2225
[house@leonov] cat .bashrc
for HOST in $( cat .ssh_hosts )
do
  ALIAS="ssh_$( echo $HOST | awk -F "." '{print $2}' )"
  HOST=$( echo $HOST | awk -F ":" '{print $1}' )
  PORT=$( echo $HOST | awk -F ":" '{print $2}' )
  alias $ALIAS="ssh -p $PORT $HOST"
done
[house@leonov] source .bashrc
[house@leonov] alias | grep '[ssh]_'
alias ssh_example1='ssh -p  mx.example1.com.au'
alias ssh_example2='ssh -p  mx2.example2.com.au'
alias ssh_example3='ssh -p  mx.example3.com.au'
alias ssh_example4='ssh -p  mail.example4.com.au'
alias ssh_example5='ssh -p  mail.example5.org.au'
alias ssh_example6='ssh -p  mail.example6.com.au'
1 Like

Thanks dr.house!

However using $HOST overwrote my shells default $HOST and broke the script and my shells $HOST env.

I changed it to the variables I had originally and it works a treat.

Nice :slight_smile:

Edit: actually I'm talking rubbish sorry, its becuase you used HOST twice as a variable.

I ended up putting this in, which works:

for SSHHOST in $( cat /etc/mxg/ssh-hosts )
do
  SSHALIAS=$( echo $SSHHOST | awk -F "." '{print $2}' )
  SSHHOSTNAME=$( echo $SSHHOST | awk -F ":" '{print $1}' )
  SSHPORT=$( echo $SSHHOST | awk -F ":" '{print $2}' )
  alias $SSHALIAS\="ssh -p $SSHPORT $SSHHOSTNAME"
done

Regards

Avoid the use of backticks or $() and cat:

while read HOST
do
  ALIAS="ssh_$( echo $HOST | awk -F "." '{print $2}' )"
  HOST=$( echo $HOST | awk -F ":" '{print $1}' )
  PORT=$( echo $HOST | awk -F ":" '{print $2}' )
  alias $ALIAS="ssh -p $PORT $HOST"
done < ssh_hosts

Don't go for the Useless Use of Cat Award :).

1 Like

Thanks Franklin52,

So for anyone who reads this thread, the way I have it setup now in .bashrc is:

while read SSHHOST
do
  ALIAS="ssh_$( echo $SSHHOST | awk -F "." '{print $2}' )"
  HOST=$( echo $SSHHOST | awk -F ":" '{print $1}' )
  PORT=$( echo $SSHHOST | awk -F ":" '{print $2}' )
  alias $ALIAS="ssh -p $PORT $HOST"
done < /etc/mxg/ssh-hosts