Tuesday, January 31, 2012

Command Redirection

Redirection is a function common to most command-line interpreters, that can redirect standard streams to user-specified locations. The input or output stream location is referred to as a handle.
Definition: Redirection is the switching of a standard stream of data so that it comes from a source other than its default source or or that it goes to some destination other than its default destination.
standard streams for input, output, and error
Redirection operatorDescription
>Writes the command output to a file or a device, such as a printer, instead of the Command Prompt window.
<Reads the command input from a file, instead of reading input from the keyboard.
>>Appends the command output to the end of a file without deleting the information that is already in the file.
>&Writes the output from one handle to the input of another handle.
<&Reads the input from one handle and writes it to the output of another handle.
|Reads the output from one command and writes it to the input of another command. Also known as a pipe.
The following table lists the available handles:
HandleNumeric equivalentDescription
STDIN0Keyboard input
STDOUT1Output to the Command Prompt window
STDERR2Error output to the Command Prompt window
UNDEFINED3-9These handles are defined individually by the application and are specific to each tool.
The numbers zero through nine (that is, 0-9) represent the first 10 handles. You can use Cmd.exe to run a program and redirect any of the first 10 handles for the program. To specify which handle you want to use, type the number of the handle before the redirection operator. If you do not define a handle, the default < redirection input operator is zero (0) and the default > redirection output operator is one (1). After you type the < or > operator, you must specify where you want to read or write the data. You can specify a file name or another existing handle.
To specify redirection to existing handles, use the ampersand (&) character followed by the handle number that you want to redirect (that is, &handle#). For example, the following command redirects handle 2 (that is, STDERR) into handle 1 (that is, STDOUT): 1<&2
Duplicating handles:
command 2> filenameRedirect any error message into a file
command 2>> filenameAppend any error message into a file
(command) 2> filenameRedirect any CMD.exe error into a file
command > file 2>&1Redirect errors and output to one file
command > file 2<&1Redirect output and errors to one file
command > fileA 2> fileBRedirect output and errors to separate files
command 2>&1 > filenameThis will fail!
Redirect to NUL (hide errors):
command 2> nulRedirect error messages to NUL
command >nul 2>&1Redirect error and output to NUL
command >filename 2> nulRedirect output to file but suppress error
(command) >filename 2> nulRedirect output to file but suppress CMD.exe errors

Note, any long filenames must be surrounded in "double quotes". A CMD error is an error raised by the command processor itself rather than the program/command.

Redirection with > or 2> will overwrite any existing file.

You can also redirect to a printer with > PRN or >LPT1

To prevent the > and < characters from causing redirection, escape with a caret: ^> or ^<

Using the pipe operator (|)
The pipe operator (|) takes the output (by default, STDOUT) of one command and directs it into the input (by default, STDIN) of another command. For example, the following command sorts a directory:
dir | sort
In this example, both commands start simultaneously, but then the sort command pauses until it receives the dir command's output. The sort command uses the dir command's output as its input, and then sends its output to handle 1 (that is, STDOUT).

Examples of redirection:
DIR >MyFileListing.txt
   DIR /o:n >"Another list of Files.txt"

   ECHO y| DEL *.txt

   ECHO Some text ^<html tag^> more text
   MEM /C >>MemLog.txt

   Date /T >>MemLog.txt

   SORT < MyTextFile.txt

   SET _output=%_missing% 2>nul

   DIR C:\ >List_of_C.txt 2>errorlog.txt
   FIND /i "Jones" < names.txt >logfile.txt

   DIR C:\ >List_of_C.txt & DIR D:\ >List_of_D.txt

   ECHO DIR C:\ ^> c:\logfile.txt >NewScript.cmd

   (TYPE logfile.txt >> newfile.txt) 2>nul

Wednesday, January 25, 2012

Concurrent Administrative Operations

Not all administrative tasks are allowed to run concurrently. In the table below, a black circle indicates two operations that cannot run in a database at the same time.
Grid showing tasks that can run concurrently
File shrink operations spend most processing time reallocating pages into areas retained after the shrink has completed; it then attempts to change the file size only as the last step. File shrink operations can be started while a backup is running, provided that the backup finishes before the file shrink operation attempts to change the size of the files.

Source : http://technet.microsoft.com/en-us/library/ms189315.aspx

Tuesday, January 24, 2012

Using Automatic Proxy Configuration

Automatic proxy (auto-proxy) makes system administration easier, because you can automatically configure proxy settings such as server addresses and bypass lists. To configure more advanced settings for auto-proxy, you can create a separate .js, .jvs, or .pac script file and then copy the file to a server location. Then, you can specify the server location for the script file within the Automatic Configuration settings of browser. The auto-proxy script file is executed whenever a network request is made. Within the script, you can configure multiple proxy servers for each protocol type; then, if a proxy server connection fails, browser automatically attempts to connect to another proxy server that you have specified.
PAC File
A proxy auto-config (PAC) file defines how web browsers and other user agents can automatically choose the appropriate proxy server (access method) for fetching a given URL. The Proxy auto-config file format was originally designed by Netscape in 1996 for the Netscape Navigator 2.0 and is a text file that defines at least one JavaScript function, FindProxyForURL(url, host), with two arguments. By convention, the PAC file is normally named proxy.pac.
The JS function syntax: string FindProxyForURL(url, host)
url The full URL being accessed or URL of the object
host The hostname extracted from the URL. This is only for convenience, it is the exact same string as between :// and the first : or / after that. The port number is not included in this parameter. It can be extracted from the URL when necessary.
return value A string describing the configuration. The return value of the function should be a semicolon seperated list of options from the following list:
DIRECT Connections should be made directly, without any proxies.
PROXY host:port The specified proxy should be used.
SOCKS host:port The specified SOCKS server should be used.
A null string is the same as DIRECT. Each option will be tried in turn until one is useable.
To use it, a PAC file is published to a web server, and client user agents are instructed to use it, either by entering the URL in the proxy connection settings of the browser or through the use of the WPAD protocol. Even though most clients will process the script regardless of the MIME type returned in the HTTP request, for the sake of completeness and to maximize compatibility, the web server should be configured to declare the MIME type of this file to be either application/x-ns-proxy-autoconfig or application/x-javascript-config.
Example :
1. function FindProxyForURL(url, host)
     if (isPlainHostName(host))
         return "DIRECT";
          return "PROXY proxy:80";
2. function FindProxyForURL(url, host)
     if (url.substring(0, 5) == "http:")
          return "PROXY proxy:80";
     else if (url.substring(0, 4) == "ftp:")
          return "PROXY fproxy:80";
     else if (url.substring(0, 7) == "gopher:")
          return "PROXY gproxy";
     else if (url.substring(0, 6) == "https:")
          return "PROXY secproxy:8080";
          return "DIRECT";
Autoconfigure the Proxy Settings from a Local Copy of the PROXY.PAC File (IE or Netscape) :
To use local copy of PROXY.PAC file, copy the file to some local directory, and point to it.
1. Copy the PROXY.PAC file to the C:\WINDOWS directory, or other directory of your choice.
2. In the browser proxy settings, configure the Automatic Proxy Configuration (Netscape) or Use Automatic Configuration Script (IE) URL to:
Netscape, use: file:///c|/windows/proxy.pac
Internet Explorer, use: file://c:/windows/proxy.pac
In Netscape, click on the Reload button.
The Web Proxy Auto-Discovery Protocol (WPAD)
WPAD is not designed to find the actual proxy settings, but to find the PAC script which tell the browser which settings to use. WPAD uses several methods for finding out location of the PAC script. If the method does not provide information about the port or the path name, then the client should use, as defaults, port 80 and /wpad.dat respectively. The client should not use a default host.

Tuesday, January 17, 2012

SQL Server : Protocols

When an application communicates with the Database Engine, the application programming interfaces (APIs) exposed by the protocol layer formats the communication using a Microsoft-defined format called a tabular data stream (TDS) packet. The SQL Server Network Interface (SNI) protocol layer on both the server and client computers encapsulates the TDS packet inside a standard communication protocol, such as TCP/IP or Named Pipes. On the server side of the communication, the network libraries are part of the Database Engine. On the client side, the network libraries are part of the SQL Native Client. The configuration of the client and the instance of SQL Server determine which protocol is used. SQL Server can be configured to support multiple protocols simultaneously, coming from different clients. Each client connects to SQL Server with a single protocol. If the client program does not know which protocols SQL Server is listening on, you can configure the client to attempt multiple protocols sequentially. The following protocols are available:

Shared Memory The simplest protocol to use, with no configurable settings. Clients using the Shared Memory protocol can connect only to a SQL Server instance running on the same computer, so this protocol is not useful for most database activity. Clients using MDAC 2.8 or earlier cannot use the Shared Memory protocol. If such a connection is attempted, the client is switched to the Named Pipes protocol.

Named Pipes A protocol developed for local area networks (LANs). A portion of memory is used by one process to pass information to another process, so that the output of one is the input of the other. The second process can be local (on the same computer as the first) or remote (on a networked computer).

TCP/IP The most widely used protocol over the Internet. TCP/IP can communicate across interconnected networks of computers with diverse hardware architectures and operating systems. It includes standards for routing network traffic and offers advanced security features. Enabling SQL Server to use TCP/IP requires the most configuration effort, but most networked computers are already properly configured.

Virtual Interface Adapter (VIA) A protocol that works with VIA hardware. This is a specialized protocol; configuration details are available from your hardware vendor.