tags:

views:

68

answers:

3

I want to so something like that in bash (.bashrc) so the alias is set based on which comp the user logged in. I don't know how to get the 210 from 10.0.0.210 and then the best way of going through of the list 'user=xxx'

$radek ='210'
$mike ='209'


#SSH_CLIENT='10.0.0.210 53039 22'  <--- system variable
$user = based on the 4th part of IP so 
   $user = radek if 210
   $user = mike if 209

alias sites='cd /var/lib/code/'+$user

so the final alias looks likeg

'cd /var/lib/code/radek' if logged from 210 computer

'cd /var/lib/code/mike' if logged from 209 computer


Final code thanks to @Dennis Williamson

users[210]=radek
users[209]=mike

octet=($SSH_CLIENT)    # split the value on spaces
#octed=${octet[0]##*.}        # extract the last octet from the ip address
alias sites='cd /var/lib/code/'${users[${octet[0]##*.}]}
+1  A: 

try this:

export user=`env|grep -i SSH_CLIENT|cut -d' ' -f1|cut -d'.' -f4`

and remove the + in the alias. It should just be

alias sites='cd /var/lib/code/'$user

Based on your edit hopefully this should work:

temp_user=`env|grep -i SSH_CLIENT|cut -d' ' -f1|cut -d'.' -f4`
user=`env|awk -F= "/=$temp_user/"'{print $1}'`
domino
@domino: what about the name? I need to substitute the 210 with radek but I want to make it general as I there is few of us :-)
Radek
+1  A: 

If you dont have a hard requirement for the format to store the user to ip mapping, the following sample script will work :

user_210="radek"
user_209="mike"

function define_alias
{
        local ip_last_part=`echo $1 | cut -d ' ' -f1 | cut -d '.' -f4`
        eval user=$`echo "user_$ip_last_part"`
        echo "User '$user' identified for ip ending in '$ip_last_part'"
        alias sites="cd /var/lib/code/$user"
        echo "Alias defined : `alias sites`"
}


#Exampe usage :

# will come from env
export SSH_CLIENT='10.0.0.210 53039 22'
define_alias $SSH_CLIENT


export SSH_CLIENT='10.0.0.209 53039 22'
define_alias $SSH_CLIENT

If you dont want to use the function, you copy the code in the function out side and use domino's suggestion to get the last part of the IP. Like this :

user_210="radek"
user_209="mike"
ip_last_part=`env | grep -i SSH_CLIENT | cut -d ' ' -f1 | cut -d '.' -f4`
eval user=$`echo "user_$ip_last_part"`
echo "User '$user' identified for ip ending in '$ip_last_part'"
alias sites="cd /var/lib/code/$user"
echo "Alias defined : `alias sites`"

HTH,
Madhur Tanwani

madhurtanwani
+2  A: 

Give this a try:

users[210]=radek
users[209]=mike

octet=($SSH_CLIENT)    # split the value on spaces
octet=${octet[0]##*.}  # extract the last octet from the ip address
alias sites='cd /var/lib/code/'${user[octet]}

Another way to assign users:

names=(bob jim anne kelly rick)
octet=211
for name in ${names[@]}
do
    users[octet++]=$name
    if (( octet > 255 ))
    then
        echo "Error: range limit exceeded"
        break
    fi
done
Dennis Williamson
@Dennis Williamson: wow, wow, wow. It's very nice. I had to modify the code little bit to make it work. Thank you so much. `users[210]=radekusers[209]=mikeoctet=($SSH_CLIENT) # split the value on spaces#${octet[0]##*.} # extract the last octet from the ip addressalias sites='cd /var/lib/code/'${users[${octet[0]##*.}]}`
Radek
@Radek: Oops, sorry, it was a typo on my part. Your version works, but I'm editing the answer to show what I had intended.
Dennis Williamson
@Dennis Williamson: don't be sorry :-) You helped me so you can be only proud of yourself. The most elegant answer ...
Radek