Bash and Command line One-Liners
From KCLUG Wiki
This requires that you install the packages 'sharutils' and 'ImageMagick'.
import -type Optimize -quality 100 -silent -window root png:- | uuencode ScreenShot.png | mail -s 'Taking a screenshot from the command line' billycrook@gmail.com
#!/bin/bash
#the machine to sync accounts to
sourcemachine=yourhost
for file in passwd shadow group gshadow
do
scp ${sourcemachine}:/etc/${file} /etc/${file}.new
done
#list the db's and the from and too filenames
echo 'user /etc/passwd /etc/passwd.new
group /etc/group /etc/group.new' | \
while read object oldfile newfile
do
#image the colons are spaces. now get the name and old it number and iterate on each line
sed 's/:/ /g' ${oldfile} | \
while read name ignore oldid ignore2
do
#If the id number is between 500 and 65536 (If it's an actual person's user account)
if [[ ${oldid} -ge 500 ]] && [[ ${oldid} -lt 65536 ]]
then
unset newid
#find the correct new ID number
newid=$( grep "^${name}:x:" ${newfile} | sed -e "s/^${name}:x://" -e "s/:.*$//" )
#if newid not found, exit immediately and prompt admin
if [[ "${newid}" == "" ]]
then
echo "SEVERE ERROR: The ${object} ${name} was found in the old table, but not in the new table. Add that ${object} on the source machine, and recopy etc/passwd,shadow,group,gshadow to this one."
exit 1
fi
#If there should be no change, save some work...
if [[ ${newid} -eq ${oldid} ]]
then
echo skipping ${object} ${name} because its id is ${oldid} in both tables.
continue
fi
#find an unused id number in that table, which is also unused in the goals file for that table
for try in `seq 500 65535`
do
#search for the id number in old new, and goals
if ! grep -q ${try} ${oldfile} ${newfile} goals
then
#once you've found one not in use, write it to goals
echo ${object} ${oldid} ${try} ${newid} >> goals
#print to secreen for debugging
echo ${object} ${name} had oldid ${oldid}, and has newid ${newid}, will use ${try} as intermediary to get there.
#exit the loop that's searching for unused ids
break
fi
done
fi done
done
#go through toe goals list in two passes
for part in A B
do
#in each pass, iterate over eachline, and pick out the object, oldid, intermediaryid, and newid
cat goals | while read object oldid intermed newid
do
#in the first pass,
if [[ ${part} == "A" ]]
then
#add the action that takes the object to its intermediary ID
echo ${object} ${oldid} ${intermed} >> actions
else #in the second pass
#takle it to its destination id
echo ${object} ${intermed} ${newid} >> actions
fi
done
done
cat actions | while read object from to
do
[[ ${object} == "group" ]] && prefix=':' || unset prefix
echo chown --from=${prefix}${from} ${prefix}${to} -R / -c
done
#cleanup
rm goals actions -f
#instate the new account tables
echo mv /etc/passwd /etc/passwd.orig
echo mv /etc/shadow /etc/shadow.orig
echo mv /etc/group /etc/group.orig
echo mv /etc/gshadow /etc/gshadow.orig
I like apg, and will probably still use it, but for scripts to be dependant on one less external binary, try this:
pwchars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ##add these on if you really want to be awnry \!\`~@#$%^&*(){}[]-_=+<,>.:;?/\\|"
pwlen=10
for (( c=1; c<=${pwlen}; c++ ))
do
passwd="${passwd}${pwchars:$(($RANDOM%${#pwchars})):1}"
done
echo ${passwd}

