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
'cd /var/lib/code/ radek ' if logged from 210 computer
'cd /var/lib/code/ mike ' if logged from 209 computer
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]##*.}]}
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
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}'`
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.