Thursday, September 3, 2020

".ps1 is not digitally signed. The script will not execute on the system."

I am forced to use M$ Windåse at work, which is not my preferred OS. Most things annoys me about it and I do not understand most things when my Winbox complains. Latest complaint from my Winbox was

.ps1 is not digitally signed. The script will not execute on the system.

trying to install Grunt via Chocolatey. The solution is to bypass the signing requirement

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

<rant>So much for security.... My Winbox is attached to an AD with all the drawbacks and not really any advantages. Admin privileges only via external "security" program, so... we pretend it is safe, but... mostly just a nuisance... god I miss Linux or OSX.... Yes i do use WSL for most work, but dang... it's slow!!!!</rant>

As usual this tip is mostly for my own sanity, so I can find this again the next time Winbox complains. Solution courtesy of Caio Moreno.

Wednesday, January 22, 2020

Runnig multipe versions of PHP on Ubuntu

Running multiple versions of PHP on Ubuntu is fairly trivial since all supported version can be found in the main PPA for supported PHP versions. The repository can be added with

$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update

Look here1 for details. Then the relevant versions can then be installed

$ sudo apt install php7.4
$ sudo apt install php5.6

It is now possible to switch to the relevant version interactively with

$ sudo update-alternatives --config php

or alternatively directly with

$ sudo update-alternatives --set php /usr/bin/php5.6

This will change the version on the command line. Change version used by Apache with2

$ sudo a2dismod php7.4
$ sudo a2enmod php5.6
$ sudo systemctl restart apache2

Extensions

Most relevant extensions can be installed directly via apt. Fx.

$ sudo apt install php-ast

This will add the extension to all installed version of PHP the extension is supported for

Extensions via PECL3

Some extension is only available via PECL. PECL comes with the dev files for PHP, also available via apt

$ sudo apt install php7.4-dev

When using multiple versions of PHP, you need to set up the dev dependencies correctly, so extensions are installed correctly. php, phpize and php-config all need to be set to use the correct PHP version

$ sudo update-alternatives --config php
$ sudo update-alternatives --config phpize
$ sudo update-alternatives --config php-config

Extension installed via PECL is only installed for the current version of PHP. You need to manually add the extension to php.ini with (ast extension used as an example)

extension=ast.so

Manually installing extensions4

If you are compiling an extension yourself, you need to setup PHP the same way as when using PECL. After downloading the source code for the extension, build and install the extension with

$ cd extname 
$ phpize 
$ ./configure 
$ make

Remember to add the extension to php.ini

References

  1. https://launchpad.net/~ondrej/+archive/ubuntu/php
  2. https://opensenselabs.com/blog/tech/change-php-version-drupal-website-apache-nginx
  3. https://www.php.net/manual/en/install.pecl.pear.php
  4. https://www.php.net/manual/en/install.pecl.phpize.php

Tuesday, February 19, 2019

Replacing text using sed on OSX

Yet another reminder post for my self.

One again is bumped into the error message

sed: 1: "test.txt": undefined label 'est.txt'

when trying to replace text in a file using sed. When using sed on OSX to replace text in files, using -i, the BSD variant of sed on OSX requires an extension parameter, where a copy of the original file saved.

The syntax is as following

sed -i '.bak' 's/before/after' file.txt

If you are sure of what you are doing you can leave the extension parameter empty and not having a backup.

sed -i '' 's/before/after' file.txt

As a reference, here is the syntax for the GNU variant, ie. Ubuntu and friends

sed -i 's/before/after' file.txt

Tuesday, December 22, 2015

Screenshots in OSX

This is mostly for my own convenience, since I always forget the screenshot key combinations for doing the various kinds of screenshots you can do in OSX.

  • Command + Shift + 3 : Take screenshot of the whole screen
  • Command + Shift + 4 : Take screenshot of a part of the screen
  • Command + Shift + 4 : Press Space and select window to take screenshot of
The command for taking a screenshot of a window also works on open menus.

Appels official documentation: https://support.apple.com/en-us/HT201361

Make Alt + left/right work in iTerm2

