September 28, 2009

gawk command to telnet into a tcp server and issue a command

#!/usr/bin/gawk -f
BEGIN{
cmd="/inet/tcp/0/127.0.0.1/1800";
printf("showconfig\n") |& cmd;
printf("bye\n") |& cmd;
while ( (cmd |& getline) >0){
print $0;
}
close(cmd);
}

September 14, 2009

awk binary editing

Gawk is powerful. Learn it when you are young!

To use awk to edit a binary file, you can simply do this:

gawk -F "" 'BEGIN{RS="/nuclear power is good/"}{printf("%s",substr($0,1,5) "\x81" substr($0,7))}' 1.out > aa

-F"" tells awk to treat every character as a field
RS="/nuclear power is good/" tells awk to use this string as the row/record separator. Since this string does not exist in your binary file (if by a weird chance it does, change it to another random string), awk will treat all file as one single record.

Now you have the entire binary file as a string in $0, just use your string functions to do substitutions and changes. In the example above, I changed the character at offset 6 to hex 0x81.

Is this wonderful?

September 10, 2009

awk script for processing comma seperated files

This script remove leading and trailing spaces of each line,
and the field separator is any amount of space, tab mixed with one comma (of course you can also use = or other symbols).

#!/usr/bin/awk -f

BEGIN{
FS=" *, *"
}
{
gsub(/^[ \t]+|[ \t]+$/, "");
printf("|%s|\n",$1);
printf("|%s|\n",$2);
print "===="
}

August 31, 2009

unbrick TEW-652BRP router

TEW-652BRP is one of the cheapest 11N wireless routers (availabe around $30). It can be flashed with Dlink DIR-615 firmware and openwrt firmware.

The bootloader is u-boot, and it has a built-in web server for firmware upgrade. To unbrick/upgrade, following these steps:

1. unplug power
2. hold reset button using a pin
3. plug power back while holding the reset button
4. wait around 20-30s.
5. You should see the STATUS LED (the one next to power) blink slowly. This is the indication that the device is in u-boot web server mode
6. Set your PC IP address to 192.168.0.2. The router IP address is 192.168.0.1
7. Browse to 192.168.0.1, upload the firmware. The firmware is required to use dlink hardware ID (ending with .2)
8. Wait until it is 100% finished (indicated on the screen), and then wait another 30s, and repower the router (it does NOT reboot automatically).
9. That's it. The default Dlink firmware IP address is 192.168.10.1

August 25, 2009

trace inter-process communication

"strace" and "lsof" are two very powerful tools to debug running processes in a Linux system.

Use "lsof -p PID" to find out the socket/fifos a process is using
Use "lsof -p PID -Pn" to have lsof show ip and port number instead of names

Use [ strace -p 24846 -ewrite,read -s 1024 -xx 2>&1 | grep -E "^write\(7|^read\(6" ] to trace the real-time read/write of the process

August 14, 2009

Set the sort order of file open dialog

Set the order you wish to see using Windows Explorer, hold Ctrl and press
the Close button on the window. The sorting order for the Open/Save dialog
should be changed :-)

I wish this is documented somewhere by the guys in Redmond.

August 5, 2009

ASUS Restore firmware /unbrick

ASUS WL-500gP V2 is a great router. One of good things is you can always unbrick it.

To unbrick it. unplug it, hold on the "restore" button, plug power, and wait for 5 seconds, the power LED should go into slow blinking. This shows that the box is in recovery mode.

Connect a PC to a LAN port, and make sure you disable all other network connections on the PC you are using to flash. This is the key!!

Set you PC Ethernet IP to 192.168.1.5/255.255.255.0. You will NOT be able to ping 192.168.1.1. That is OKAY.

Now fire up the ASUS Firmware Restoration utility (came with your box in the CD or downloadable from ASUS website in Utilties), and load the firmware.

Regardless whether your firmware is good or bad, you can always use this method to reflash the box.

DD-WRT sets the NVRAM password. I had to restore the firmware to ASUS firmware and press RESTORE button for more than 10 seconds, and reboot to erase the NVRAM password. Then I reflash my box to tomato firware.

VMWare Player hidden vmnetcfg

2/28/2003 Update:

In VMWare Player 5 you need to:
go to the vmware-directory and run in elevated cmd.exe-box

rundll32.exe vmnetui.dll VMNetUI_ShowStandalone

http://communities.vmware.com/message/2155960#2155960

Original Post:

