{"id":596,"date":"2012-09-25T23:10:07","date_gmt":"2012-09-25T23:10:07","guid":{"rendered":"https:\/\/trouble.org\/?p=596"},"modified":"2012-09-25T23:10:07","modified_gmt":"2012-09-25T23:10:07","slug":"lsof-lite-iiii","status":"publish","type":"post","link":"https:\/\/trouble.org\/?p=596","title":{"rendered":"lsof lite I\/III"},"content":{"rendered":"<p>After beating on some really anemic linux installations that had&#8230; well, just about nothing installed (one didn&#8217;t have &#8220;tr&#8221;, one didn&#8217;t have &#8220;df&#8221;, etc&#8230; come on, that&#8217;s pretty sad ;)), I decided to start writing some shell scripts in very, very basic shell (you can do a lot with shell, awk, and sed!)<\/p>\n<p>Here&#8217;s one that while not perfect, at least seems to work (so far!) &#8211; I <3 <a href=\"http:\/\/people.freebsd.org\/~abe\/\" title=\"vic rulez\" target=\"_blank\">lsof<\/a>, but it turns out you can get a bit of its functionality via \/proc (plus it saved me from cursing more at the stupid people who made it so fucking difficult to make a cross compiled frickin&#8217; static binary.)  Here&#8217;s one that shows all the network connections and tries to show the pid associated with them.  I didn&#8217;t bother trying to make it really work, hoping that I&#8217;d stop fooling around with these damn things, but it was useful enough to post.  It currently also doesn&#8217;t distinguish on the interface, so ports open on 127.0.0.1, say, are listed.  That said, try doing this by hand, esp without netstat -p, lsof, nn, and all the other billion tools that do this  for you.  Bleah.<\/p>\n<p>Ouput looks something like:<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">[WPCM450 ~]$ .\/lsof-net-pid.sh<br \/>\ncollecting port data...<br \/>\nmachine is listening on:<br \/>\ntcp:&nbsp; &nbsp; &nbsp;8195 &nbsp; 1317 = \/usr\/local\/bin\/guiDataServer<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; 1391 = \/usr\/local\/bin\/guiDataServer<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; 1392 = \/usr\/local\/bin\/guiDataServer<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; 3925 = \/usr\/local\/bin\/guiDataServer<br \/>\ntcp:&nbsp; &nbsp; &nbsp;5988 &nbsp; 1162 = \/sbin\/sfcbd -d<br \/>\ntcp:&nbsp; &nbsp; &nbsp;22 1263 = \/sbin\/sshd -g 60<br \/>\ntcp:&nbsp; &nbsp; &nbsp;22 6536 = sshd: root@pts\/0<br \/>\n[...]<\/div><\/div>\n<p>Script below.  Blindingly fast on a real system, dog slow on a BMC, say :)  Should be sorted, should be not necessary, too.<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"overflow:auto;white-space:nowrap;height:800px;\"><div class=\"bash codecolorer\">:<br \/>\n<br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># busybox - find network ports being listened to by pid<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<br \/>\n<span class=\"re2\">fd<\/span>=<span class=\"st0\">&quot;\/proc\/*\/fd&quot;<\/span><br \/>\n<br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># list of all the stuff everyone is listening to<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;collecting port data...&quot;<\/span><br \/>\n<br \/>\n<span class=\"co0\"># slooow... jumping through more hoops than michael jordon.. ls will produce lines like:<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># [...]<\/span><br \/>\n<span class=\"co0\"># \/proc\/1261\/fd:<\/span><br \/>\n<span class=\"co0\"># \/proc\/1262\/fd:<\/span><br \/>\n<span class=\"co0\"># \/proc\/1263\/fd:<\/span><br \/>\n<span class=\"co0\"># &nbsp;0 lrwx------ &nbsp; &nbsp;1 root &nbsp; &nbsp; root &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 64 Sep 25 15:03 3 -&gt; socket:[4535]<\/span><br \/>\n<span class=\"co0\"># [...]<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># this will have matching pairs of pid's &amp; inodes of sockets<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"co0\"># grab the line before the match as the pid, since ls won't say what is going on<\/span><br \/>\n<span class=\"co0\"># and find won't work on this<\/span><br \/>\n<span class=\"co0\">#<\/span><br \/>\n<span class=\"re2\">all_sox<\/span>=<span class=\"sy0\">`<\/span><span class=\"kw2\">ls<\/span> <span class=\"re5\">-asl<\/span> <span class=\"sy0\">\/<\/span>proc<span class=\"sy0\">\/<\/span><span class=\"br0\">&#91;<\/span><span class=\"nu0\">0<\/span>-<span class=\"nu0\">9<\/span><span class=\"br0\">&#93;<\/span><span class=\"sy0\">*\/<\/span>fd <span class=\"nu0\">2<\/span><span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">\/<\/span>dev<span class=\"sy0\">\/<\/span>null <span class=\"sy0\">|<\/span> <span class=\"kw2\">egrep<\/span> <span class=\"st_h\">'socket:|fd'<\/span> <span class=\"sy0\">|<\/span> &nbsp;<span class=\"kw2\">awk<\/span> <span class=\"st_h\">'{ if (\/fd:\/) { split($1,fd,&quot;\/&quot;); } if (\/socket\/) print fd[3], $NF ; }'<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">sed<\/span> <span class=\"re5\">-e<\/span> <span class=\"st_h\">'s\/socket:\\[\/\/'<\/span> <span class=\"re5\">-e<\/span> <span class=\"st_h\">'s\/\\]\/\/'<\/span><span class=\"sy0\">`<\/span><br \/>\n<br \/>\n<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;machine is listening on:&quot;<\/span><br \/>\n<span class=\"kw2\">grep<\/span> <span class=\"re5\">-v<\/span> local_address <span class=\"sy0\">\/<\/span>proc<span class=\"sy0\">\/<\/span>net<span class=\"sy0\">\/<\/span>?<span class=\"br0\">&#91;<\/span><span class=\"kw3\">cd<\/span><span class=\"br0\">&#93;<\/span>p<span class=\"sy0\">*<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">awk<\/span> <span class=\"st_h\">'{print $1, $3, $11}'<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw1\">while<\/span> <span class=\"kw2\">read<\/span> proto port inode ; <span class=\"kw1\">do<\/span><br \/>\n&nbsp; &nbsp;<span class=\"kw1\">if<\/span> <span class=\"br0\">&#91;<\/span> <span class=\"st0\">&quot;X<span class=\"es2\">$proto<\/span>&quot;<\/span> <span class=\"sy0\">!<\/span>= <span class=\"st0\">&quot;X&quot;<\/span> <span class=\"br0\">&#93;<\/span> ; <span class=\"kw1\">then<\/span><br \/>\n&nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"co0\"># echo &quot;$proto, $port, $inode&quot;<\/span><br \/>\n&nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw3\">echo<\/span> <span class=\"re5\">-n<\/span> <span class=\"re1\">$proto<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">sed<\/span> <span class=\"re5\">-e<\/span> <span class=\"st_h\">'s@^.*net\/@@'<\/span> <span class=\"re5\">-e<\/span> <span class=\"st_h\">'s\/:\/\/'<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw3\">echo<\/span> <span class=\"re5\">-n<\/span> <span class=\"st0\">&quot;:&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"co0\">#<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"co0\"># more busybox hoop jumping... sometimes like ice skating with roller skates<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"co0\">#<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"co0\"># the amazing sed+ stuff courtesy of http:\/\/stackoverflow.com\/questions\/3675012\/hex-to-dec-con<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"co0\">#<\/span><br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw3\">echo<\/span> <span class=\"re1\">$port<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">awk<\/span> -F: <span class=\"st_h\">'{print $2}'<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">sed<\/span> <span class=\"st_h\">'s,\\(..\\)\\(..\\)\\(..\\)\\(..\\),\\4\\3\\2\\1,g'<\/span> <span class=\"sy0\">|<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw2\">read<\/span> hex; <span class=\"kw3\">echo<\/span> <span class=\"re5\">-en<\/span> <span class=\"st0\">&quot;<span class=\"es1\">\\t<\/span>&quot;<\/span> $<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#40;<\/span> 0x<span class=\"co1\">${hex}<\/span> <span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp;<span class=\"kw1\">if<\/span> <span class=\"br0\">&#91;<\/span> <span class=\"st0\">&quot;X<span class=\"es2\">$inode<\/span>&quot;<\/span> <span class=\"sy0\">!<\/span>= <span class=\"st0\">&quot;X&quot;<\/span> <span class=\"br0\">&#93;<\/span> ; <span class=\"kw1\">then<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re2\">pid<\/span>=<span class=\"sy0\">`<\/span><span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;<span class=\"es2\">$all_sox<\/span>&quot;<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">grep<\/span> <span class=\"st0\">&quot; <span class=\"es2\">$inode<\/span>&quot;<\/span><span class=\"sy0\">`<\/span><br \/>\n<br \/>\n<span class=\"co0\"># fd=`ls -asl \/proc\/[0-9]*\/fd 2&gt; \/dev\/null | egrep 'socket:\\[4535\\]|fd' | &nbsp;awk '{ if (\/fd:\/) { split($1,fd,&quot;\/&quot;); } if (\/socket\/) print &quot;FD: &quot;, fd[3]; }'`<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#91;<\/span> <span class=\"st0\">&quot;X<span class=\"es2\">$pid<\/span>&quot;<\/span> == <span class=\"st0\">&quot;X&quot;<\/span> <span class=\"br0\">&#93;<\/span> ; <span class=\"kw1\">then<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw3\">echo<\/span> <span class=\"re5\">-e<\/span> <span class=\"st0\">&quot;<span class=\"es1\">\\t<\/span>*** can't find process for inode <span class=\"es2\">$inode<\/span> ***&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">else<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"re2\">matching_pids<\/span>=<span class=\"sy0\">`<\/span><span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;<span class=\"es2\">$pid<\/span>&quot;<\/span> <span class=\"sy0\">|<\/span> <span class=\"kw2\">awk<\/span> <span class=\"st_h\">'{ pids=sprintf(&quot;%s %s&quot;, pids, $1);} END { print pids }'<\/span><span class=\"sy0\">`<\/span><br \/>\n&nbsp;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"co0\"># echo &quot;MATCHING: $matching_pids&quot;<\/span><br \/>\n&nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"re2\">spacing<\/span>=<span class=\"st0\">&quot;\\<span class=\"es1\">\\t<\/span>&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw1\">for<\/span> p <span class=\"kw1\">in<\/span> <span class=\"re1\">$matching_pids<\/span> ; <span class=\"kw1\">do<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#91;<\/span> <span class=\"re5\">-f<\/span> <span class=\"sy0\">\/<\/span>proc<span class=\"sy0\">\/<\/span><span class=\"re1\">$p<\/span><span class=\"sy0\">\/<\/span>cmdline <span class=\"br0\">&#93;<\/span> ; <span class=\"kw1\">then<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"re2\">name<\/span>=<span class=\"sy0\">`<\/span><span class=\"kw2\">tr<\/span> <span class=\"st_h\">'\\000'<\/span> <span class=\"st0\">&quot; &quot;<\/span> <span class=\"sy0\">&lt;<\/span> &nbsp;<span class=\"sy0\">\/<\/span>proc<span class=\"sy0\">\/<\/span><span class=\"re1\">$p<\/span><span class=\"sy0\">\/<\/span>cmdline <span class=\"sy0\">|<\/span> <span class=\"kw2\">sed<\/span> <span class=\"st_h\">'s\/ *$\/\/'<\/span> <span class=\"sy0\">`<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">else<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"re2\">name<\/span>=<span class=\"st0\">&quot;?&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">fi<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">echo<\/span> <span class=\"re5\">-e<\/span> <span class=\"st0\">&quot;<span class=\"es2\">$spacing<\/span><span class=\"es2\">$p<\/span> = <span class=\"es2\">$name<\/span>&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re2\">spacing<\/span>=<span class=\"st0\">&quot;\\<span class=\"es1\">\\t<\/span>\\<span class=\"es1\">\\t<\/span>&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw1\">done<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw1\">fi<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"kw1\">else<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;no inode found associated with port <span class=\"es2\">$port<\/span>&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"kw1\">fi<\/span><br \/>\n&nbsp; &nbsp;<span class=\"kw1\">fi<\/span><br \/>\n&nbsp; &nbsp;<span class=\"kw1\">done<\/span><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>After beating on some really anemic linux installations that had&#8230; well, just about nothing installed (one didn&#8217;t have &#8220;tr&#8221;, one didn&#8217;t have &#8220;df&#8221;, etc&#8230; come on, that&#8217;s pretty sad ;)), I decided to start writing some shell scripts in very, very basic shell (you can do a lot with shell, awk, and sed!) Here&#8217;s one [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,154,4,6],"tags":[158,157,334,155,156],"class_list":["post-596","post","type-post","status-publish","format-standard","hentry","category-code","category-ipmi-2","category-security","category-tech","tag-bmc","tag-busybox","tag-hack","tag-lsof","tag-shell"],"_links":{"self":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=596"}],"version-history":[{"count":6,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/596\/revisions"}],"predecessor-version":[{"id":616,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/596\/revisions\/616"}],"wp:attachment":[{"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}