Powershell Quirks

Calling functions: Parantheses or not?

When calling powershell functions, do not use parantheses and commas:

function Add($x, $y) {
    return $x + $y
}

$result = AddNumbers (5, 10) # Wrong! This passes the array (5,10) as the first argument
$result = AddNumbers 5 10 # Right

Source & more info: http://ss64.com/ps/syntax-functions.html

However, when calling methods of .NET objects, you should provide parantheses and commas for function calls.

$now = Get-Date # Note this is a System.DateTime object
$inAYear = $now.AddYears(1)

Writing output and returning stuff from functions is (mostly) the same

You can consider functions as part of your pipe and filter mechanism. Not only what you return using the return statement is returned, but also everything output with echo or Write-Output. Here are some examples of what is and what is not passed as output of a function.

Function A() {
    'a' # passed as output
}

Function B() {
    return 'b' # passed as output
}

Function C() {
    echo 'c' # passed as output
}

Function D() {
    Write-Output 'd' # passed as output
}

Function E() {
    Write-Host 'e'
}

Function F() {
    Write-Verbose 'f'
}

$out = "
A: '{0}'
B: '{1}'
C: '{2}'
D: '{3}'
E: '{4}'
F: '{5}'
" -f (A), (B), (C), (D), (E), (F)
Write-Output $out

The result of execution is:

A: 'a'
B: 'b'
C: 'c'
D: 'd'
E: ''
F: ''

Scoping… Global? Local?

Using a global variable inside a function introduces a local copy of that variable and does not update the global var by default:

#definitions
$someglobal = 42</code>

Function IncrementAndPrint() {
    Write-Host "IncrementAndPrint before inc: $someglobal"
    $someglobal++ # Writing to a global introduces a local copy of the global var
    Write-Host "IncrementAndPrint after inc: $someglobal"
}

# Script
Write-Host "Script before: $someglobal"
IncrementAndPrint
Write-Host "Script after: $someglobal"

The output is:

Script before: 42
IncrementAndPrint before inc: 42
IncrementAndPrint after inc: 43
Script after: 42

To really a global you can change function IncrementAndPrint as follows:

Function IncrementAndPrint() {
    Write-Host "IncrementAndPrint before inc: $someglobal"
    $global:someglobal++
    Write-Host "IncrementAndPrint after inc: $someglobal"
}

The result is now:

Script before: 42
IncrementAndPrint before inc: 42
IncrementAndPrint after inc: 43
Script after: 43

Invoking external commands

A good resource on invoking external commands can be found here:
http://edgylogic.com/blog/powershell-and-external-commands-done-right/

Wednesday, October 22nd, 2014 Uncategorized No Comments

Editing files remotely via sshfs

When accessing a remote host via sshfs there are two common problems when trying to edit files.

  1. UIDs/GIDs differ between your machine and the remote host. So permissions on the mounted filesystem are wrong.
  2. Files cannot be updated (e.g. saved from editors) for no apparent reason.

To address the first problem you can use the uid=… and gid=… options.
The second problem can be addressed with the workaround=rename option via sshfs.

To mount a remote location you can use the following command line.

sshfs -o uid=<local uid>,gid=<local gid>,workaround=rename <remoteuser>@<remotehost>:<path> <mountpoint>

For example if a user has uid 1000 and gid 1000 and wants to mount the /home/me directory on the remote server “myserver” to the local directory ~/mnt/me he could use the following command:

sshfs -o uid=1000,gid=1000,workaround=rename me@myserver:/home/me ~/mnt/me

Now files should have the correct permissions and saving (overwriting) files should work.

Tuesday, July 16th, 2013 Uncategorized No Comments

Switching GTK themes (widgets and icons)

The application lxappearance from XFCE is a useful tool to change icon and widget themes for your favourite window manager.

For me this tool was especially useful to regain icons in my file manager (e.g. thunar) when using a minimal window manager (e.g. awesome). Just run lxappearance and apply a theme, the items should now appear when you start your file manager.

Saturday, April 6th, 2013 Uncategorized No Comments

Ubuntu on ASUS EEE PC 1025CE