I like VMWare player for its stability but wanted to have more control on the network configuration. For example, I wanted to configure which physical network card to map to the bridged network in vmware player. I dug around the internet but did not find any. Fortunately when scanning through the EXE files installed by VMWare Player I found what I was looking for: vmnetcfg.exe.

So simply go to the following directory in your Windows and run "vmnetcfg"

C:\Program Files\VMware\VMware Player>vmnetcfg

And you should see a window popping up where you can configured all network configurations.

I think the configurations are saved to somewhere in the registry. After changing my bridged network map, and rebooted my virtual Linux, the bridged network worked great.

You can also changed DHCP settings, NAT settings (port forwarding on both TCP and UDP), and all kinds of other interesting things. Be sure to check it out if you use vmware player.

This is the screenshot:

August 3, 2009

Change Linux Ethernet Speed and Duplex

Task: Change the speed and duplex settings

Setup eth0 negotiated speed with mii-tool
Disable autonegotiation, and force the MII to either 100baseTx-FD, 100baseTx-HD, 10baseT-FD, or 10baseT-HD:# mii-tool -F 100baseTx-HD
# mii-tool -F 10baseT-HD
Setup eth0 negotiated speed with ethtool# ethtool -s eth0 speed 100 duplex full
# ethtool -s eth0 speed 10 duplex half
To make these settings permanent you need to create a shell script and call from /etc/rc.local (Red Hat) or if you are using Debian create a script into the directory /etc/init.d/ directory and run update-rc.d command to update the script.

Read man page of mii-tool and ethtool for more information.

source: http://www.cyberciti.biz/faq/linux-change-the-speed-and-duplex-settings-of-an-ethernet-card/

July 29, 2009

enable multiple dummy ethernet interfaces in Linux

To get my dummy interfaces, just do this:
 modprobe dummy numdummies=5

July 22, 2009

How can I force a reload of an image from the server if the image already exists in the browser's cache?

Q416 How can I force a reload of an image from the server if the image already exists in the browser's cache?

By adding a different search string value after the image href. The following reloads the image from the server every 10 seconds:



<---script language="JavaScript">
function reloadImage() {
var now = new Date();
if (document.images) {
document.images.myImageName.src = 'picture.gif?' + now.getTime();
}
setTimeout('reloadImage()',10000);
}
setTimeout('reloadImage()',10000); //-->





Source: http://www.irt.org/script/416.htm

June 29, 2009

Measure TCP Throughput using netcat (nc)

netcat (nc) is installed on almost all Linux computers. So when you do not have tools such as iperf available for TCP throughput testing, try the way of doing it with netcat.

On machine A, do :

nc -v -v -l -n -p 2222 >/dev/null

For redhat or Fedora Core do this instead:

nc -v -v -l -n 2222 >/dev/null

On machine B do:
time yes|nc -v -v -n 192.168.0.8 2222 >/dev/null

Now wait 30 seconds, and hit Ctrl-C on machine B, and you shoud get something like this:
sent 87478272, rcvd 0
real 0m9.993s
user 0m2.075s
sys 0m0.939s

Now calculate the throughput
87478272 * 8 / 9.993 (the nubmer above after real)

You can use python as a calculator to do this. :-)

Now that's pretty easy, isn't it?

June 10, 2009

tcpdump "received by filter"

When you use tcpdump on Linux, and do not see all the packets you are expecting, and get something like this:

10 packets captured
100 packets received by filter
0 packets dropped by kernel

Make sure you use "tcpdump -Nn ...". This disables tcpdump name lookup, and makes tcpdump much faster/real-time. It took me some time to find this out. Hope this helps.

June 4, 2009

How to change stored Network Printer Credentials

Try running on the problematic PC (XP), from the start/run box:
rundll32.exe keymgr.dll, KRShowKeyMgr
This should open the password management console. If your saved share is present, you should be able to delete it.
The following article outlines in detail
http://www.windowsnetworking.com/kbase/WindowsTips/WindowsXP/AdminTips/Security/RemoveSavedPasswords.html

May 7, 2009

Virtualbox, Ubuntu, Change Screen Resolution

After installing the guest package, Ubuntu 9.04 still does not show the right screen resolution.

Then do this:

At the terminal type this

sudo dpkg-reconfigure -phigh xserver-xorg

and log out, and log back in.

Now your screen resolution is the same as your window size. Just maximize your window or go full screen to get the max screen resolution

April 21, 2009

