Thursday, October 17, 2013

Notes VI

 List of Commands for vi - An Unix Editor

Complete Documentation
Undo Command
Screen Commands
Cursor Positioning Commands
Search Commands
Text Insertion Commands
Text Deletion Commands
Changing Text
Search and Replace
Cutting and Pasting Text
File Commands



Complete Documentation
The vi editor is a common editor for unix systems in that it makes use of a regular keyboard with an escape key. On the DECstation, the escape key is the F11 key. It therefore works on all unix computers. Complete documentation is available by typing

man vi  at the unix prompt.


Undo Command
u                    undo the last command.

Screen Commands
CTL/l Reprints current screen.
CTL/Y Exposes one more line at top of screen.
CTL/E Exposes one more line at bottom of screen.
CTL/F Pages forward one screen.
CTL/B Pages back one screen.
CTL/D Pages down half screen.
CTL/U Pages up half screen.
H     Go to first line on screen
M     Go to middle line on screen
L     Go to last line on screen
nH    Go to line n beneath top of screen
nL    Go to line n above bottom of screen
nG    Go to line n
:n    Go to line n


Cursor Positioning Commands
j      Moves cursor down one line, same column.
k      Moves cursor up one line, same column.
h      Moves cursor back one character.
l      Moves cursor forward one character.
RET    Moves cursor to beginning of next line.

0      Moves cursor to beginning of current line.
^      Moves cursor to beginning of first non-blank character of current line.
$      Moves cursor to end of current line.
+      Moves to next line, first non blank character
-      Moves to previous line, first non blank character
SPACE  Moves cursor forward one character.
nG     Moves cursor to beginning of line n. Default is last line of file.
0      Moves the cursor to the first character of the line.
:n     Moves cursor to beginning of line n.
b      Moves the cursor backward to the beginning of this or previous word.
B      Moves the cursor backward to the beginning of this or previous word, ignoring symbols.
e      Moves cursor to end of this or next word.
w      Moves cursor to beginning of this or next word.
E      Moves cursor to end of this or next word, ignore any symbols.
W      Moves cursor to beginning of this or next word, ignore any symbols.
(      Moves to beginning of current or previous sentence
)      Moves to end of current or next sentence
{      Moves to beginning of current or previous paragraph
}      Moves to end of current or next paragraph

Search Commands
/pattern  Moves cursor forward to next occurrence of pattern.
?pattern  Moves cursor backward to next occurrence of pattern.
/, n      Move to next search
?, N      Move to previous search
.         Search wildcard - any single character
*         Search wildcard - zero or more characters
^         Search wildcard - beginning of line
$         Search wildcard - end of line
[...]     Search wildcard - any character in bracket, [xyz] means x or y or z
[..-..]   Search wildcard - any character in range, [a-z] means anything from a to z


Text Insertion Commands
a    Appends text after cursor. Terminated by escape key.
A    Appends text at the end of the line. Terminated the escape key.
i    Inserts text before cursor. Terminated by the escape key.
I    Inserts text at the beginning of the line. Terminated by the escape key.
o    Opens new line below the current line for text insertion. Terminated by the escape key.
O    Opens new line above the current line for text insertion. Terminated by the escape key.
DEL  Overwrites last character during text insertion.
ESC  Stops text insertion. The escape key on the DECstations is the F11 key.

Text Deletion Commands
x     Deletes current character.
X     Delete character before cursor.
6x    Delete next 6 characters
4dd   Delete the next 4 lines
dd    Deletes current line.
dw    Deletes the current word.
d)    Deletes the rest of the current sentence.
d}    Deletes the rest of the current paragraph.
D, d$ Deletes from cursor to end of line.
d0    Deletes from cursor to beginning of line
3dw   Deletes next 3 words
3dl   Delete next 3 letters
P     Puts back text from the previous delete.

Changing Text
cw          Changes characters of current word until stopped with escape key.
c$          Changes text up to the end of the line.
C, cc       Changes remaining text on current line until stopped by pressing the escape key.
c^          Change text to beginning of line
c)          Change text to beginning of sentence  
c}          Change text to beginning of paragraph
3cw         Change next 3 words
~           Changes case of current character.
xp          Transposes (swap) current and following characters.
J           Joins current line with next line.
s           Deletes the current character and goes into the insertion mode.
rx          Replaces current character with x.
R           Replaces the following characters until terminated with the escape key.