I recently got a new harddrive, SSD!!!! So I made the decision to wipe the slate and did a clean install. But as always not every thing worked as before, as always. One of the most annoying things was that Alt + left arrow did not jump one word to the left and vide versa for Alt + right arrow. This is OSX default behaviour, on Linux it is Ctrl + arrow keys.

Anyway, to solve this you need to change what iTerm sends when you hit the key combination. Quickest way is to change set keys shortcuts for the default profile to "xterm Defaults"
Next you need to set the correct key bindings in your .inputrc, .bashrc etc. The file depends on how you set up your system. I cannot remember what file is standard on OSX, but i have my config stuff in a .bash_profile to avoid polluting the system more the needed. So i put the following two lines in my .bash_profile

bind '"\033[1;9D": backward-word'
bind '"\033[1;9C": forward-word'

Reload your config file or open a new terminal and I have Alt + arrow keys back working.

Have a look at http://superuser.com/questions/357355/how-can-i-get-controlleft-arrow-to-go-back-one-word-in-iterm2 also.

Wednesday, August 12, 2015

Spotify web player keyboard shortcuts

It seems that the keyboard shortcuts for the Spotify web player are different based on OS and browser!!! And it looks like they tend to change a bit over time!! Thank you Spotify.

These work on OSX with Chrome (I am on a danish keyboard, not sure if this have anything to say)

Space - Play/Pause
Shift+Right arrow - Next track
Shift+Left arrow - Previous track
Shift+Ctrl+Right arrow - Forward in track approx. 15 sec
Shift+Ctrl+Right arrow - Backwards in track approx. 15 sec
Ctrl+Alt+s - Open search
Ctrl+Alt+Up arrow - Increase volume (not above system volume)
Ctrl+Alt+Down arrow - Decrease volume

Any I missed??

UPDATE

You need to have focus i the actual player for the shortcuts to work. So you need to click in the right side player for the keyboard shortcuts to work.

Tuesday, April 1, 2014

No DNS search domain for SSH after Mavericks upgrade

I often access a lot of server via SSH that all resides on the same domain. So I have set up a the as a search domain. Since I am on OSX I have do do this via the Network preference GUI. To add a search domain edit the connection, hit the DNS tab and a  the domain you want to autocomplete from. This will add the search domain to your resolve.conf, that is automatically generated on OSX. If you are on other *NIX systems, just edit your resolve.conf and add

search example.org

This will, after reload, enable you to ssh to a host under the search domain without specifying the full hostname, ie.

$ ssh root@host

will try to connect  to host.example.org.

For some reason this does not work out of the box on OSX after the Lion release. And it was again broken after I upgraded to Mavericks. Apparently the search domain is not used. Thank to Google I found this which helped my resolve the problem.

To fix the problem edit /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist and add the following key

<string>-AlwaysAppendSearchDomains</string>

to the array under ProgramArguments. After a restart of the DNS service by doing

$ sudo launchctl unload -w System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
$ sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist

SSH should complete hostnames from the search domain.

Friday, November 15, 2013

Magic screen on the wall, who is the fairest one of all?

Just wanted to share a very old article I stumbled upon a few day ago. It is a small into to GNU screen and can be found here http://www.linuxjournal.com/article/6340.

Like many other vannabe *unix natives I have being using screen on and off for different tasks, but not very efficiently. Basically I just used it to launch a process and then Ctrl-A+D to detach and get back later with screen -r. Just recently discovered that screen is a very power full tool for doing your every day work.

Now the first thing I do when opening my laptop is "still" to open Chrome but the second thing I do now is to type screen in my terminal and hence spawning windows within screen with all the stuff I need. But in order to work efficiently with screen you need to know the basics of navigating between windows, copy-paste and so forth. All this a more is covered very nicely in the article above.

Friday, September 20, 2013

Disk space not used, but not free. Where did it go...

I recently pulled out almost all of my hair, searching for lost disk space on a Ubuntu box.