Struct sockaddr and friends

Prototypes



include <netinet/in.h>

// All pointers to socket address structures are often cast to pointers
// to this type before use in various functions and system calls:

struct sockaddr {
unsigned short sa_family; // address family, AF_xxx
char sa_data[14]; // 14 bytes of protocol address
};


// IPv4 AF_INET sockets:

struct sockaddr_in {
short sin_family; // e.g. AF_INET, AF_INET6
unsigned short sin_port; // e.g. htons(3490)
struct in_addr sin_addr; // see struct in_addr, below
char sin_zero[8]; // zero this if you want to
};

struct in_addr {
unsigned long s_addr; // load with inet_pton()
};


April 1, 2009

vim marks

Marks

You can set marks within your documents to jump quickly between different positions of a document or even many documents.

Vim automatically sets various marks like

* {0-9} are the last 10 positions of closed files (0 the last, 1 the last but one)
* <> are the left and right position of marked texts
* ( and ) are the start or end of the current sentence
* { and } are the start or end of the current paragraph
* [ and ] are the first or last character of the last yanked or changed text
* . position of the last change
* ' or ` position before the last jump
* " position before the last exit of the file (local to a file)
* ^ position of the last insert-stop

To set a manual mark, use m{a-zA-Z} (m followed by either a,b..z or A,B,..Z), and to jump to one of the marks (manual or automatic) you can choose between ' and `

* ' ...sets the cursor to the first non-blank character in the marked line
* ` ...sets the cursor to the exact position where the mark was set

There is a little difference between lower-case and upper-case characters:

* {a-z} are local to a file
* {A-Z} are stored and available over sessions (associated with a file)

You can use L for your work-log and T for your time-table for example, and quickly update the information there.

For example you can jump to the last known position of a file before it was closed by typing `" (it’s easy to configure Vim to do it automatically at start).

To get a list of all marks Vim knows about type :marks. To delete marks use :delmarks (:delmarks a b c removes marks a and b and c, to delete all marks use :delmarks!).

More tips at http://blog.interlinked.org/tutorials/vim_tutorial.html

March 30, 2009

Fix Debian Problem: W: There is no public key available for the following key IDs

When you do an "apt-get" on Debian, you get:

W: There is no public key available for the following key IDs:
.....

To fix this problem, just run this:

sudo apt-get install debian-archive-keyring

March 20, 2009

google search tips 1

Typically google will display page with the "searched text" either in the page or in referring pages.

To only show pages with the search phrases in them, start your search with "allintext:"

Other possible web search options are:
allinanchor:,
allintext:,
allintitle:,
allinurl:,
cache:,
define:,
filetype:,
id:,
inanchor:,
info:,
intext:,
intitle:,
inurl:,
phonebook:,
related:,
site:

March 19, 2009

GNU Screen Reference

screen Quick Reference

Getting in

start a new screen session: screen
.. with session name: screen -S
attach to a running session: screen -r
.. to session with name: screen -r
the “ultimate attach”: screen -dRR (Attaches to a screen session. If the session is attached elsewhere, detaches that other display. If no session exists, creates one. If multiple sessions exist, uses the first one.)

Escape key

All screen commands are prefixed by an escape key, by default C-a (that's Control-a, sometimes written ^A). To send a literal C-a to the programs in screen, use C-a a.

Getting out

detach: C-a d
detach and logout (quick exit): C-a D D
exit screen: exit all of the programs in screen.
force-exit screen: C-a C-\ (not recommended)

Help

See help: C-a ? (lists keybindings)

Window Management

create new window: C-a c
change to last-visited active window: C-a C-a (commonly used to flip-flop between two windows)
change to window by number: C-a (only for windows 0 to 9)
change to window by number or name: C-a '
change to next window in list: C-a n or C-a
change to previous window in list: C-a p
see window list: C-a ” (allows you to select a window to change to)
show window bar C-a w (if you don't have window bar)
close current window: Close all applications in the current window (including shell)
kill current window: C-a k (not recommended)
rename current window: C-a A

Split screen

split display: C-a S
jump to next display region: C-a tab
remove current region: C-a X
remove all regions but the current one: C-a Q

Misc

redraw window: C-a C-l
enter copy mode: C-a [ (also used for viewing scrollback buffer)
paste: C-a ]
monitor window for activity: C-a M
monitor window for silence: C-a _
enter digraph: C-a C-v
lock (password protect) display: C-a x
enter screen command: C-a :