Search and Replace
:s/old/new          Replaces old with new
:s/old/new/g        Replaces old with new all occurrances
:s/old/new/c        Replaces old with new - need confirmation
:11,l2 s/old/new    Replaces old with new, within range of line l1 and l2





Copy(yank) and Paste Commands
yy      Copy line to buffer. Does not delete the line from its current position.
yw      Copy (to buffer) to the end of word
y0      Copy from the beginning of line
y$, Y   Copy to the end of line
y)      Copy to the end of sentence
y}      Copy to the end of paragraph
p       Places buffer after the current position of the cursor.
P       Places buffer before the current position of the cursor.


File Commands
vi filename     where filename is the name of the file to be edited.
vi -rfilename   recover file in case of crash
vi +/blah file  open file "file" at the point in the file containing text "blah"
:R file         Inserts the file filename where the cursor was before the ``:'' was typed.
:w file         Save current file as a new file with name "file"
:l1,l2w file    Save from line 11 to line l2 in file called "file"
:.,$w file      Save from current line "." to last line "$" of file
ZZ              Exits vi and saves changes.
:wq             Writes changes to current file and quits edit session.
:q!             Quits edit session (no changes made).
:e              reverts back to last saved mode.

Notes Networking


Contents
========
inetd
socket.h
Setup Linux Network
Network VM with Virtual Box - Bridged, Internal, Host-Only


inetd
========
inetd - maintains passive sockets on a variety of these well-known ports. When a new connection is created, Inetd starts a program to handle the connection, based upon a configuration table. This way, one program can handle incoming connections for a variety of services. Inetd only runs server programs as they are needed, and will spawn multiple server programs to service multiple network connections. Inetd works best for network services with fairly long duration, so the extra startup overhead becomes negligible.

inetd services - echo, discard, daytime, chargen, time
- Usage: eg. telnet <IP> echo


socket.h
=========
#include <sys/socket.h>
int     accept(int socket, struct sockaddr *address, socklen_t *address_len);
int     bind(int socket, const struct sockaddr *address, socklen_t address_len);
int     connect(int socket, const struct sockaddr *address, socklen_t address_len);
int     getpeername(int socket, struct sockaddr *address, socklen_t *address_len);
int     getsockname(int socket, struct sockaddr *address, socklen_t *address_len);
int     getsockopt(int socket, int level, int option_name, void *option_value, socklen_t *option_len);
int     listen(int socket, int backlog);
ssize_t recv(int socket, void *buffer, size_t length, int flags);
ssize_t recvfrom(int socket, void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t *address_len);
ssize_t recvmsg(int socket, struct msghdr *message, int flags);
ssize_t send(int socket, const void *message, size_t length, int flags);
ssize_t sendmsg(int socket, const struct msghdr *message, int flags);
ssize_t sendto(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len);
int     setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len);
int     shutdown(int socket, int how);
int     socket(int domain, int type, int protocol);
int     socketpair(int domain, int type, int protocol, int socket_vector[2]);


Setup Linux Network
===================
Based on Fedora 5 exprience

Setup DNS
1. add DNS IP address to file: /etc/resolv.conf; OR
2. Use GUI to enter primary, secondary DNS

Restart Network
1. Reboot; OR
2. /etc/init.d/network restart

Setup Static Routing
- for PC with external IP to connect to a Local network
1. Create a file called /etc/sysconfig/network-scripts/route-eth0
2. Enter this in the file:
      192.168.0.0/16 via 129.78.77.253
   ... where first IP range in internal network and last IP is gateway
Alternatively,  
3. Create a file called /etc/sysconfig/static-route
4. Enter this in the file:
      any net 192.168.0.0/16 gw 129.78.77.253
     
Remove Microsoft IP range
1. Edit file /etc/sysconfig/network, and put:
     NOZEROCONF=1
2. Edit files /etc/sysconfig/network-scripts/ifcfg_eth<0|1|2|...>, put:
     NOZEROCONF=1

Check Kernel IP Table:
    netstat -nr
   
Add route manually,
route add -net 192.168.0.0/16 gw 129.78.77.253 eth0
 

Remove Microsoft Routing




Network VM with Virtual Box - Bridged, Internal, Host-Only
===========================================================

This section covers the three types of networking options between Linux Guest VMs in Virtual Box, in relation to a Windows host.

