{"id":3472,"date":"2014-08-21T09:29:50","date_gmt":"2014-08-21T01:29:50","guid":{"rendered":"http:\/\/rmohan.com\/?p=3472"},"modified":"2014-08-21T10:10:26","modified_gmt":"2014-08-21T02:10:26","slug":"shell-samples","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=3472","title":{"rendered":"shell samples"},"content":{"rendered":"<h1 class=\"book-heading\" style=\"color: #000000;\">awk examples<\/h1>\n<p style=\"color: #000000;\">\u00a0cat 03092608.LOG|grep XYZ|awk -F&#8221;.&#8221; &#8216;{print $2}&#8217; |sort|awk &#8216;{tot += 1 } $1 != prev {print prev,tot;tot=0;prev=$1}END{print prev,tot;tot=0;prev=$1}&#8217;<\/p>\n<div id=\"node-80\" class=\"section-4\" style=\"color: #000000;\">\n<h1 class=\"book-heading\">adhocs<\/h1>\n<p><strong>1.\u00a0find_processes_using_ipcs.ksh<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>ipcs -bop|grep `whoami`|awk &#8216;{print $10}&#8217;|egrep -v &#8220;^0|^$&#8221;|sort -u|awk &#8216;{print &#8220;ps -ef|grep &#8220;, \u00a0$1}&#8217;|ksh<\/p>\n<div><strong>2. iostat<\/strong><\/div>\n<div>\u00a0 iostat -xn 4 30<\/div>\n<div><\/div>\n<div><strong>3. jmap<\/strong><\/div>\n<div>\u00a0\u00a0jmap -heap:format=b &lt;pid&gt;<\/div>\n<div><\/div>\n<div><strong>4. monitorMemLeaking<\/strong><\/div>\n<div>\n<div>if [ $# -lt 1 ]<\/div>\n<div>then<\/div>\n<div>\u00a0 echo &#8220;Usage: $0 [pid]&#8221;<\/div>\n<div>\u00a0 exit<\/div>\n<div>fi<\/div>\n<div><\/div>\n<div>PID=$1<\/div>\n<div><\/div>\n<div>while true<\/div>\n<div>do<\/div>\n<div>\u00a0 vsz1=`ps -eo vsz,rss,pid|grep &#8220;$PID$&#8221; |awk &#8216;{print $2}&#8217;`<\/div>\n<div>\u00a0 rss1=`ps -eo vsz,rss,pid|grep &#8220;$PID$&#8221; |awk &#8216;{print $3}&#8217;`<\/div>\n<div>\u00a0 echo &#8220;vsz1=$vsz1, rss1=$rss1&#8230; wait for 5 mins\\n&#8221;<\/div>\n<div>\u00a0 sleep 300<\/div>\n<div>\u00a0 vsz2=`ps -eo vsz,rss,pid|grep &#8220;$PID$&#8221; |awk &#8216;{print $2}&#8217;`<\/div>\n<div>\u00a0 rss2=`ps -eo vsz,rss,pid|grep &#8220;$PID$&#8221; |awk &#8216;{print $3}&#8217;`<\/div>\n<div>\u00a0 echo &#8220;vsz2=$vsz2, rss2=$rss2\\n&#8221;<\/div>\n<div>\u00a0 let vszLeakingRate=&#8221;(${vsz2} &#8211; ${vsz1}) \/ 5&#8243;<\/div>\n<div>\u00a0 let rssLeakingRate=&#8221;(${rss2} &#8211; ${rss1}) \/ 5&#8243;<\/div>\n<div>\u00a0 echo &#8220;PID=$PID, vszLeakingRate = $vszLeakingRate (KB)\/ per min, rssLeakingRate=$rssLeakingRate (KB)\/per min\\n\\n&#8221;<\/div>\n<div>done<\/div>\n<\/div>\n<div><\/div>\n<div><strong>5.\u00a0port_check.ksh<\/strong><\/div>\n<div>#!\/usr\/bin\/ksh<\/div>\n<div>#If lsof or nmap is available, we might be able to do this easily<\/div>\n<div><\/div>\n<div>echo &#8220;which port?&gt;\\c &#8220;<\/div>\n<div>read port<\/div>\n<div><\/div>\n<div>#for pid in `ps -ef -o pid | tail +2`<\/div>\n<div>#do<\/div>\n<div>#foundport=`\/usr\/bin\/pfiles $pid 2&gt;&amp;1 | grep &#8220;sockname:&#8221; | grep &#8220;port: $port$&#8221;`<\/div>\n<div>#if [ &#8220;$foundport&#8221; != &#8220;&#8221; ]<\/div>\n<div>#then<\/div>\n<div># \u00a0 \u00a0echo &#8220;proc: $pid, $foundport&#8221;<\/div>\n<div>#fi<\/div>\n<div>#done<\/div>\n<div>#<\/div>\n<div>echo &#8220;Going to find out which process is using port: $port&#8221;<\/div>\n<div><\/div>\n<div>ps -ef|grep `whoami`|grep -v grep|awk &#8216;{print $2}&#8217;|while read i<\/div>\n<div>do<\/div>\n<div># \u00a0 \u00a0check=`pfiles $i 2&gt;&amp;-|grep &#8220;sockname:&#8221; | grep &#8220;port:&#8221; | grep $port$&#8221;`<\/div>\n<div>\u00a0 \u00a0 check=`pfiles $i 2&gt;&amp;-| grep $port$&#8221;`<\/div>\n<div>\u00a0 \u00a0 echo &#8220;$check.\\c&#8221;<\/div>\n<div>\u00a0 \u00a0 if [ ! -z &#8220;${check}&#8221; ]<\/div>\n<div>\u00a0 \u00a0 then<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 print &#8220;\\n&#8221;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 print &#8220;The port number $port used by process ID=$i&#8221;<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 #break<\/div>\n<div>\u00a0 \u00a0 fi<\/div>\n<div>\u00a0 \u00a0 check=&#8221;&#8221;<\/div>\n<div>done<\/div>\n<div>exit<\/div>\n<div><\/div>\n<div><strong>6. Difference between SIZE and RSS of a process?<\/strong><\/div>\n<div>The basic difference is that SIZE represents the total address space size\u00a0of a process, while RSS is the actual physical memory currently occupied\u00a0by pages belonging to that process (roughly speaking). Regions of an\u00a0address space mapped to something don&#8217;t necessarily occupy any RAM.<\/div>\n<div><\/div>\n<div><strong>7.\u00a0RSS_vs_real_memory.txt<\/strong><\/div>\n<div>\n<div>Using the ps command it is possible to look at the resident set size\u00a0(rss) parameter of each process&#8230; it is my understanding that\u00a0the value of rss indicates how much real memory the process is using&#8230;\u00a0\u00a0if i sum up all of the rss values for each process, i sometimes find\u00a0that the sum is greater than the available real memory installed\u00a0in the system&#8230;<\/div>\n<div><\/div>\n<\/div>\n<div>The non-modifiable parts (the compiled code &amp; const data) of\u00a0shared libraries &amp; ELF binaries are only loaed into memory once and\u00a0those are shared among all the processes. \u00a0The modifiable parts\u00a0(non-const data, etc.) are copied into each processes&#8217;s address space.<\/div>\n<div>\/usr\/proc\/bin\/pmap -x will show you how much is shared and how much is\u00a0not.<\/div>\n<div><\/div>\n<div><strong>8. vmstat<\/strong><\/div>\n<div>\n<div>The &#8220;free&#8221; column tells you how much real memory is *not* being used.\u00a0That&#8217;s usually more important &#8212; if that gets low, then you need more\u00a0memory. \u00a0And if it&#8217;s always very high, you could be wasting memory.\u00a0The &#8220;top&#8221; command also shows this information.<\/div>\n<div><\/div>\n<div>The problem with top and vmstat is that they do not account for the\u00a0real memory that the OS keeps cached. Memory that the OS caches\u00a0looks to these programs like active memory. In a sense it is active\u00a0memory since the OS is hanging on to it waiting for new requests&#8230;\u00a0but, it is deceptive since the memory is actually available for use&#8230;<\/div>\n<\/div>\n<div>Also, just because free memory may be reported as low on a system does\u00a0not necessarily indicate that the machine needs more memory&#8230; other\u00a0things such as heavy paging and the scanner running often are better\u00a0indicators that there is not enough real memory&#8230; this is due to the\u00a0OS caching mentioned above&#8230; the system may seem low on memory but\u00a0the OS is holding onto blocks of memory that are actually available\u00a0for use by processes<\/div>\n<div><\/div>\n<div><strong>9.\u00a0socket-status.txt<\/strong><\/div>\n<div>State \u00a0 Description<\/div>\n<div>LISTEN \u00a0accepting connections<\/div>\n<div>ESTABLISHED \u00a0 \u00a0 connection up and passing data<\/div>\n<div>SYN_SENT \u00a0 \u00a0 \u00a0 \u00a0TCP; session has been requested by us; waiting for reply from remote endpoint<\/div>\n<div>SYN_RECV \u00a0 \u00a0 \u00a0 \u00a0TCP; session has been requested by a remote endpoint for a socket on which we were listening<\/div>\n<div>LAST_ACK \u00a0 \u00a0 \u00a0 \u00a0TCP; our socket is closed; remote endpoint has also shut down; we are waiting for a final acknowledgement<\/div>\n<div>CLOSE_WAIT \u00a0 \u00a0 \u00a0TCP; remote endpoint has shut down; the kernel is waiting for the application to close the socket<\/div>\n<div>TIME_WAIT \u00a0 \u00a0 \u00a0 TCP; socket is waiting after closing for any packets left on the network<\/div>\n<div>CLOSED \u00a0socket is not being used (FIXME. What does mean?)<\/div>\n<div>CLOSING TCP; our socket is shut down; remote endpoint is shut down; not all data has been sent<\/div>\n<div>FIN_WAIT1 \u00a0 \u00a0 \u00a0 TCP; our socket has closed; we are in the process of tearing down the connection<\/div>\n<div>FIN_WAIT2 \u00a0 \u00a0 \u00a0 TCP; the connection has been closed; our socket is waiting for the remote endpoint to shut down<\/div>\n<div><\/div>\n<div><strong>10. ssh setup<\/strong><\/div>\n<div>\n<div>ssh-keygen generate 2 files which go hand-in-hand. The private key file (id_rsa) should\u00a0reside on my $HOME\/.ssh, in order to encrypt things; the putlib key file(id_rsa.pub)\u00a0should be appended in remote host at its $HOME\/.ssh\/authorized_keys file.<\/div>\n<div><\/div>\n<div>When I ssh to remote host, remote host&#8217;s daemon (sshd) will decrypt my logon\u00a0using the public key from $HOME\/.ssh\/authorized_keys file. This will allow me<\/div>\n<div>to logon without prompting for password.<\/div>\n<\/div>\n<div>\n<div>This is the logic:<\/div>\n<div>1)ssh remoteHost<\/div>\n<div>2)ssh look at $HOME\/.ssh\/id_rsa and also $HOME\/.ssh\/known_hosts<\/div>\n<div>\u00a0 create request and send to remoteHost<\/div>\n<div>3)remoteHost will look at its $HOME\/.ssh\/authorized_keys and if it does see one entry<\/div>\n<div>\u00a0 which is able to decipher the request, connect is OK<\/div>\n<div>4)ssh on the originating host will put an entry to $HOME\/.ssh\/known_hosts if it&#8217;s not there<\/div>\n<\/div>\n<div><\/div>\n<div><strong>11.\u00a0ucbps<\/strong><\/div>\n<div>#Long form of ps command<\/div>\n<div>\/usr\/ucb\/ps -auxww<\/div>\n<div><\/div>\n<div><strong>12.\u00a0who_did_what<\/strong><\/div>\n<div>#!\/bin\/ksh<\/div>\n<div>#<\/div>\n<div>#####<\/div>\n<div><\/div>\n<div>STRING1=$1<\/div>\n<div>STRING2=$2<\/div>\n<div>STDOUT=\/tmp\/stdout.out<\/div>\n<div><\/div>\n<div>echo &#8220;&#8221; &gt; $STDOUT<\/div>\n<div>cd $HOME<\/div>\n<div>#for i in `ls $HOME\/.history.[a-z]*`<\/div>\n<div>for i in `ls .history.[a-z]*`<\/div>\n<div>do<\/div>\n<div>\u00a0 if (( `strings $i | grep -i $STRING1 | grep -i $STRING2 | wc -l` &gt; &#8220;0&#8221; ))<\/div>\n<div>\u00a0 then<\/div>\n<div>\u00a0 \u00a0 echo &#8221; ## `ls $i` ## &#8221; \u00a0# &gt;&gt; $STDOUT<\/div>\n<div>\u00a0 \u00a0 strings $i | grep -i $STRING1 | grep -i $STRING2 # &gt;&gt; $STDOUT<\/div>\n<div>\u00a0 fi<\/div>\n<div>done<\/div>\n<div><\/div>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>awk examples <\/p>\n<p style=\"color: #000000;\"> cat 03092608.LOG|grep XYZ|awk -F&#8221;.&#8221; &#8216;{print $2}&#8217; |sort|awk &#8216;{tot += 1 } $1 != prev {print prev,tot;tot=0;prev=$1}END{print prev,tot;tot=0;prev=$1}&#8217;<\/p>\n<p> adhocs <\/p>\n<p>1. find_processes_using_ipcs.ksh<\/p>\n<p>&nbsp;<\/p>\n<p>ipcs -bop|grep `whoami`|awk &#8216;{print $10}&#8217;|egrep -v &#8220;^0|^$&#8221;|sort -u|awk &#8216;{print &#8220;ps -ef|grep &#8220;, $1}&#8217;|ksh<\/p>\n<p> 2. iostat iostat -xn 4 30 3. jmap jmap -heap:format=b &lt;pid&gt; 4. monitorMemLeaking if [ $# [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3472"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3472"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3472\/revisions"}],"predecessor-version":[{"id":3473,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/3472\/revisions\/3473"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}