Linux
22 October 2011 1 Comment

Expand Tabs to Spaces Recursively

If you have a bunch of files with tabs, and you want to replace these tabs with the appropriate amount of spaces then you can use the expand command.

$ expand --tabs=4 stuff.php > stuff.php

With the –tabs switch the tab width is denoted, in spaces. So in this example each tab is replaced with four spaces.

In order to run this command on all PHP files in a directory, including its subdirectories, use this script:

#!/bin/sh
#
# Iteratively replaces tabs in .php files with 4 spaces.
#
 
find . -name "*.php" | while read line
do
  expand --tabs=4 $line > $line.new
  mv $line.new $line
done

Save it to a file and execute it in the directory you want to run the expands:

// Create the file in the current directory
$ touch expand.sh
 
// Use a text-editor like 'nano' to put the script code in the file.
$ nano expand.sh
 
// Make the script executable
$ chmod +x expand.sh
 
// Run the script
$ ./expand.sh

Tags: , expand, , php, spaces, tabs
Linux
21 June 2011 1 Comment

Linux: Showing Open Ports and the Processes that Own Them

How do you list the open TCP and UDP ports on your server and the processes that own them? The answer is to use either the netstat or the lsof command:

netstat

$ sudo netstat -lptu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 *:ftp                   *:*                     LISTEN      3825/vsftpd
tcp        0      0 *:ssh                   *:*                     LISTEN      3539/sshd
tcp        0      0 localhost:6600          *:*                     LISTEN      3922/mpd
tcp        0      0 localhost:mysql         *:*                     LISTEN      30004/mysqld
tcp        0      0 *:svn                   *:*                     LISTEN      3810/svnserve
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN      3539/sshd
tcp6       0      0 [::]:microsoft-ds       [::]:*                  LISTEN      3805/smbd
tcp6       0      0 localhost:6600          [::]:*                  LISTEN      3922/mpd
tcp6       0      0 [::]:netbios-ssn        [::]:*                  LISTEN      3805/smbd
tcp6       0      0 [::]:www               

Tags: , lsof, , open ports, tcp, udp
Networking
18 June 2011 0 Comments

Viewing and Manipulating the ARP Cache on Windows and Linux

Other posts in the ARP series: The ARP protocol explained and An introduction to ARP cache poisoning.

Both Windows and Linux have a tool called arp. I tested this with Windows 7 and Linux kernel 2.6.31, but this information should be true for just about every OS version since the dawn of TCP/IP.

Command Overview

Be aware that there is one ARP cache (table) per interface, as opposed to the routing table, which is global for the system.

Windows: arp.exe

Open an (you need administrator rights). Now you can type arp to execute the Windows ARP cache manager.

Display the current ARP entries for each interface with arp -a:

[sourcecode language="text" classname="nonum"]
C:>arp -a

Interface: 232.19.232.231 — 0xb
Internet Address      Physical Address      Type
232.19.232.2          22-22-2c-27-ac-22     dynamic
232.19.232.22         22-21-f3-23-3e-23     dynamic
232.19.232.32         22-29-33-c1-c2-24     dynamic
232.19.232.91         22-2d-29-a9-33-17     dynamic
232.19.232.231        22-22-29-c2-22-b7     dynamic
232.19.232.242       …

Tags: , arp.exe, clear cache, internet address, , , , physical address,