1830165 Members
6443 Online
109999 Solutions
New Discussion

Shell Scripting

 
SOLVED
Go to solution
mvr
Regular Advisor

Shell Scripting

Hello,

I'm writing a script to automate some tasks on the network. The problem that I came a cross is that I don't know how to open /etc/passwd file, edit the line, saved and exit.
What I would like to do is the next:
our PDC is on HP-UX 11.00.
In order for the workstation to join domain, it has to have $ in the end of the name e.g.
wsat1210$:*:125:102:MVRMachine:/dev/null:/bin/false

This is not a standard characters for the password file, so I can't do that via SAM.

I know that there is the command to open file for editing, but I didn't have too much luck doing it.

Thank you
Miro
10 REPLIES 10
Chris Vail
Honored Contributor
Solution

Re: Shell Scripting

This doesn't sound like a terribly good idea to begin with, but its your system. In Unix, you can do almost anything, including hack the password file. Easiest would be to use sed.
sed 's/wsat1210/wsat1210$/' /etc/passwd>/tmp/somefile
mv /tmp/somefile /etc/passwd

This uses the 'swap' command to exchange "wsat1210" with "wsat1210$", and copies the result into a temporary file. The second command copies the temporary file over the original. It would be an EXCELLENT idea to make a backup copy of your password file before attempting this.

Good Luck
Chris
Rodney Hills
Honored Contributor

Re: Shell Scripting

Many potential problems can occur with the kind of changes you're attempting to do.

But, to make a change to any file, many tools exist. Some of the more popular are "perl", "awk", and "sed".

Using "sed" you could do the following (again editting the /etc/passwd file could have unexpected results, especially on a "trusted" system) -

sed -e 's/^wsat1210:/wsat1210$:/' /etc/passwd2
mv /etc/passwd /etc/passwd.orig
mv /etc/passwd2 /etc/passwd

Good Luck (be sure you have good backups...)

-- Rod Hills
There be dragons...
harry d brown jr
Honored Contributor

Re: Shell Scripting


Are you confusing workstation name with username ??? ==> "In order for the workstation to join domain"

What kind of "domain" requires a $ at the end of a users name?

live free or die
harry
Live Free or Die
Ramkumar Devanathan
Honored Contributor

Re: Shell Scripting

This is for Harry Brown. Hi - you require such usernames suffixed with a $ when you have your unix box acting as a pdc for a windows domain, with samba running.

As for you Miro i have nothing much to add over the info the others have posted.

regards,
Ramkumar.
HPE Software Rocks!
David_246
Trusted Contributor

Re: Shell Scripting

Hi Miro,

I guess you do know $user ??

#!/bin/ksh

userline=`/usr/bin/cat /etc/passwd | grep "^${user}:"`

/usr/bin/cat | grep -v "^${user}:" >/etc/passwd.mv

user="${user}\$"
userline=`echo $userline | awk -F: '{print $2 ":" $3 ":" $4 ":" $5 ":" $6 ":" $7}'`
userline="${user}:${userline}"

echo "$userline">>/etc/passwd.mv
rt=`/usr/bin/diff /etc/passwd /etc/passwd.mv`

if [ "$rt" < "5" ]
then
mv /etc/passwd.mv /etc/passwd
else
/usr/bin/logger -p user.warning "$0: Error in addapting user !!"
fi

I prefer perl for doing things like this, but he. You have to be able to understand perl for that. And as you can see I'm not very good at writing shell-scripts.


Regs David
@yourservice
Sylvia Nikolova
New Member

Re: Shell Scripting

Hi Miro,

Try the following lines:
awk 'BEGIN { FS = ":"; OFS = ":"} {$1=$1"$"; print $0}??? /../yourfile > file1
mv file1 /../yourfile

Hope this help you!!!
Regards,

Sylvia
mvr
Regular Advisor

Re: Shell Scripting

Thank you all for the comments as well as help. I know that is not the best idea to do this with password file, but I have to. In our environment in order to add a new user and the new workstation in to domain we need 6 steps. Most of the time there is confusion what need to be done firs and the way to do it. On this way it will be strait forward and hopefully no problems.

Miro
John Poff
Honored Contributor

Re: Shell Scripting

Hi,

Lots of neat solutions. Here is a real easy one. Use the 'useradd' command as root and you can specify a '$' in the user ID. Like this:

useradd -u 50001 -g users -d /home/users/testjp "testjp$"


This gives me this entry in /etc/passwd:

testjp$:*:50001:20::/home/users/testjp:/sbin/sh

Of course, you'll still have to set the password but you can use the 'passwd' command to set the password discipline, requiring a change at the first login.

Just one way to do it.

JP
John Poff
Honored Contributor

Re: Shell Scripting

If you want to add the '$' to an existing account on the system, you can use the 'usermod' command, like this:

# tail -1 /etc/passwd
testjp:*:50001:20::/home/users/testjp:/sbin/sh

# usermod -l "testjp$" testjp
# tail -1 /etc/passwd
testjp$:*:50001:20::/home/users/testjp:/sbin/sh


JP
Cody Godines_1
Occasional Advisor

Re: Shell Scripting

Hi there,
Try this:

#!/bin/tcsh
# SCRIPT

set numPassLines = (`grep -c ":" passwd`)
@ i = 1
while ( $i <= $numPassLines )
echo $ >> passwd
echo -n "Gx" >> temp_vi_cmd
echo -n "$i" >> temp_vi_cmd
echo -n "G" >> temp_vi_cmd
echo "wP" >> temp_vi_cmd
echo "ZZ" >> temp_vi_cmd
(vi passwd < temp_vi_cmd) >> & temp
rm -f temp temp_vi_cmd
@ i ++
end
unset numPassLines