I was already very pleased with the sed one-liners, but today I found grab bag. A fully loaded bag of sed scripts, tutorials, etc.
Why didn’t I check http://sed.sourceforge.net before???
I was already very pleased with the sed one-liners, but today I found grab bag. A fully loaded bag of sed scripts, tutorials, etc.
Why didn’t I check http://sed.sourceforge.net before???
When creating user accounts, it is tempting to use a default password and ask the user to change it right after logging in the first time. Unless the user is forced to do this by means of a password policy, most users are just happy with the supplied password.
I know about a company (a hospital!) that uses one and the same password for new email accounts, allowing (ex)colleagues to read each others’ email!
A nice utility to generate random passwords is pwgen. The passwords are semi-random: random enough to be safe, but not too difficult to remember. By default, pwgen presents you a list from which you can choose:
$ pwgen Vahvoi5u Da5ahrah Ogo0voh0 Fo3rieji aiw5zeuR qui1aiYo thae5Chu Ha8bohSu oa2Eu3go JooQuia8 Aroh7Ohc UCoo4Hah Woocee1i Ahnein6s di6aGeph fi5Ahngu ooRe8eeW Aish0sha aikei3Fa Abohhai3 aeNgoo6o yiw2keiS eiyo5geH To5we6ey
… and 16 more lines. The man page explains that this is done so that you can pick a password without being afraid of someone ‘shoulder surfing’ for the chosen password.
Used from within a script (non-interactively), pwgen will generate a single password:
$ pwgen | wc
1 1 9
Using pwgen to create a new user would involve a number of steps:
You can argue about weather it is safe to write down the password, that’s another discussion.
I’ve written a little script that combines these steps.
The most interesting part of the script is the way that the generated password is fed in to the useradd command. The useradd option -p allows you to supply the encrypted password. “…, as returned by crypt(3)”, the man-page says.
Well, that brings up a problem. Isn’t there a binary or built-in equivalent? I would rather see crypt(1) being referenced… but that tool doesn’t exist.
This is where openssl comes to help! It can generate a passwd style hash for a given password. For example, to generate a hash for ‘mypassword’, execute:
$ openssl passwd -1 mypassword $1$xBN626Gk$vsK88ODo4CfQ3pBx9Z1vD0
This looks like something that we can feed to useradd!!
The minimalist version of the script now looks like:
#!/bin/sh
# first argument is the username
USER=$1
# generate password
PASSWORD=`pwgen -cn -1`
# generate password hash
PW_HASH=`openssl passwd -1 ${PASSWORD}`
# create the user account
useradd -M -p ${PW_HASH} ${USER}
# display username and password
echo Account created:
echo Username: ${USER}
echo Password: ${PASSWORD}
The full script adds some ‘fancy features’ like checking the input argument and printing a receipt with the username and password and a link to the documentation.
I’m using the script to add accounts to a server that acts as a SSH gateway. There’s no need for home directories on this computer. By using the -M option for useradd, no home directory will be created.
Maybe I should write my next Nerdnote(tm) about setting up an SSH server to use as a secure gateway to a private network.
I just wrote vncshare, a script that allows you to temporary share your vnc session. It changes the vnc password to ‘helpme’ for 30 seconds. Then it gets reverted to the original one, giving your colleague a time window to login to your vnc session. The script makes use of the ‘trap’ command to make sure the password is always reverted when the script is aborted.
Try the following Gostscript one-liner:
$ gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=combined.pdf article1.pdf article2.pdf notes.pdf
Typo3update is designed to directly update content to a Typo3 website from the command line. It works as a network service and can be used from any machine that can connect to the server running the Typo3 website. For example, quota overviews and cluster news information that is available from command line programs or text files can be updated to Typo3 using simple bash scripts. Typo3update takes the actual content from standard input. Additionally, it requies two command line options: uid and the header.
Have a look at the following example:
$ cat textfile | typo3update 9533 "About"
This will insert the contents of textfile into content element 9533 that has the heading “About”. The heading parameter is to double check the content element id. If the id doesn’t match the supplied heading, typo3update refuses to update (overwrite) the content. Instead, it writes a line to an error log file.
The content element should be created first from the Typo3 backend. To identify the uid of the content element in a certain page, just hover your mouse pointer above the content element icon. The uid will show up in a tool-tip:

(more…)
Redirect stdout to stderr:
$ echo Oops, something went wrong >&2
Redirect stderr to /dev/null:
$ ls /home /qwerty 2> /dev/null
home:joe john
To get rid off all output:
$ ls /home /qwerty >/dev/null 2>/dev/null
# or
$ ls /home /qwerty >/dev/null 2>&1
Just a very short note. It’s not so difficult, but I keep on forgetting the syntax. Here it is:
me@localhost$ tar cf - . | \
ssh user@otherhost 'cd dir; tar xf -'
Today, I wrote a little bash scripts that scans the network for computers that are not registered in DNS. Reverse DNS to be more precise. It uses nmap to do a reverse DNS lookup for each computer it pings:
$ nmap -sPR -oG - 192.168.0.*
The result is filtered for failed DNS lookups:
$ nmap -sPR -oG - 192.168.0.* | grep "()"
Since all computers are contacted their mac address is known in the ARP (Address Resolution Protocol) table:
$ /sbin/arp | grep -w 192.168.0.35 | awk '{ print $3 }'
After looking up the MAC address, a table is presented with ip addresses and their mac addresses. This is the complete script.
While there seems nothing wrong with this:
sign="--"
awk '{if ($1 == $sign ) print $1 }'
..it doesn’t give the intended result. To be able to use bash variables in awk, you first need to assign the variables with the -v option:
sign="--"
awk -v sign="$sign" '{if ($1 == sign ) print $1 }'
From the awk man page:
The option -v followed by var=value is an assignment to be done before (the awk) prog is executed; any number of -v options may be present.
Met hdiutil kun je vanaf de command line eigenlijk alles doen wat je met ‘Disk Utility’ kan doen. Ook het dubbel klikken van een dmg bestand vanuit finder is een actie die je hiermee kunt automatiseren en dus ook vanaf afstand met ssh kan uitvoeren. Hieronder een voorbeeld hoe je AdiumX zou kunnen installeren:
$ wget http://adiumx.cachefly.net/AdiumX_0.89.1.dmg $ hdiutil attach AdiumX_0.89.1.dmg /dev/disk4 Apple_partition_scheme /dev/disk4s1 Apple_partition_map /dev/disk4s2 Apple_HFS /Volumes/Adium X 0.89.1 $ ditto /Volumes/Adium\ X\ 0.89.1/Adium.app /Applications/ $ open /Applications/Adium.app
Maar goed, wat is er nou makkelijker dan:

Voor alle opties, zie ‘man hdiutil‘