Smokes your problems, coughs fresh air.

Category: Technology (Page 19 of 47)

Bash script template

#!/bin/bash
#
# Author: Wiebe Cazemier (wiebe@halfgaar.net)
#
# Template bash script, for when you need something overengineerd :)
 
# Hack prevention
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
 
# Error codes
wrong_params=5
interrupted=99
default_error=1
 
# Function to echo in color. Don't supply color for normal color.
echo_color()
{
  message="$1"
  color="$2"
 
  red_begin="\033[01;31m"
  green_begin="\033[01;32m"
  yellow_begin="\033[01;33m"
  color_end="\033[00m"
 
  # Set color to normal when there is no color
  [ ! "$color" ] && color_begin="$color_end"
 
  if [ "$color" == "red" ]; then
    color_begin="$red_begin"
  fi
 
  if [ "$color" == "green" ]; then
    color_begin="$green_begin"
  fi
 
  if [ "$color" == "yellow" ]; then
    color_begin="$yellow_begin"
  fi
 
  echo -e "${color_begin}${message}${color_end}"
}
 
end()
{
  message="$1"
  exit_status="$2"
 
  if [ -z "$exit_status" ]; then
    exit_status="0"
  fi
 
  if [ ! "$exit_status" -eq "0" ]; then
    echo_color "$message" "red"
  else
    echo_color "$message" "green"
  fi
 
  if [ "$exit_status" -eq "$wrong_params" ]; then
    dohelp
  fi
 
  exit $exit_status
}
 
# Define function to call when SIGTERM is received
trap "end 'Interrupted' $interrupted" 1 2 3 15
 
dohelp()
{
  echo ""
  echo "Example script"
  echo ""
  echo "help = todo"
 
  # Exit because you don't want the script to do anything after displaying help
  exit 
}
 
 
while [ -n "$*" ]; do
  flag=$1
  value=$2
 
  case "$flag" in
    "--option1")
      option1=$value
      shift
    ;;
    "--help")
      dohelp
    ;;
    "--")
      break
    ;;
    *)
      end "unknown option $flag. Type --help" "$wrong_params"
    ;;
  esac
 
  shift
done
 
if [ -z "$option1" ]; then
  end "option1 not given" $wrong_params
fi

Bash parameter parsing

Here is a code snippet I use for parameter parsing:

dohelp()
{
  "Example script"
  ""
 
  # Exit because you don't want the script to do anything after
  # displaying help, and do so with error, so that calling scripts won't think it succeeded
  1
}
 
  [ -n "$*" ];
  flag="$1"
  value="$2"
 
  "$flag"
    "--one")
      one="$value"
     
    ;;
    "--two")
      two="$value"
     
    ;;
    "--pretend")
      pretend=true
    ;;
    "--help")
      dohelp
    ;;
    "--")
     
    ;;
    *)
      -e "unknown option $flag\n"
      dohelp
    ;;
 
 

Allowing apache to set Nagios cmd file

On debian, to prevent:

Error: Could not stat() command file ‘/var/lib/nagios3/rw/nagios.cmd’!

Do:

/etc/init.d/nagios3 stop
dpkg-statoverride --update --add nagios www-data 2710 /var/lib/nagios3/rw
dpkg-statoverride --update --add nagios nagios 751 /var/lib/nagios3
/etc/init.d/nagios3 start

source.

Effective CLI habits

Just an example of some effective CLI magic that I copy/pasted into a draft aboutexactly a year ago. Can you see what’s happening? I’m moving some selected files into a subdirectory.

$ ls *png
boucoule-17jaar-met-steen.png         evening_cloud.png  small-map-molenweg.png  tile11.png
boucoule-2001-2002-face5-400x300.png  hardwood-logo.png  step-01.png             tile9a.png
$ ls *png|while read f; do echo $f; done
boucoule-17jaar-met-steen.png
boucoule-2001-2002-face5-400x300.png
evening_cloud.png
hardwood-logo.png
small-map-molenweg.png
step-01.png
tile11.png
tile9a.png
$ ls *png|while read f; do svn mv $f index; done
A         index/boucoule-17jaar-met-steen.png
D         boucoule-17jaar-met-steen.png
A         index/boucoule-2001-2002-face5-400x300.png
D         boucoule-2001-2002-face5-400x300.png
A         index/evening_cloud.png
D         evening_cloud.png
A         index/hardwood-logo.png
D         hardwood-logo.png
A         index/small-map-molenweg.png
D         small-map-molenweg.png
A         index/step-01.png
D         step-01.png
A         index/tile11.png
D         tile11.png
A         index/tile9a.png
D         tile9a.png

