Results 1 to 5 of 5
Bit of background -
We're running a number of linux boxes that we log into remotely - all with the same user ID. One of the boxes has a multi ...
- 07-11-2008 #1
login script to change user name?
Bit of background -
We're running a number of linux boxes that we log into remotely - all with the same user ID. One of the boxes has a multi user licence for the software we use, but it has problems when more than one user with the same user name are using certain tools in it, so we've got <username>, <username1>, <username2> on it.
Question -
Is it possible to write a login script (shell being used is cshell) to check the number of instances of <username> on the system? If there's more than 1, then su -<username1>, unless there's 1 of those, then instead, su -<username2> ?
If so, how would it be done? I was thinking something along the lines of grepping into the who command... but I'm fairly new to this.
Cheers,
Tom
Edit :: Posted this here as the box is running Redhat... but maybe it should have been better in scripting forum? :/Last edited by Tom_H; 07-11-2008 at 11:26 AM. Reason: obvious - the 'edit' says it.
- 07-12-2008 #2Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
For real simplicity I'd add a character to each username rather than an incrementing digit and do something like:
The space after the $LOGNAME in the grep is needed to avoid matching "user" with "userx" and you'd need to start with a short username so you don't run out of characters too quickly (and you don't have too many users wanting to login simultaneously). Anything more sophisticated would either require unique profiles for each username (then you could hardcode the next one in sequence) or scripting to increment the username number, which I'll leave as an exercise for the reader.Code:if who | grep "$LOGNAME " then su - ${LOGNAME}x fi
I doubt this will work in the cshell, but if you will use such an abomination you deserve to have to convert the script yourself!
- 07-13-2008 #3
Great, gives me somewhere to start - even if I have to convert bits.

Problem is, I can program (in general) and can work what I want to do, but the exact syntaxes are all new to me.
And the cshell is needed for the software we use - I'm slightly more familiar with bash (though only slightly).
=============
Edit.
Now my brain has kicked into gear after coffee, there's going to be a maximum of 3 users using the software on that machine anyway (licence is for 3). The username they use is fixed, but with a 1 or 2 as a postfix - but that could be changed to letters. So, although it is relatively clumsy in general, for this specific case could I do:
Or would it be easier to just give each username it's own "check for self" script, as I presume even when su'ing to another user it'll run it's own login script?Code:if who | grep "$LOGNAME " then if who | grep "$LOGNAME1" then su - ${LOGNAME}2 -PASSWORD fi su - ${LOGNAME}x -PASSWORD fi
So, if log in as 'username' it'll check for instances of itself, then change to username1. This will check for itself, and change to username2.
I can see them both working - but if everyone is logging in as username, I was presuming initially to only make changes to that login script.
- 07-14-2008 #4Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
I've had a bit of a think and you could do it with a single login script, but all three of your users would use it (ln ~username/.login ~username1/.login; ln ~username/.login ~username2/.login (assuming csh supports the ~ convention, modify as necessary)).
Use the case statement in the login script (again this will work in bash/ksh)
This way adding another instance just involves creating the additional user and adding a line to the case statement in .login, and as it's hard-linked to all usernames they'll all pick up the change immediately.Code:if who | grep "$LOGNAME " then case $LOGNAME in *1) su - username2 ;; *2) su - username3 ;; *) su - username1 ;; fi
- 07-14-2008 #5
Okay, I've looked up the relevant syntaxes in cshell - the case stuff uses 'switch' but other than that it seems similar.
This is what I'm now running with:
=================Code:if who | grep "$user " then switch $user case user: echo "Already a user, switching to user1" su user1 case user1: echo "Already a user1, switching to user2" su user2 case user2: echo "Already a user2, switching to user3, not checking for other instances of user3!" su user3 default: echo "Staying as" $user endsw fi
EDIT:
Tried this, and it's got a syntax error on line 5. Not sure what I've done wrong there. :S (other than having to use the C-Shell in the first place...)
=================
Was hoping there would be a way to pass the password as an argument to the su command, but --help isn't suggesting one. As this way, the password needs entering quite a few times if you get bumped all the way through.


Reply With Quote