Linux Oneliners
perl -pe 'chomp ; s/(.)/sprintf(\"%%%02X\", ord($1))/seg;'
e.g. echo "hello" | perl -pe 'chomp ; s/(.)/sprintf("%%%02X", ord($1))/seg;'
will give: %68%65%6C%6C%6F
URL decode a string
perl -pe 's/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;'
e.g. echo "%68%65%6C%6C%6F" | perl -pe 's/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;'
will give: hello
base64 encode a string
perl -MMIME::Base64 -ne "print encode_base64($_)"
e.g.
echo "hallo een twee" | perl -MMIME::Base64 -ne 'print encode_base64($_)'
will give: aGFsbG8gZWVuIHR3ZWUK
base64 decode a string
perl -MMIME::Base64 -ne "print decode_base64($_)"
e.g.
echo "aGFsbG8gZWVuIHR3ZWU" | perl -MMIME::Base64 -ne 'print decode_base64($_)'
will give: hallo een twee
Make a screendump and save to a unique file name
xwd | convert - `date "+%Y%m%d:%H%M"`.jpg
Actually, you can also use the import command, both convert
and import come from the indispensable ImageMagick tools. The syntax
for import would be:
import `date "+%Y%m%d:%H%M"`.jpg
e.g. if you execute the command you get a crosshair cursor,
click on the window you want the screenshot of, and this will
produce a file something like: 20060821:1820.jpg
list every perl module that is outdated
perl -MCPAN -e 'CPAN::Shell->r;'
e.g.
# perl -MCPAN -e 'CPAN::Shell->r;'
CPAN: Storable loaded ok
Going to read /root/.cpan/Metadata
Database was generated on Mon, 21 Aug 2006 11:32:38 GMT
Package namespace installed latest in CPAN file
Apache::SOAP undef 0.60 BYRNE/SOAP/SOAP-Lite-0.60a.tar.gz
Archive::Tar 1.28 1.30 KANE/Archive-Tar-1.30.tar.gz
Archive::Zip 1.12 1.16 SMPETERS/Archive-Zip-1.16.tar.gz
etc....
update every perl module that is outdated on my disk:
perl -MCPAN -e 'CPAN::Shell->install(CPAN::Shell->r)'
convert a unix timestamp in a file to human readable format
perl -pe 's/(\d{5,20})/localtime$1/eg' < filename
e.g.
# echo "1197206684 logging of some action" | perl -pe 's/(\d{5,20})/localtime$1/eg'
Sun Dec 9 14:24:44 2007 logging of some action
What's the UNIX timestamp? Have a look at:
www.unixtimestamp.com
check an SSL enabled site from the commandline
openssl s_client -connect hostname:443
This will show you the certificate chain (the CA and the X.509 cert of the site itself)
and also which cyphers are user in the connection. Use :443 for an https://
website and :993 for an imaps connection, and so on.
e.g.
[ ~]> openssl s_client -connect webmail.xs4all.nl:443
CONNECTED(00000003)
depth=0 /C=NL/O=*.xs4all.nl/OU=https://services.choicepoint.net/get.jsp?GT59386789/OU=See www.rapidssl.com/cps (c)05/OU=Domain Control Validated - RapidSSL(TM)/CN=*.xs4all.nl
...
New, TLSv1/SSLv3, Cipher is DHE-RSA-AES256-SHA
Server public key is 1024 bit
SSL-Session:
Protocol : TLSv1
Cipher : DHE-RSA-AES256-SHA
etc...
get an overview of which programs are listening on the network
netstat -tupan
This will show you whether it's an UDP or TCP service,
the portnumber it's listening on and the Proccess ID plus Program name.
e.g.
[~]# netstat -tupan
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:800 0.0.0.0:* LISTEN 23517/rpc.mountd
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1964/portmap
tcp 0 0 0.0.0.0:784 0.0.0.0:* LISTEN 23500/rpc.rquotad
tcp 0 0 127.0.0.1:50000 0.0.0.0:* LISTEN 2208/hpiod
udp 0 0 0.0.0.0:781 0.0.0.0:* 23500/rpc.rquotad
udp 0 0 0.0.0.0:797 0.0.0.0:* 23517/rpc.mountd
udp 0 0 0.0.0.0:68 0.0.0.0:* 1901/dhclient
udp 0 0 0.0.0.0:111 0.0.0.0:* 1964/portmap
etc...
Use netstat -tupa to get service names instead of numbers
You can also use lsof -i
N.B.Even if a daemon is listening on a port number, connections
from the outside might be blocked off by iptables firewalling.
check with iptables -nL
Extract one file from an RPM package
rpm2cpio package.rpm |cpio -ivd path_without_leading_slash/filename
e.g.
[~]# rpm2cpio logrotate-1.0-1.i386.rpm |cpio -ivd usr/man/man8/logrotate.8
usr/man/man8/logrotate.8
14 blocks
[~]#
If you want to extract all files, just end with cpio -ivd and
leave out the filename.
If you only want to look what's in the package, use rpm -qlp ;-)
or:
rpm2cpio package.rpm | cpio -tv
Search and Replace a string on all files in a directory
perl -p -i -e 's/oldstring/newstring/g' *
You can also do this recursively on all files in the directory
and the subdirectories beneath it with:
perl -p -i -e 's/oldstring/newstring/g' `find ./ -name *.html`
Wake a machine through a wake-on-LAN packet
perl -MNet::Wake -e 'Net::Wake::by_udp(undef,"00:13:72:15:b8:87" ) '
Of course you need the perl Module Net::Wake installed.
If you don't have it, install it from CPAN with:
perl -MCPAN -e 'install Net::Wake'
print a file without configuring the printer
netcat 192.168.10.10 9100 < file.ps
Netcat connects to the printer (in this example with ip-address 192.168.10.10) on
port 9100 and then just forwards the postscript file over that connection.
This trick should work with most HP Jetdirect printers.
save a (video) stream to disk
mplayer -dumpstream -dumpfile whatever.rm rtsp://url-to-stream
You can have a look with another instance of mplayer while the
first one is still downloading:
mplayer whatever.rm
An easy utility to find out de stream URL's (they are often hidden inside
a lot of HTML and javascript) is:
streamsniff
just run streamsniff as root while connecting with your browser to the video
stream.
create a simple backup
dd if=/dev/hda1 | gzip -c | split -b 2000m - /somewhere/backup.img.gz.
We read the raw disk device (/dev/hda1) and pipe it through gzip for compression.
split takes care of limiting the output files to 2GB (just to keep things manageble).
restore with:
cat /somewhere/backup.img.gz.* | gzip -dc | dd of=/dev/hda1
process queued mail from the sendmail directory /var/spool/clientmqueue
/usr/lib/sendmail -L sm-msp-queue -Ac -v -q
Sometimes you find out that sendmail is not processing your sent email and
everything is still sitting in de client mailqueue directory. With the above
command you can then send out all pending email in one go.
last edit: 20071209
back to: www.oiepoie.nl