"df" showed that almost all space on disk was used, but i was not able for find the files taking up all the space. Of course I went through all the usual suspects (http://ubuntuforums.org/showthread.php?t=1122670), but in the end cause was very simple and yet not obvious.
Side story; the box runs a installation of MySQL that had been mangled when trying to import a database from another box. The MySQL files had been deleted, but I forgot to shutdown the MySQL daemon. So all files used by MySQL was still held by MySQL. This led to disk space being used, but not found when searching for files. the trick to discover the used space was the following:

lsof | grep "deleted"

I know it is a obvious error, but I use a few hours to find this.So heres the tip for you.

Monday, September 9, 2013

Simple copy of file between linux boxes with netcat

I am still pretty new all this Linux sysadmin stuff, so when I get new tricks to to help me I clap my hands and quickly write it down. So here goes.

If you need to copy large amounts between instances, you might not have access to copy between the specified instances via scp. Here netcat, nc, can help you. It opens a direct connection between the two instances, so you can stream bit from one to the other.

Example

I want to send file1.txt from instance A to instance B.

On instance B do:

$ tar -cf - . | nc A_ip 4000

On instance A do:

$ nc -l 4000 | tar xvf file1.txt

If you need to send over a lot of files you can use the following on instance A:

$ nc -l 4000 | tar xvf -

If you are sending a a huge amount of data, you might need to throw gzip and gunzip in to the mix:

$ tar -cf - . | gunzip | nc A_ip 4000
$ nc -l 4000 | gzip |tar xvf file1.txt

Of course you need to make sure the ports are open for communication, but most instances have at least one open port that can be used. Just make sure the port is not use by other programs.

Note that netcat syntax differs a bit between the original unix version and the OpenBSD version. The above examples are in the OpenBSD variant.

Reference

 - http://www.commandlinefu.com/commands/view/2536/using-netcat-to-copy-files-between-servers


Friday, March 15, 2013

Sad day for people who can concentrate for more than 2 minutes

Its a sad day today. Google Reader have been terminated!

In the newest spring cleaning from Google, Google is shutting down Google Reader along side a lot of other stuff. I am not to say wether this is good business or not  as seen from Googles point of view. But from my point of view, its a sad day.

I have been using Google Reader for more than 6 years and have grown accustomed to its simplicity, easy navigation and general ease of use. These characteristics are, in my opinion  not to be found any where else in any RSS reader, hence a sad day.

The main thesis out in webland is that most people now a days use social media for keeping track of news and therefor there is no use for Google Reader/RSS anymore. That might be right for day to day news, "Elephant ate my baby" kind of stuff, but to me and a lot of others, there a re simply to much stuff I need to keep my eye on and social media is simply going by to fast and to shallow. I need a place where everything is aggregated and stays there until I have decided that it should not any more. Social media today simply does not do this very efficient today. Most social media today is like a flashing bilbard on Time Square, things flashing by to feed people in a hurry and people who cannot concentrate for more that 2 minuts at the time. My guess is that most people disagree with me on that point, but from where I am sitting this is the truth.

Enough ranting on social media for now. The bottom line is that I will be missing my faithful companion Google Reader. Hopefully I will find a new friend that will support me in my daily work and help me stay up to date.

Wednesday, June 6, 2012

Installing PCNTL for PHP on OSX Lion

Today I needed to install the PCNTL extension for PHP for a tutorial session at the Dutch PHP Conference. And since I am working on OSX Lion, this of course gave some problems due to the rather odd installation of Apache and PHP on OSX.

Several sites on the web gave quick guides for this, but still, as always there are a few things you need to keep in mind for it all to work. I will try to walk through the process as detailed as possible:

First start by installing Xcode. You can get the latest version from the App Store. NOTE that the App Store only have the latest version of Xcode, so if you need a version for Snow Leopard or older, you need to dig deep on the web (https://connect.apple.com is a good place to start).

Next you need to install the Command Line Tools from Xcode. You do this by opening Xcode, go to Preferences (CMD + ,) -> Downloads -> Components and hit install on the Command Line Tools. You can get the Command Line Tools without installing Xcode via https://connect.apple.com

Now you need to find out what version of PHP is installed on OSX

$ php -v
PHP 5.3.10 with Suhosin-Patch (cli) (built: Feb 20 2012 22:55:53) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

and then get the source code for that version

$ curl -O http://us.php.net/distributions/php-5.3.10.tar.gz

When the download have finished, you should unpack, find the PCNTL extension, phpize, configure, make and then make install

$ tar -xzvf php-5.3.10.tar.gf
$ cd php-5.3.10/ext/pcntl
$ phpize
$ ./configure
$ make
$ sudo make install

If phpize is complaining, you are probably missing autoconf. The easiest way to install autoconf is via Homebrew. You install Homebrew by doing

$ /usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"

The first thing you need to do afterwards is to run

$ brew doctor

to find out if any dependencies are missing. Homebrew will telle you what to do, if any is missing.

Lastly you need to enable the PCNTL extension. Do this by adding the following to your php.ini

extension=pcntl.so

if you do not know where your php.ini file are, you probably need to create on, by doing

sudo cp /private/etc/php.ini.default /private/etc/php.ini

Restart Apache and verify that the PCNTL extension are loaded correctly

$ sudo apachectl restart 
$ php -m | grep pcntl 
pcntl

If the PCNTL extension for some reason is not loaded, try and copy the pcntl.so from the compiled source code to the extensions directory

$ sudo cp php-5.3.10/ext/pcntl/modules/pcntl.so /usr/lib/php/extensions/no-debug-non-zts-10090626/

Restart apache and verify the the PCNTL extension is loaded.

Resources:

Tuesday, January 17, 2012

An odd error with VirtualBox on OSX 10.6

I recently updated my version of VirtualBox on my OSX 10.6 box to version 4.1.8 and it suddenly stopped working. When I tried to start on of the virtual I was given the following error message:

Failed to load VMMR0.r0 (VERR_SUPLIB_WORLD_WRITABLE).


After searching the Internet a bit I found a highly unusually solution. Remove write permission for Other!!

$ chmod o-w Applications

Why this work, I do not know but it does. If you know why, please leave a comment.

Wednesday, December 14, 2011

Installing Gearman on Ubuntu 10.04

This is a quick guide on how to install Gearman on Ubuntu 10.04 LTS.

I found numerous guides online, but none of them seems to do the complete trick for me. I know most installations are different from each other, so that is probably the reason why non of them worked on my setup. So here is my addition to the list of Gearman install guides.

  1. The first thing you need to do is to find a PPA repository with an updated version of Gearman. The one bundled in Ubuntu by default is very old. The only reasonable up to date repository I could find is

    https://launchpad.net/~gearman-developers/+archive/ppa

  2. If you do not have the add-apt-repository command installed, you have to install it

    $ sudo apt-get install python-software-properties

  3. Now you can add the repository by issuing the following command

    $ sudo add-apt-repository ppa:gearman-developers/ppa

  4. After adding the repository, you should update the package list

    $ sudo apt-get update

  5. Now you can install the latest version of Gearman

    $ sudo apt-get install gearman gearman-job-server gearman-tools libgearman4 libgearman-dev

    Please note that as of December 2011 the latest update to the repository was in May 2011 with Gearman version 0.14.
  6. You are now free to install Gearman extension to whatever language  you like. Most requirements should be covered by the above mentioned packages.
    You can start the Gearman job server by the following comand

    $ gearman -d
References

Wednesday, September 7, 2011

Sticky session on Linux

Today i stumbled upon the slides from a talk given at Confoo 2011 by Sean Coates called "Fifty Tips, Tricks and Tools ...in one talk". Most of the stuff was not new to me, but still informative. But slide 56 gave a very usefull snippet "Sticky shell start", which allows you to login and be placed where the last shell operated.

You should place the following snippit in your ~/.bashrc or ~/.profile

cd () { builtin cd "$@" ; pwd > ~/.pwd ; }
cd "`cat ~/.pwd`"  

To me this is very usefull since i often jump between different servers and often need to go to the same place every time. so this saves me a lot of typing.

Tuesday, August 16, 2011

Quick tip: Save without permissions in VIM

Like many others I use VIM for my daily work. And like many others I often forget to open files with the right permissions (i.e. sudo vim ...).

To save a file that you need special permissions for, do the following:

:w !sudo tee %

this will allow you to save the file even though you do not have the right permissions (You need to have sudo rights of course).

More tips can be found here.

Friday, March 18, 2011

Edit existing SVN commit messages

I tend to forget things easily and for that reason I often go to my trusted old friend, Google to find stuff. But at times this can be cumbersome since Google results tend to be very cluttered, especially when trying to find tech stuff.

So I took up the advice of @lornajane and write down my finding i my blog, mainly because it is easier to find it again. And "Yes", I will forget it again and I will have to find the stuff again.

Sometimes the fingers work quicker than the brain and the eyes. I my case this often shows in log messages when committing new code. So here it is. If you want to change an exsisting commit message, use the command below.

svn propset -r N --revprop svn:log "new log message" URL

N is the revision number and URL are the repository URL to the fil you changed.

NOTE you need to be administrator of the repository, in order for this to work. I mainly use Google Code for hosting code and if you are the repo owner, you are the administrator.

The above example where found here.

Friday, January 14, 2011

Navigating BASH history

Today I came across this blog post by @lornajane describing how to navigate your BASH history using Ctrl-r. I have used this functionality in the past, as it is very useful when trying to find past commands or for doing repetitive tasks with small modifications that are to small to be automated. But when working in the terminal all day, I find this approach a bit to inconvenient, i.e. I have to think to much ;-)

So I have switch to another approach which works a lot better for me. I use the up and down keys for searching back and forwards in my BASH history. I have added the following to my .bash_profile:

# make bash autocomplete with up arrow
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'

This enables me to quickly "auto complete" the commands I am typing. Although this approach has some drawbacks as well. If you have pressed the up or down key, you wont be able to further narrow your search. For me this is not a problem, since most of the time I only need to go back a few commands in the history, so a small narrowing of the suggestions are fine for my needs. But if you need to search further back in your history or not quite sure what you are actually looking for the Ctrl-r approach will probably be better for you.

Wednesday, October 6, 2010

file_get_contents acting very slow

Lately i have been working on a simple REST-like service interface to an existing product at work. I admit, I found some code online that I modified. No need to do work already done, right? ;-)