The ASUS 1025CE EEE PC has a cedarview Atom processor (N2800), 1 GB RAM, and the Intel GMA 3650 graphic chip.

Currently, the graphics chip is not well supported in linux. However it is possible to have a working system including smooth playback of 720p videos.

The following method worked for me:

  1. Install Xubuntu 12.04. I tried 12.10 first, but it didn’t work out for me despite the fact that it includes the newer gma500_gfx driver
  2. Boot your OS and fully update it. You may have to update twice. When update is done, reboot.
  3. sudo add-apt-repository ppa:sarvatt/cedarview
  4. sudo apt-get install add-apt-key
  5. sudo apt-get install cedarview-drm libva-cedarview-vaapi-driver cedarview-graphics-drivers
  6. sudo apt-get install gdm # The default login manager lightdm, didn’t start on my system
  7. sudo dpkg-reconfigure gdm # pick gdm as default
  8. Reboot your system.

On the 1025CE I tested audio, wifi, graphics including video acceleration, and suspend functionality.

Linux is a lot lighter on the RAM (~600MB free) than windows (~100MB free). This results in a smoother experience and more possibilities to run demanding apps.

Reference: http://askubuntu.com/questions/168986/cedar-view-drivers-arent-working

Friday, April 5th, 2013 Uncategorized No Comments

Citrix receiver in Linux Ubuntu 12.04 or 12.10

When you do the default install of the Citrix receiver 11 in Ubuntu 12.04 or 12.10, it will not run by default. When opening your remote desktop (.ica file) from Firefox, it will fail silently. When trying from command line, you will get an error about loading shared library libXaw.so.7. An easy way to remedy this is to do a
sudo apt-get install ia32-libs

Another problem you may encounter is a missing certificate. We encountered the missing Thawte certificate. Where to get the certificate exactly is up to you (since there are potentially so many different ones) but it must be put in the /keystore/cacerts folder.

Friday, April 5th, 2013 Uncategorized No Comments

Copying some files while preserving directory structure

To copy some select files (e.g. a/b/test.txt and a/c/test2.txt) while preserving the directory structure (a/b and a/c) you can use the following:

rsync -Rv a/b/test.txt a/c/test2.txt mybackupdir

With this command you end up having the following files:

mybackupdir/a/b/test.txt
mybackupdir/a/c/test2.txt

Via: Stackoverflow

Tuesday, November 13th, 2012 Uncategorized No Comments

Using cscope and cctree to analyze your C source code

Suppose you have your source in /my/sourcedir.

The simplest way to get a cscope database:
cd /my/sourcedir
cscope -R

This will create a cscope.out file which cscope can use as a database.

A more complicated but sometimes more useful way to create a cscope database:

  • Create a file called cscope.files in some directory (say /my/cscopedir) and put in the (preferably absolute paths of the files you want to be scanned by cscope, probably using find to automatically add files with a certain extension).
  • Do the following:

cd /my cscopedir
cscope -b

You can add a -q option to cscope to make a reverse lookup list which makes certain lookups a lot faster.

Tuesday, October 2nd, 2012 Uncategorized No Comments

Set up a virtual network in VMware ESX

Set up a virtual network in VMware ESXi

The following steps show how to set up a virtual network environment for testing. With this method, (v)network adapters of (v)machines are plugged into (v)networks. The nodes inside the network are all virtual machines and need to be configured properly.

About ESXi

In this way a set of networks can be defined. To make things a bit more clear here is a bit more explanation about ESXi:

  1. ESXi describes an infrastructure of the network and virtual machines inside the network.
  2. Networks can be seen as physically interconnected network (e.g. some switches connected to each other, providing ports to plug in network cards)
  3. A network card is plugged into a single network.
  4. Virtual machines can have 0..* network cards. (machines with more than 1 card are typically routers)

Define networks

In OS:

  1. Download VMWare ESXi iso file.

In VMWare player:

  1. Create new VM (select ISO file)
  2. Install ESXi. Make sure to use large enough disk size (default 40 GB) to hold VMs plugged into the virtual network.
  3. Boot ESXi