Bonus points if you notice that I could have moved the JPEGs and PNGs in one command instead of doing the same thing for the second time for the JPEGs as below. (I probably forgot that I also had some JPEGs lying around, or there must have been some other lame excuse.)

$ ls *jpg
bruggetje-225x300.jpg  favicon.jpg  purple-rowan.jpg        rowan-2007.jpg                rowan-wilderness.jpg
bruggetje.jpg          hekje.jpg    rowan-2007-448x300.jpg  rowan-wilderness-400x300.jpg
$ ls *jpg|grep -v favi
bruggetje-225x300.jpg
bruggetje.jpg
hekje.jpg
purple-rowan.jpg
rowan-2007-448x300.jpg
rowan-2007.jpg
rowan-wilderness-400x300.jpg
rowan-wilderness.jpg
$ ls *jpg|grep -v favi|while read f; do svn mv $f index; done
A         index/bruggetje-225x300.jpg
D         bruggetje-225x300.jpg
A         index/bruggetje.jpg
D         bruggetje.jpg
A         index/hekje.jpg
D         hekje.jpg
A         index/purple-rowan.jpg
D         purple-rowan.jpg
A         index/rowan-2007-448x300.jpg
D         rowan-2007-448x300.jpg
A         index/rowan-2007.jpg
D         rowan-2007.jpg
A         index/rowan-wilderness-400x300.jpg
D         rowan-wilderness-400x300.jpg
A         index/rowan-wilderness.jpg
D         rowan-wilderness.jpg

Posting to WordPress via the command-line

In February I was interested in posting to WordPress from the command-line, a possibility that I enjoyed when I wanted to apply some CLI-magic to some of my MediaWiki installations in the past.

I came across a great Perl module (WordPress::CLI) by Leo Charre. It depends on WordPress::XMLRPC

Another approach is to use the appfs FUSE filesystem, which uses WordPress’ support for the Atom Publishing Protocol. There’s another FUSE filesystem, BlogFS. This one depends on WordPress’ XML-RPC instead of its Atom interface.

Setting up pptpd and pptp for a VPN

source and source and source. I’m keeping it as simple as possible. The serverside LAN in this example is 10.50.0.0/16. On the server, install pptpd. Then edit /etc/pptpd.conf and set:
# This is the IP the server will have from the clients perspective. SHould be the servers local IP.
localip 10.50.0.1
# And from this range, the client IPs will be given. Here, the range 10.50.91.x is reserved for VPN hosts.
remoteip 10.50.91.1-254
Then edit /etc/ppp/pptpd-options and set options (comments have been removed from this example):
name my-pptpd
refuse-pap
refuse-chap
refuse-mschap
require-mschap-v2
require-mppe-128
proxyarp
nodefaultroute
lock
nobsdcomp
Then restart pptpd. Then edit accounts in /etc/ppp/chap-secrets. Example:
user            my-pptpd   password                       *
On the client, /etc/options.pptp (comments once again removed):
lock
noauth
refuse-pap
refuse-eap
refuse-chap
refuse-mschap
nobsdcomp
nodeflate
/etc/ppp/chap-secrets:
user      my-pptpd    password        *
Then make /etc/ppp/peers/johnsvpn:
pty "pptp hostname --nolaunchpppd"
name user
remotename my-pptpd
require-mppe-128
file /etc/ppp/options.pptp
ipparam johnsvpn
You should then be able to turn it on with “pon johnsvpn”. Use poff to turn it off. To be able to access the entire LAN from the client, run this:
route add -net 10.50.0.0 netmask 255.255.0.0 dev ppp0
More is necessary, like permanent host-to-LAN config (with route pushing or something), DNS, testing if windows works, etc. More is to come.

Making a Compaq Deskpro sff boot without keyboard

To boot a compaq Deskpro sff without keyboard, you need to configure the BIOS in a special way. I found this:
– Run BIOS setup by pressing F10 – Goto ‘Security’ and ‘Set Power On Password’ – Type in a password and F10 to accept the change – As soon as password is set, ‘Password Options’ will appear under ‘Security’ tab – Enable ‘Network Server’ mode in there – F10 to save changes and exit When booting up it won’t ask you for F1 anymore. However, if you or someone else plugs in a keyboard it will ask for a power-on password.

Disabling exim port 25 listening when zimbra is installed

Edit: This doesn’t work anymore I don’t think, because more modern versions check for SMTP conflicts and don’t allow this. When you’re installing zimbra in an Ubuntu or Debian machine, it seems it installs the MTA in such a way that command line tools like mail and such don’t work. But when you install exim, it conflicts with the postfix in Zimbra. To fix it, you can install exim4, but configure this line in /etc/default/exim4:
QUEUERUNNER='queueonly'

Taking control of the wpautop filter