The problem arose when i tried to call my brand new web service. I got extremely long waiting time (about 15 secs to precise), before the resource was returned. The code I used look like this:

$result = file_get_contents('http://example.com/service/');

As simple as it gets. When I tried calling the service directly from my browser, the result came back instantly.

I turned to my old friend Google to look for an answer. Most of the answers i got was that the DNS lookup was slow. So i tried my script from all the different servers I have access to, but with the same result.

The solution was quite simple, when I finally found it. In my REST-like service I set the follow header:

header('HTTP/1.1 200 OK');

By doing this I force the connection to use HTTP 1.1. This is pretty standard, but apparently by doing this I use the default behaviour of HTTP 1.1 and that is to keep the connection alive. The same as setting:

header('Connection: keep-alive');

So the connection started by file_get_contents() is keept open, until it times out. Timeout is set to 15 secs on the server!! So the solution was to put the following header in my service:

header('Connection: close');

A w00p w00p the result was returned instantly.

A quick tip if you run into this problem, and the service is not your own, is to set the header yourself. You can do this by giving a context to the file_get_contents. This can be done by doing the following:

$opts = array(

'http'=>array(
header' => 'Connection: close'
)
);
$context = stream_context_create($opts);

$result = file_get_contents('http://example.com/service/', false, $context);

This will force the connection to close immediately after the resource have been retrived. Al option that can be set in a context (for HTTP) can be found here: http://php.net/manual/en/context.http.php

Monday, March 8, 2010

POST array in PHP decoded wrong??

I just stubled upon an error? in the way PHP decode the keys in the $_POST array. If you define an input field in a form with a name containing a . (period) f.x. 'test.field', then the corresponding key in the $_POST array, when posted to a PHP script, will be 'test_field'. PHP substitute the . with a _ (underscore). According the HTML spec the . is a legal character in the name as long as it starts with [a-zA-Z].
So why do PHP make this substitution? I tried to find the answer online, but without any luck. Please post a comment if you know the answer or where to find it.

Update
I found this comment on php.net http://www.php.net/manual/en/language.variables.external.php#81080 Apparently this is done for backwards compatibility with register globals.