netcat

http://www.binarytides.com/netcat-tutorial-for-beginners/

https://nmap.org/ncat/guide/ncat-usage.html#ncat-connect

So basically netcat is a tool to do some bidirectional network communication over the TCP/UDP protocols.

ncat <host> [<port>]

<host> may be a hostname or IP address, and <port> is a port number. Listen mode is the same, with the addition of the --listen option (or its -l alias):

ncat –listen [<host>] [<port>]
ncat -l [<host>] [<port>]

1. Telnet

The very first thing netcat can be used as is a telnet program. Lets see how.

$ nc -v google.com 80

Now netcat is connected to google.com on port 80 and its time to send some message. Lets try to fetch the index page. For this type “GET index.html HTTP/1.1” and hit the Enter key twice. Remember twice.

$ nc -v google.com 80
Connection to google.com 80 port [tcp/http] succeeded!
GET index.html HTTP/1.1

HTTP/1.1 302 Found
Location: http://www.google.com/
Cache-Control: private
Content-Type: text/html; charset=UTF-8
X-Content-Type-Options: nosniff
Date: Sat, 18 Aug 2012 06:03:04 GMT
Server: sffe
Content-Length: 219
X-XSS-Protection: 1; mode=block

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

The output from google.com has been received and echoed on the terminal.

2. Simple socket server

To open a simple socket server type in the following command.

$ nc -l -v 1234

The above command means : Netcat listen to TCP port 1234. The -v option gives verbose output for better understanding. Now from another terminal try to connect to port 1234 using telnet command as follows :

$ telnet localhost 1234
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
abc
ting tong

After connecting we send some test message like abc and ting tong to the netcat socket server. The netcat socket server will echo the data received from the telnet client.

$ nc -l -v 5555

Connection from 127.0.0.1 port 5555 [tcp/rplay] accepted
abc
ting tong

This is a complete Chatting System. Type something in netcat terminal and it will show up in telnet terminal as well. So this technique can be used for chatting between 2 machines.

netcat

shell-1

http://www.tldp.org/LDP/abs/html/sha-bang.html

http://www.tldp.org/LDP/abs/html/special-chars.html

# all kinds of command and special characters

In computing, a shebang (also called a sha-bang)——-   #!

#!/bin/sh
#!/bin/bash
#!/usr/bin/perl
#!/usr/bin/tcl
#!/bin/sed -f
#!/bin/awk -f

Each of the above script header lines calls a different command interpreter, be it /bin/sh, the default shell (bash in a Linux system) or otherwise.

Invoking the script

Either:
chmod 555 scriptname (gives everyone read/execute permission) [2]

or
chmod +rx scriptname (gives everyone read/execute permission)

chmod u+rx scriptname (gives only the script owner read/execute permission)

Having made the script executable, you may now test it by ./scriptname. [3] If it begins with a “sha-bang” line, invoking the script calls the correct command interpreter to run it.

As a final step, after testing and debugging, you would likely want to move it to /usr/local/bin (as root, of course), to make the script available to yourself and all other users as a systemwide executable. The script could then be invoked by simply typing scriptname [ENTER] from the command-line.

Chapter 3. Special Characters

Comments. Lines beginning with a # (with the exception of #!) are comments and will not be executed.

A command may not follow a comment on the same line. There is no method of terminating the comment, in order for “live code” to begin on the same line. Use a new line for the next command.

Of course, a quoted or an escaped # in an echo statement does not begin a comment.

Command separator [semicolon]. Permits putting two or more commands on the same line.

echo hello; echo there


if [ -x "$filename" ]; then    #  Note the space after the semicolon.
#+                   ^^
  echo "File $filename exists."; cp $filename $filename.bak
else   #                       ^^
  echo "File $filename not found."; touch $filename
fi; echo "File test complete."

Terminator in a case option [double semicolon].

case "$variable" in
  abc)  echo "\$variable = abc" ;;
  xyz)  echo "\$variable = xyz" ;;
esac

;;&, ;&

Terminators in a case option (version 4+ of Bash).

When considering directory names, a single dot represents the current working directory, and two dots denote the parent directory.

bash$ pwd
/home/bozo/projects

bash$ cd .
bash$ pwd
/home/bozo/projects

bash$ cd ..
bash$ pwd
/home/bozo/
	        echo -n

The dot often appears as the destination (directory) of a file movement command, in this context meaning current directory.

bash$ cp /home/bozo/current_work/junk/* .

Copy all the “junk” files to $PWD.

\

escape [backslash]. A quoting mechanism for single characters.

\X escapes the character X. This has the effect of “quoting” X, equivalent to ‘X’. The \ may be used to quote and , so they are expressed literally.

See Chapter 5 for an in-depth explanation of escaped characters.

/

Filename path separator [forward slash]. Separates the components of a filename (as in /home/bozo/projects/Makefile).

This is also the division arithmetic operator.

\

escape [backslash]. A quoting mechanism for single characters.

`

command substitution. The `command` construct makes available the output of command for assignment to a variable. This is also known as backquotes or backticks.

:

null command [colon]. This is the shell equivalent of a “NOP” (no op, a do-nothing operation). It may be considered a synonym for the shell builtin true. The : command is itself a Bashbuiltin, and its exit status is true (0).

ps -aux|grep mouse|awk ‘{print $2}’

ps -aux 列出所有process的信息,grep抓住,awk 只列出第二列的内容

man command

查看命令的使用方法,用q退出

$

Variable substitution (contents of a variable).

var1=5
var2=23skidoo

echo $var1     # 5
echo $var2     # 23skidoo

A $ prefixing a variable name indicates the value the variable holds.

Example 4-3. Variable Assignment, plain and fancy

#!/bin/bash

a=23              # Simple case
echo $a
b=$a
echo $b

# Now, getting a little bit fancier (command substitution).

a=`echo Hello!`   # Assigns result of 'echo' command to 'a' ...
echo $a
#  Note that including an exclamation mark (!) within a
#+ command substitution construct will not work from the command-line,
#+ since this triggers the Bash "history mechanism."
#  Inside a script, however, the history functions are disabled by default.

a=`ls -l`         # Assigns result of 'ls -l' command to 'a'
echo $a           # Unquoted, however, it removes tabs and newlines.
echo
echo "$a"         # The quoted variable preserves whitespace.
                  # (See the chapter on "Quoting.")

exit 0

4.3. Bash Variables Are Untyped

Unlike many other programming languages, Bash does not segregate its variables by “type.” Essentially, Bash variables are character strings, but, depending on context, Bash permits arithmetic operations and comparisons on variables. The determining factor is whether the value of a variable contains only digits.

find the file/number of line containing “matching_string”

sudo grep -rnw ‘path’ -e “matching_string”

no line folding

In less, it’s called line folding rather than line wrapping.  To set it not to fold, use the -S option:

-S, --chop-long-lines

Causes lines longer than the screen width to be chopped rather than folded. That is, the portion of a long line that does not fit in the screen width is not shown. The default is to fold long lines; that is, display the remainder on the next line.

less(1)


Alternatively, as mentioned in the below comment, if you already opened the file, you can toggle the mode by typing -S (and then Enter for some implementations).

shell-1