I. Bridged Networking
Use Case: The Windows host (eg 192.168.1.7) and the Linux VM (eg 192.168.1.9) have independent IP, but are both belong to the same subnet network. In the usual case where the network is dynamic and the DCHP server, assigns an IP to the Windows host, it will also assign an IP to the Linux VM. Like the Windows host, the Linux VM can also connect to the outside world.

Windows Host setup: Nothing to do, if the Windows host is already connected, ie can browse the internet or ping and external IP. To check the network details, type:
   ipconfig /all
Look for the sections, either:
   i) Wireless LAN adapter Wi-Fi:
  Description ...... Qualcomm Atheros.....
   ii) Ethernet Adapter ethernet:
  Description ...... Qualcomm Atheros ....
Since there are many adapters, you need to look for the sections, using description, that relates to the physical adapters installed on your PC / laptop. The adaptes could be LAN ethernet or Wi-Fi, and the Description gives a clue if they are physical or virtual adapters.

Pick the correct physical adapter and note down the following:
IPv4 address:    eg      192.168.1.7
Subnet Mask:     usually 255.255.255.0
Default Gateway: usually 192.168.1.1

Linux Guest VM setup:
In the Virtual Box, VM settings -
- Enable Network Adapter - Check
- Attached to - Bridged Adapter
- Name: Choose your physical adapter (see Windows Host Setup above). Usually it is "... Wireless Adapter ..." or "... Ethernet Controller ..."
- Promiscuous Mode = Deny,
- Mac Address: (Write this down for later use)  eg. ...1D:9A
- Cable Conneceted - Check

Start the Linux VM,
- determine if it is eth0 or eth1 by: ifconfig -a
  WARNING: By default, configuration exist for eth0 only. But VB requires eth1,2,3,4
  So Copy the config file:
       cd /etc/sysconfig/network-scripts
       cp ifcfg-eth0 ifcfg-eth1
- edit /etc/sysconfig/network-scripts/eth1
    DEVICE = eth1   (change this as necessary)
    HWADDR=  enter the MAC address from the VM settings section above
    TYPE=Ethernet
    ....
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=dhcp
- re-activate the interface:
   ifdown eth1
   ifup eth1
   /etc/init.d/network restart
- check by looking at: ifconfig -a
  the results for eth1 are: IP=192.168.1.9, Bcast 192.168.1.255, Mask 255.255.255.0



II. Internal Networking
Use Case: Two or more Linux VMs (eg 192.168.56.110 and 192.168.56.115) are required to communicate with each other, hence the same subnet 192.168.56.x. They are not connected to the outside world and do not communicate with the Windows host (192.168.1.7).

Windows Host setup: Nothing to setup.
Do the following only after the Linux setup is complete, and then come back to Windows to verify the following:
To check the network details, type:
   ipconfig /all
Look for the section:
  Ethernet adapter VirtualBox Host-Only Network:
  Description: VirtualBox Host-Only Ethernet Adapter.
  Physical Address: eg ... 8C:D9. Not needed to be the same from the Linux VM settings below.
  ....
  IPv4 address:    eg      192.168.56.1
  Subnet Mask:     usually 255.255.255.0

Linux Guest VM setup:
In this section, we will setup up the VM above (VM1) and a new VM (VM2) where VM1 uses eth2 to communicate with VM2 which uses eth0. The VM1 uses eth2 because its eth1 is already involved with Bridged networking.

In the Virtual Box, VM settings for both VM1 (Adapter 2 tab) and VM2 (Adapter 1 tab)
- Enable Network Adapter - Check
- Attached to - Internal Network
- Name: intnet
- Promiscuous Mode = Deny, Allow All
- Mac Address: (Write this down for later use)  ( 9F:4B,   ...77:84)
- Cable Conneceted - Check

Start the Linux VM1,
- determine if it is eth0 or eth1 by: ifconfig -a
  WARNING: By default, configuration exist for eth0 only. But VB requires eth1,2,3,4
  Since VM1 has already eth1 for bridged, the eth2 adapter will be used here.
- edit /etc/sysconfig/network-scripts/eth2
    IPADDR=192.168.56.105
    NETMASK=255.255.255.0
    DEVICE = eth2   (change this as necessary)
    HWADDR=  enter the MAC address from the VM settings section above, eg. 9F:4B
    TYPE=Ethernet
    ....
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=static  
- re-activate the interface:
   ifdown eth2
   ifup eth2
   /etc/init.d/network restart
