Learning Models

arrays.pl

A UNIX Command
$cat arrays.pl
# Simple array constructs.
@fred = ("How", "are", "you", "today?");
print "\@fred contains (@fred).\n";

$mike = $fred[1];
print "$mike $fred[3]\n";

# The array name in a scalar context gives the size.
$fredsize = @fred;
print '@fred has ', "$fredsize elements.\n";

# The $#name gives the max subscript (size less one).
print "Max sub is $#fred\n";

$perl arrays.pl
@fred contains (How are you today?).
are today?
@fred has 4 elements.
Max sub is 3
$

UNIX Explanation
Variables whose names begin with @ are arrays. If @sue is
an array,  it is  different variable from  $sue. However,
members   of  @sue   are  selected   by   $sue[$i].   The
construction $#arrname gives the maximum subscript of the
array @arrname.

source : http://sandbox.mc.edu/~bennet/perl/leccode/var2_pl.html

cat + grep + wc and pipe operator

A UNIX Command
$ls
1984  2001  2004  2007	2010		 TED-talks-grouped-by-year-in-high-quality.metalink
1990  2002  2005  2008	2011		 ted_urls
1998  2003  2006  2009	ted_download.sh
$cat TED-talks-grouped-by-year-in-high-quality.metalink | grep -w url | wc -l
980
$

UNIX Explanation
This command finds out the  number of lines with the word
"url" in the file TED*.

bzip2, bunzip2 – a block-sorting file compressor, v1.0.4

Connected From This

A UNIX Command

$wget http://www.kernel.org/pub/linux/kernel/v3.0/patch-3.0.4.bz2
--2011-09-03 19:22:43--  http://www.kernel.org/pub/linux/kernel/v3.0/patch-3.0.4.bz2
Resolving www.kernel.org... 130.239.17.5, 149.20.4.69, 199.6.1.165, ...
Connecting to www.kernel.org|130.239.17.5|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 96120 (94K) [application/x-bzip2]
Saving to: `patch-3.0.4.bz2'

100%[===================================================================================>] 96,120      85.3K/s   in 1.1s

2011-09-03 19:22:45 (85.3 KB/s) - `patch-3.0.4.bz2' saved [96120/96120]

$bunzip2 patch-3.0.4.bz2
$ls patch-3.0.4
patch-3.0.4
$


UNIX Explanation

bzip2  compresses files  using the  Burrows-Wheeler block
sorting text  compression algorithm, and  Huffman coding.
Compression  is generally  considerably better  than that
achieved    by    more    conventional    LZ77/LZ78-based
compressors,  and approaches the  performance of  the PPM
family of statistical compressors.

Related Source Code Exposition


for (t = 0; t < nGroups; t++) {
minLen = 32;
maxLen = 0;
for (i = 0; i len[t][i] > maxLen) maxLen = s->len[t][i];
if (s->len[t][i] len[t][i];
}
BZ2_hbCreateDecodeTables (
&(s->limit[t][0]),
&(s->base[t][0]),
&(s->perm[t][0]),
&(s->len[t][0]),
minLen, maxLen, alphaSize
);
s->minLens[t] = minLen;
}

Source Code Highlight

Create the Huffman decoding tables

Featured Image

Related Knowledge

bzip2  expects a  list  of file  names  to accompany  the
command-line   flags.   Each  file   is  replaced   by  a
compressed   version    of   itself,   with    the   name
"original_name.bz2".  Each  compressed file has  the same
modification  date,   permissions,  and,  when  possible,
ownership  as the corresponding  original, so  that these
properties  can be  correctly  restored at  decompression
time.   File name  handling is  naive in  the  sense that
there is no mechanism for preserving original file names,
permissions,  ownerships or  dates  in filesystems  which
lack  these concepts,  or have  serious file  name length
restrictions, such as MS-DOS.

source : debian manual pages for bzip2

scalar.pl —-x strings

A UNIX Command
$perl scalar.pl
The variable $fred contains Fred here.
Sum is 66.
$cat scalar.pl
$fred = "Fred here";
$barney = 56;
$sum = 10 + $barney;
print 'The variable $fred' . " contains $fred.\n";
print "Sum is $sum.\n";

$cat scalar.pl
$fred = "Fred here";
$barney = 56;
$sum = 10 + $barney;
print 'The variable $fred.' . " contains $fred.\n";
print "Sum is $sum.\n";

$perl scalar.pl
The variable $fred. contains Fred here.
Sum is 66.
$cat scalar.pl
$fred = "Fred here";
$barney = 56;
$sum = 10 + $barney;
print "The variable $fred." . " contains $fred.\n";
print "Sum is $sum.\n";

$perl scalar.pl
The variable Fred here. contains Fred here.
Sum is 66.
$

UNIX Explanation
Various operations on scalar (string) variables.

source: http://sandbox.mc.edu/~bennet/perl/leccode/var1_pl.html

parameters tcp_retries2

A UNIX Parameter
$cat /proc/sys/net/ipv4/tcp_retries2
15
$

Parameter Definition
How  many  times  to   retry  before  killing  alive  TCP
connection. RFC1122 says that  the limit should be longer
than 100 sec.  It is too small number.  The default value
of 15 corresponds to ~ 13 - 30 minutes, depending on RTO.

Parameter Code Internals


snippet 1
{
.procname = "tcp_retries2",
.data = &sysctl_tcp_retries2,
.maxlen = sizeof(int),
.mode = 0644,
.proc_handler = proc_dointvec
},

snippet 2
if (retransmits_timed_out(sk, sysctl_tcp_retries1, 0, 0)) {
/* Black hole detection */
tcp_mtu_probing(icsk, sk);

dst_negative_advice(sk);
}

