Execute commands from script in current bash session


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute commands from script in current bash session
# 1  
Old 07-15-2010
Execute commands from script in current bash session

I have a file as follows:

Code:
cat /etc/mxg/ssh-hosts

Code:
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.

Code:
cat /usr/local/bin/sshaliases.sh

Code:
#!/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
Code:
sshaliases.sh

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

Code:
example1

which will actually run...
Code:
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?
# 2  
Old 07-15-2010
Code:
[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'

This User Gave Thanks to dr.house For This Post:
# 3  
Old 07-15-2010
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 Smilie

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

I ended up putting this in, which works:

Code:
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

Last edited by jelloir; 07-15-2010 at 08:56 AM..
# 4  
Old 07-15-2010
Avoid the use of backticks or $() and cat:
Code:
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 Smilie.
This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 07-15-2010
Thanks Franklin52,

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

Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finish current script and execute next script

Hi, I've come accross a situation where I need to exit from current shell script at the same time I need to start/activate another shell script. How can I do that in KSH ?? Need help !! For example, my script is as below #!/bin/ksh paramFile="/home/someXfile.lst" ] && <<Here I... (1 Reply)
Discussion started by: R0H0N
1 Replies

2. Shell Programming and Scripting

i want to execute shell script on remote server with in sftp session

Hi, I want to execute shell script with in sftp session for remote server. like i have a shell script test.sh that is on local server.i want to execute that script on remote server sftp user@192.168.56.10 sftp> test.sh ---execute for remote server not for local server. sftp... (3 Replies)
Discussion started by: SAUD PASHA
3 Replies

3. Shell Programming and Scripting

running a bash script even after logging out from the current session

HI , I have a simple script that moves files from one folder to another folder, I have already done the open-ssh server settings and the script is working fine and is able to transfer the files from one folder to another but right now I myself execute this script by using my creditianls to... (4 Replies)
Discussion started by: nks342
4 Replies

4. Shell Programming and Scripting

Execute shell script within sftp session

Hi all , can any one tell me how to run a script within a sftp session. let me tell u in bit clear way : After I connected to sftp location , cd ing to some directory then I need to execute a one script. Please tell me if u have any idea on this . Looking forward to your reply guys... (1 Reply)
Discussion started by: sravan008
1 Replies

5. UNIX for Dummies Questions & Answers

Export script to current session

Hi, I have a script called bash$> cat -ev setprofile.sh alias rm='rm -i'$ $ The alias does not take effect unless i run the script as bash$> . ./setprofile.sh What do I have to do in-order to simply run (9 Replies)
Discussion started by: shifahim
9 Replies

6. Shell Programming and Scripting

Execute ssh commands through bash script

Hi all! I am trying to write a script that will check if a certain directory is available at several different nodes and then do stuff in it ..... On the beginning of the script I give as a variable the directory and the number of the nodes and then I loop like this: for... (3 Replies)
Discussion started by: idet2
3 Replies

7. UNIX for Advanced & Expert Users

How to execute multiple unix commands in one session from java

Hi, Iam trying to code in java and wanted to run the commands in the Unix remote servers. I have the following code to run multiple GREP commands in a single session. But when i execute this, the first command executes successfully, whereas from the next line it says "Exception Occured... (1 Reply)
Discussion started by: gravi2020
1 Replies

8. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

9. Shell Programming and Scripting

How do i execute script in the current shell

How do i run a shell script or perl script with in the context of a current shell. I know that i can use command source. but we can't pass any arguments to our script if we use source command as it takes only one argement i.e filename Is there any way to run a script in the current shell... (5 Replies)
Discussion started by: Naresh Kumar
5 Replies

10. Shell Programming and Scripting

how do i get my script to execute multiple commands?

New to shell scripting. I can't get my script to execute multiple commands. Here's the code. It's a menu script. #!/bin/ksh clear print "SDE MENU" PS3="SDE MENU, enter choice:" select clean_menu in "tasdedev instance 5151" "orkindev instance 5155" "tasdetst instance 5157" "orkinsys... (1 Reply)
Discussion started by: hvincent
1 Replies
Login or Register to Ask a Question