{"id":598,"date":"2012-09-25T23:10:15","date_gmt":"2012-09-25T23:10:15","guid":{"rendered":"https:\/\/trouble.org\/?p=598"},"modified":"2012-09-25T23:10:15","modified_gmt":"2012-09-25T23:10:15","slug":"lsof-lite-iiiii","status":"publish","type":"post","link":"https:\/\/trouble.org\/?p=598","title":{"rendered":"lsof lite (II\/III)"},"content":{"rendered":"<p>Here&#8217;s one that looks up processes that have a file open&#8230; well, actually, more like a file expression; &#8220;foo&#8221; would match &#8220;\/bar\/foo&#8221; and &#8220;\/foo\/bar&#8221; (by intent), so use full paths if you&#8217;re not feeling frisky.  And yes&#8230; busybox really does have that many duplicate processes with that file open&#8230;.<\/p>\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">[WPCM450 \/tmp]$ .\/lsof-pid-on-file.sh NVRAM_PrivateStorage00.dat<br \/>\n\/bin\/fullfw<br \/>\n&nbsp; &nbsp; &nbsp;\/flash\/data0\/BMC_Data\/NVRAM_PrivateStorage00.dat<br \/>\n\/bin\/fullfw<br \/>\n&nbsp; &nbsp; &nbsp;\/flash\/data0\/BMC_Data\/NVRAM_PrivateStorage00.dat<br \/>\n\/bin\/fullfw<br \/>\n&nbsp; &nbsp; &nbsp;\/flash\/data0\/BMC_Data\/NVRAM_PrivateStorage00.dat<br \/>\n\/bin\/fullfw<br \/>\n&nbsp; &nbsp; &nbsp;\/flash\/data0\/BMC_Data\/NVRAM_PrivateStorage00.dat<br \/>\n\/bin\/fullfw<br \/>\n&nbsp; &nbsp; &nbsp;\/flash\/data0\/BMC_Data\/NVRAM_PrivateStorage00.dat<br \/>\n[...]<br \/>\npid #'s that use NVRAM_PrivateStorage00.dat: &nbsp;419 430 431 432 437 448 449 450 451 452 453 454 455 456 457 458 459 460 461 464 465<br \/>\npid names that use NVRAM_PrivateStorage00.dat: \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw \/bin\/fullfw<\/div><\/div>\n<p>Script.<\/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=\"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&nbsp;<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> can<span class=\"co3\">\\'<\/span>t <span class=\"kw2\">find<\/span> process <span class=\"kw1\">for<\/span> inode <span class=\"re1\">$inode<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw3\">echo<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">else<\/span><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=\"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\">sed<\/span> <span class=\"re5\">-e<\/span> <span class=\"st_h\">'s\/\\o000\/ \/g'<\/span> <span class=\"re5\">-e<\/span> <span class=\"st_h\">'s\/ *$\/\/'<\/span> <span class=\"re5\">-e<\/span> <span class=\"st_h\">'s\/^ *\/\/'<\/span> <span class=\"sy0\">\/<\/span>proc<span class=\"sy0\">\/<\/span><span class=\"re1\">$p<\/span><span class=\"sy0\">\/<\/span>cmdline<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; &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=\"es1\">\\t<\/span><span class=\"es2\">$p<\/span> = <span class=\"es2\">$name<\/span>&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw1\">done<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class=\"kw3\">echo<\/span><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<br \/>\n&nbsp; &nbsp;<span class=\"kw1\">done<\/span><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s one that looks up processes that have a file open&#8230; well, actually, more like a file expression; &#8220;foo&#8221; would match &#8220;\/bar\/foo&#8221; and &#8220;\/foo\/bar&#8221; (by intent), so use full paths if you&#8217;re not feeling frisky. And yes&#8230; busybox really does have that many duplicate processes with that file open&#8230;. [WPCM450 \/tmp]$ .\/lsof-pid-on-file.sh NVRAM_PrivateStorage00.dat \/bin\/fullfw &nbsp; [&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-598","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\/598","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=598"}],"version-history":[{"count":3,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions"}],"predecessor-version":[{"id":617,"href":"https:\/\/trouble.org\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions\/617"}],"wp:attachment":[{"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trouble.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}