retry_until = sysctl_tcp_retries2;
if (sock_flag(sk, SOCK_DEAD)) {
const int alive = (icsk->icsk_rto < TCP_RTO_MAX);

retry_until = tcp_orphan_retries(sk, alive);
do_reset = alive ||
!retransmits_timed_out(sk, retry_until, 0, 0);

if (tcp_out_of_resources(sk, do_reset))
return 1;
}


Related From Research Paper
A tool  for TCP  stack testing and  TCP/IP fingerprinting
(a.k.a.   OS  detection)   is  introduced.   While  tools
presently exist  to do either  OS detection[1, 2]  or TCP
stack testing[3, 4], the  methods they employ are limited
by  the  techniques  and  analysis  performed,  sometimes
resulting in incorrect re- sults or no results at all. We
introduce   synscan,  a  tool   whose  objective   is  to
fingerprint     every      aspect     of     a     TCP/IP
implementation.    synscan   is    not    meant   as    a
proof-of-concept tool; rather, it  is a robust and useful
tool which can  be used in addition to  others for TCP/IP
stack  testing and OS  de- tection.  synscan incorporates
most  of the  techiques used  by the  existing  tools and
introduces  a number  of new  ones.  synscan's  s primary
advantage is that each test begins with a TCP SYN segment
(hence the name)  to an open port, giving  it the ability
to   test  and  fingerprint   even  the   most  fortified
hosts. Conclusive data from  large network scans and com-
parisons  to   results  from  existing   tools  are  also
reported.

source:
SYNSCAN: Towards Complete TCP/IP Fingerprinting
                       Greg Taleck
                    
                    NFR Security, Inc.
               5 Choke Cherry Rd, Suite 200
                   Rockville, MD 20850




id – print real and effective user and group IDs

A UNIX Command
$id
uid=1000(jeffrin) gid=1000(jeffrin) groups=1000(jeffrin),29(audio),1001(wireshark)
$id -n
id: cannot print only names or real IDs in default format
$id -a
uid=1000(jeffrin) gid=1000(jeffrin) groups=1000(jeffrin),29(audio),1001(wireshark)
$id -g
1000
$id -G
1000 29 1001
$id -Gn
jeffrin audio wireshark
$id -r
id: cannot print only names or real IDs in default format
$id -rG
1000 29 1001
$id -u
1000
$id -un
jeffrin
$

UNIX Explanation

GNU/Linux Based Explanation
Print  user  and  group  information  for  the  specified
USERNAME, or (when USERNAME omitted) for the current user

SunOS Based Explanation
If no user operand is provided, the id utility writes the
user and  group IDs and the corresponding  user and group
names of the invoking process to standard output.  If the
effective and real IDs do not match, both are written. If
multiple groups  are supported by  the underlying system,
/usr/xpg4/bin/id  also  writes  the  supplementary  group
affiliations of the invoking process.


If a  user operand  is provided and  the process  has the
appropriate  privileges, the  user and  group IDs  of the
selected user  are written.  In this  case, effective IDs
are assumed to be identical  to real IDs. If the selected
user has more than  one allowable group membership listed
in  the group database,  /usr/xpg4/bin/id writes  them in
the same manner as  the supplementary groups described in
the preceding paragraph.

FreeBSD Based Explanation
The  id utility  displays the  user and  group  names and
numeric  IDs, of  the  calling process,  to the  standard
output.   If the  real and  effective IDs  are different,
both are  displayed, otherwise only  the real ID  is dis-
played.

If a user (login name  or user ID) is specified, the user
and group IDs of that  user are displayed.  In this case,
the real and effective IDs are assumed to be the same.

mysql start

MySQL tinkering


mysql> create database jeffdatabase
-> ;
Query OK, 1 row affected (0.03 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| jeffdatabase |
| mysql |
+--------------------+
3 rows in set (0.00 sec)

mysql> use jeffdatabase
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table songs (hindi VARCHAR(20));
Query OK, 0 rows affected (0.03 sec)

mysql>

true and false

A UNIX Command

$true
$echo $?
0
$false
$echo $?
1
$

UNIX Explanation

true - do nothing, successfully
exit with a status code indicating success.
false - do nothing, unsuccessfully
exit with a status code indicating failure.

Related Source Code Exposition


int
main (int argc, char **argv)
{

if (argc == 2)
{
initialize_main (&argc, &argv);
set_program_name (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);

atexit (close_stdout);

if (STREQ (argv[1], "--help"))
usage (EXIT_STATUS);

if (STREQ (argv[1], "--version"))
version_etc (stdout, PROGRAM_NAME, PACKAGE_NAME, Version, AUTHORS,
(char *) NULL);
}

exit (EXIT_STATUS);
}

Source Code Highlight

Recognize –help or –version only if it’s the only
command-line argument.

Featured Image

Related Knowledge

`true' does  nothing except return  an exit status  of 0,
meaning "success".  It  can be used as a  place holder in
shell  scripts  where  a  successful command  is  needed,
although the  shell built-in  command `:' (colon)  may do
the same thing faster.   In most modern shells, `true' is
a built-in command,  so when you use `true'  in a script,
you're probably  using the built-in command,  not the one
documented here.

Note,  however, that it  is possible  to cause  `true' to
exit   with  nonzero   status:  with   the   `--help'  or
`--version'  option,  and  with standard  output  already
closed or redirected to a  file that evokes an I/O error.
For example, using a Bourne-compatible shell:

 $ ./true --version >&-
     ./true: write error: Bad file number
     $ ./true --version > /dev/full
     ./true: write error: No space left on device

This version of `true' is implemented as a C program, and
is  thus  more secure  and  faster  than  a shell  script
implementation, and  may safely be used as  a dummy shell
for the purpose of disabling accounts.

source : info coreutils ‘true invocation’