WordPress does automatic paragraph formatting using the wpautop filter, some PHP code originally developed by Matt Mullenweg. For most of the time that this blog has existed, I’ve disabled the wpautop filter using the following two lines in my theme’s functions.php file:

remove_filter('the_content', 'wpautop');
remove_filter('the_excerpt', 'wpautop');

I’m not the only one to do this. But, I’m tired of having to manually type <p>s for every paragraph in every post that I write.

I’d like to be able have it back on, but preferably a bit smarter so that you don’t get all that crap (also common on bulletin boards with empty paragraphs around undetected block-level elements, especially if these are non-standard, related to plugins.

At the very least I want to be able to turn it off for when it does annoy me, on these moments that I don’t want auto-anything. Also, I need this if I don’t want to break the nearly 300 posts that I formatted manually. So, I want to use a custom field to turn the filter on or off.

I found an unpublished plugin (unpublished in the sense that the source isn’t hosted somewhere proper such as wordpress.org/extend/plugins/) which does some of what I want in a somewhat messy unmaintained manner. After years of just entering those damn <p>s (and over a year since this draft was in the making), I decided to do my own plugin for post-by-post (and global) control of the wpautop filter. It’s called wpautop-control:

<?php
/*
Plugin Name: wpautop-control
Plugin URI: http://blog.bigsmoke.us/tag/wpautop-control/
Description: This plugin allows you fine control of when and when not to enable the wpautop filter on posts.
Author: Rowan Rodrik van der Molen
Author URI: http://blog.bigsmoke.us/
Version: 1.0
*/
 
if ( is_admin() ) {
  add_action('admin_menu', 'wpautop_control_menu');
  add_action('admin_init', 'wpautop_control_settings');
 
  function wpautop_control_menu() {
    add_submenu_page('options-general.php', 'wpautop-control', 'wpautop control', 'manage_options', 'wpautop-control-menu', 'wpautop_control_options');
  }
 
  function wpautop_control_options() {
    if (!current_user_can('manage_options'))  {
      wp_die( __('You do not have sufficient permissions to access this page.') );
    }
 
  ?>
  <div class="wrap">
    <h2>wpautop control options</h2>
 
    <form method="post" action="options.php">
      <?php settings_fields('wpautop-control') ?>
      <table class="form-table">
        <tr valign="top">
          <th scope="row">wpautop filter on by default?</th>
          <td>
            <label><input type="radio" name="wpautop_on_by_default" value="1" <?php if ( get_option('wpautop_on_by_default') == '1' ) echo 'checked="1"' ?>> yes</label>
            <label><input type="radio" name="wpautop_on_by_default" value="0" <?php if ( get_option('wpautop_on_by_default') == '0' ) echo 'checked="1"' ?>> no</label>
          </td>
      </table>
 
      <p class="submit">
      <input type="submit" class="button-primary" value="Save Changes" />
      </p>
    </form>
  </div>
  <?php
  }
 
  function wpautop_control_settings() {
    register_setting('wpautop-control', 'wpautop_on_by_default', 'intval');
  }
}
else { // ! is_admin()
  add_filter('the_content', 'wpautop_control_filter', 9);
 
  function wpautop_control_filter($content) {
    global $post;
 
    // Get the keys and values of the custom fields:
    $post_wpautop_value = get_post_meta($post->ID, 'wpautop', true);
 
    $default_wpautop_value = get_option('wpautop_on_by_default');
 
    $remove_filter = false;
    if ( empty($post_wpautop_value) )
      $remove_filter = ! $default_wpautop_value;
    elseif ($post_wpautop_value == 'true')
      $remove_filter = false;
    elseif ($post_wpautop_value == 'false')
      $remove_filter = true;
 
    if ( $remove_filter ) {
      remove_filter('the_content', 'wpautop');
      remove_filter('the_excerpt', 'wpautop');
    }
 
    return $content;
  }
}
 
?>

(I’ve requested the plugin to be added to the WordPress plugin repository, so that it won’t have to be reinvented another 20 times in the next year or so.) 😉

After installing the plugin, you can choose whether to enable or disable wpautop by default. Then, for every post where you want to deviate from the default, you can set the wpautop custom field to ‘true’ or ‘false’.

I want the new default to be to enable the filter, but since all my old posts have been manually formatted, I want all these to have the wpautop field added and set to ‘false’.

Adding the appropriate custom field values to all existing posts is easy thanks to MySQL’s INSERT … SELECT syntax:

INSERT INTO wp_postmeta (post_id, meta_key, meta_value)
     SELECT wp_posts.ID, 'wpautop', 'false'
       FROM wp_posts
      WHERE post_type = 'post';
« Older posts Newer posts »

© 2024 BigSmoke

Theme by Anders NorenUp ↑