- check by looking at: ifconfig -a
  the results for eth1 are: IP=192.168.56.105, Bcast 192.168.56.255, Mask 255.255.255.0
- add the lines to /etc/hosts
  192.168.56.105  VM1-host  VM1-host.domain
  192.168.56.115  VM2-host  VM2-host.domain
- add the lines to /etc/sysconfig/network
  NETWORKING=yes
  HOSTNAME=VM1-host.domain

Start the Linux VM2,
- determine if it is eth0 or eth1 by: ifconfig -a
  WARNING: By default, configuration exist for eth0 only. But VB requires eth1,2,3,4
- edit /etc/sysconfig/network-scripts/eth0
    IPADDR=192.168.56.115
    NETMASK=255.255.255.0
    GATEWAY=192.168.56.100
    DEVICE = eth0   (change this as necessary)
    HWADDR=  enter the MAC address from the VM settings section above, eg. 77:84
    TYPE=Ethernet
    ....
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=static  
- re-activate the interface:
   ifdown eth0
   ifup eth0
   /etc/init.d/network restart
- check by looking at: ifconfig -a
  the results for eth0 are: IP=192.168.56.115, Bcast 192.168.56.255, Mask 255.255.255.0
- add the lines to /etc/hosts
  192.168.56.105  VM1-host  VM1-host.domain
  192.168.56.115  VM2-host  VM2-host.domain
- add the lines to /etc/sysconfig/network
  NETWORKING=yes
  HOSTNAME=VM2-host.domain
 


III. Host-Only Networking
Use Case: This has features of both Bridged and Internal Networking. The Windows host is connected to the Linux VM (VM1) so that they can communicate (able to ping) with each other (like Bridged network). However the VM1 is not connected to the outside world (like internal network).
The VM1 may see the Windows host as a HOST - hence the name Host only. So the VM1 may not be able to interact with the Windows host like a peer node, but it is able to access it, such as accessing a License server on the Windows Host.

Windows Host setup: This will be using the SECOND VirtualBox Host-Only adapter because the First Host-Only Adapter has been used by the Internal networking above. Do the following only after the Linux setup is complete, and then come back to Windows to verify the following:
To check the network details, type:
   ipconfig /all
Look for the section:
  Ethernet adapter VirtualBox Host-Only Network #2:
  Description: VirtualBox Host-Only Ethernet Adapter #2.
  Physical Address: eg ... 74:F0.
  ....
  IPv4 address:    eg      192.168.39.1
  Subnet Mask:     usually 255.255.255.0

Linux Guest VM setup:
For VM1, since adapter 1 is used for Bridged, adapter 2 is used for Internal, so the adapter 3 will be used here for Host-Only networking
In the Virtual Box, VM settings for Adapter 3 -
- Enable Network Adapter - Check
- Attached to - Host-Only Adapter
- Name: VirtualBox Host-Only Ethernet Adapter #2
- Promiscuous Mode = Deny,
- Mac Address: (Write this down for later use)   eg.  ... 4A:C6
- Cable Conneceted - Check

Start the Linux VM,
- determine if it is eth0 or eth1 by: ifconfig -a
  WARNING: By default, configuration exist for eth0 only. But VB requires eth1,2,3,4
  So Copy the config file:
       cd /etc/sysconfig/network-scripts
       cp ifcfg-eth1 ifcfg-eth3
- edit /etc/sysconfig/network-scripts/eth13
    DEVICE = eth3   (change this as necessary)
    HWADDR=  enter the MAC address from the VM settings section above
    TYPE=Ethernet
    ....
    ONBOOT=yes
    NM_CONTROLLED=no
    BOOTPROTO=dhcp
- re-activate the interface:
   ifdown eth1
   ifup eth1
   /etc/init.d/network restart
- check by looking at: ifconfig -a
  the results for eth1 are: IP=192.168.39.201, Bcast 192.168.39.255, Mask 255.255.255.0
- add the lines to /etc/hosts
  192.168.39.201  VM1-host  VM1-host.domain
- add the lines to /etc/sysconfig/network
  NETWORKING=yes
  HOSTNAME=VM1-host.domain
  GATEWAY=192.168.39.1