In Browser:

  1. Visit the “manage” url displayed by ESXi and download the vSphere Client

In OS:

  1. Install vSphere client
  2. Start vSphere Client

In vSphere Client (add some networks):

  1. Click Home → Click Inventory → On left, click your ESXi IP → Click Configuration tab → Click Networking
  2. Click Add Networking…
  3. Select Virtual Machine → Click Next → Select Create a vSphere standard switch → Click Next
  4. Type a a name for your network (e.g. network1)
  5. Click Next → Click Finish
  6. Add more networks to your liking

We have now defined a set of networks. We can now define virtual machines with network cards that can plug into these networks. Now you can either create or import existing Virtual Machines to your virtual network(s).

Creating virtual machines

In vSphere Client (home)

  1. Click Home → Click Inventory → On left, click your ESXi IP → Click Virtual Machines tab
  2. Right click on the white area → Click New virtual machine…

From here you can create a new virtual machine on the ESXi data store (this is the “disk” on your ESXi machine). Note that during network configuration you can select which network card is connected to which network. Now we have a VM plugged. However, before using it we need to install an OS.

In vSphere Client (home) we first need to upload the installation files (e.g. iso)

  1. Click Home → Click Inventory → Click Configuration tab → Click Storage
  2. Right click your data store → Click Browse datastore…
  3. Navigate to the location you want to upload to
  4. Click the upload button in the toolbar (disk with up arrow) → Click Upload file and select the file you want to upload

In vSphere Client

  1. Click Home → Click Inventory → On left, click your ESXi IP → Click Vritual Machines tab
  2. Right click your VM → Click Edit settings
  3. Click CD drive → Select datastore ISO file → Check Connect at power on → Click OK
  4. Right click your VM → Click Power → Click Power on
  5. Right click your VM → Click Open console

In the console you can control your VM as you like. (normal machine, router/gateway)

Using existing (player) virtual machines in ESXi

If you need to impport existing virtual machines in your test setup you can do so as follows. The following shows an example how to import an existing VMware player image into your VMware ESXi server. In Browser:

  1. Download VMWare Converter Standalone

In OS:

  1. Install the VMWare Converter

In VMWare Converter

  1. Click Convert machine → Select your source type VMware Workstation or other VMware virtual machine → Select your source Virtual machine file → Click Next
  2. Select destination type VMware Infrastructure virtual machine → Enter server ip or name,user name and password → Click next
  3. Enter a virtual machine name → Click next → Click next
  4. Customize hardware options if required → Click next → Click finish

After this wizard, the virtual machine file is automatically transferred to and registered in your ESXi server. We can now plug the imported VM into our virtual networks.

In vSphere Client

  1. Click Home → Click Inventory → On left, click your ESXi IP → Click Virtual Machines tab
  2. Right click on your VM → Click Edit settings
  3. Click the network adapter you want to plug
  4. In the Network label section select the network you want to plug the adapter into

Repeat the above instructions for all network cards and click ok. Now we are ready to boot and control the new VM.

In vSphere Client

  1. Right click your VM → Click Power → Click Power on
  2. Right click your VM → Click Open console

APPENDIX: Setting up VM to use as a router

Alpine linux is a linux distribution which allows quick configuration of a router or simple pingable node for testing. This distribution can be run from an ISO file and doesn’t need to be installed.

A machine with multiple network cards can reach other machines in the networks it is directly plugged into. For example a machine with two network cards (ip 101.0.0.1) is plugged into 101.0.0.0/8 and 102.0.0.0/8 networks. The machine cannot directly connect to nodes in the 103.0.0.0/8 network. However, maybe there is another machine (a gateway) which can forward the data to the correct machine. Say this gateway is 101.0.0.254.

The same can be done with IPv6, but instead of addresses such as 101.0.0.1 we can use 101::1 as IPv6 address.

Configuring our machine

Now the only thing our machine needs to know is that if it wants to reach the 103.0.0.0/8 network, gateway is 102.0.0.254, the gateway will be able to forward data to the final destination. To do this add the following on the machine:

Linux


ip route add 103/8 via 102.0.0.254

ip -6 route add 103::/64 via 102::254

OpenBSD

route add -net 103.0.0.0/8 102.0.0.254
route add -net 103.0.0.0 -netmask 255.0.0.0 102.0.0.254           # IPv4 alternative notation
route add -inet6 103:: -prefixlen 64 102::254

The above is a custom routing table entry specifying explicitly which gateway to use to reach a specific network. After adding it it can access the 101.0.0.0/8 network (directly accessible), the 102.0.0./8 network (directly accessible), and the 103.0.0.0/8 network (via the specified gateway).

The machine will attempt to access any other networks which are not defined explicitly via the default gateway (say 101.0.0.253). This default gateway is often a router that allows you to access your ISP to allow internet communication. The default gateway can now be be set.

In Linux this is done as follows:


route add default gw 101.0.0.254

route -A inet6 add default gw 101::254

In OpenBSD this is done as follows:


route add default 101.0.0.254

Configuring a gateway/router

If we use an alpine linux router configuration, we can set it up as follows:

  1. Mount the alpine linux iso file
  2. Make sure that your virtual machine has two network cards, eth0 plugged into 101.0.0.0/8 and eth1 plugged into 102.0.0.0/8

IPv4 configuration

Now we can configure the network interfaces of the router using:

ifconfig eth0 up 102.0.0.254
ifconfig eth1 up 103.0.0.254

Now we must make sure that the router will forward network traffic between networks:

 echo 1 > /proc/sys/net/ipv4/ip_forward 

IPv6 configuration

 
modprobe ipv6
ifconfig eth0 up
ifconfig eth1 up
ip -6 addr add 102::254/64 dev eth0
ip -6 addr add 103::254/64 dev eth1
echo 1 > /proc/sys/net/ipv6/conf/all/forwarding

Configuring the PingTest machine

With the setup in the previous sections, ping requests should arrive at the PingTest machine. This can be verified by using tcpdump (see section below). However, the ping will still fail, because the PingTest machine does not know how to send data back to the 102/8 network. To to this, we need to set up the correct routing information:

modprobe ipv6
ifconfig eth0 up 103.0.0.1
ip -6 addr add 103::1/64 dev eth0
ip route add 101/8 via 103.0.0.254
ip -6 route add 101::/64 via 103::254

Inspecting traffic and troubleshoot problems

Sometimes setting up such a setup will not work at once, but by using the following commands you can inspect which traffic arrives at certain machines.

apk add tcpdump
tcpdump -n icmp  # For checking IPv4
tcpdump -n icmp6 # Fox checking IPv6

Setting up a DHCP server to hand out IP addresses

If you need to obtain an IP via DHCP you can install and set up a DHCP server.

setup-alpine # Needed for the dhcp startup script on alpine. This will re-prompt you some questions about your network configuration, but you can answer all questions except IP configuration with the default settings
apk add dhcp
apk add vim   # Editor, you can also use nano instead
vim /etc/conf.d/dhcpd

Specify the interface on which the DHCP server should listen

DHCPD_IFACE = "eth0"

Now we edit the server configuration file

vim /etc/dhcp/dhcpd.conf

A configuration with a single subnet declaration is sufficient

subnet 102.0.0.0 netmask 255.0.0.0 {
range 102.0.0.2 102.0.0.20;
}

This configuration will hand out IP addresses between 102.0.0.2 and 102.0.0.20.

Sunday, June 17th, 2012 Uncategorized No Comments

Using gnome network manager when using window manager instead of desktop environment

When using a window manager such as awesomewm instead of a full-fledged desktop environment such as gnome, you may have problems connecting to wireless networks.

To use the gnome network manager in your environment, add the following command line to your startup applications:

nm-applet --sm-disable

Via:

Sunday, June 17th, 2012 Uncategorized No Comments

Overview of CSS compatibility per browser

Whenever you do some cross-browser CSS development, this overview page on CSS feature compatibility per browser is quite handy:

http://www.quirksmode.org/css/contents.html

Wednesday, June 6th, 2012 Uncategorized No Comments