{"id":1164,"date":"2017-02-05T11:58:39","date_gmt":"2017-02-05T16:58:39","guid":{"rendered":"http:\/\/yourLinuxGuy.com\/?p=1164"},"modified":"2017-02-05T11:58:39","modified_gmt":"2017-02-05T16:58:39","slug":"find-them-und-patch-them","status":"publish","type":"post","link":"https:\/\/yourLinuxGuy.com\/?p=1164","title":{"rendered":"Find them und patch them&#8230;"},"content":{"rendered":"<p>I want to start this post by pointing out that the title is like something you&#8217;d find in a classic German war movie.\u00a0 Except this time, it&#8217;s about servers.\u00a0 Anyway, I got this email from a reader:<\/p>\n<blockquote><p>Hi Linux Guy,<br \/>\nI have a project where I need to patch Linux systems \u2013 problem is that the client doesn\u2019t even know how or what versions of linux are out there in the environment.<\/p>\n<p>What would you recommend as a discovery method or tool to obtain the system information for linux systems?\u00a0 I recall that, back in my earlier days of IT and experimenting, I used a port-scanner to identify online systems in my neighborhood on the internet :S\u00a0 That, I believe, also identified operating system based on which ports are open, so I am thinking some sort of SNMP tool.<\/p>\n<p>Looking for your wisdom and guidance.<br \/>\n-Nick-<\/p><\/blockquote>\n<p>I appreciate your kind words!<\/p>\n<p>I think the answer you need is to start with NMAP.\u00a0 But the *question* before the answer is, &#8220;<em>Mr. or Mrs. Customer, do you mind me port-scanning machines in your infrastructure?<\/em>&#8221;\u00a0 Without approval in writing, scanning someone&#8217;s machines or subnets can get you in hot water &#8212; even legal trouble &#8212; if you aren&#8217;t careful.<\/p>\n<p>Of course, first you need to be sure you&#8217;re scanning for Linux machines.\u00a0 You can limit your scan to a simple set of addresses, if they know which addresses they would like scanned. If they don&#8217;t know, then you might have to do subnet scans, so make sure you get a complete list of subnets.\u00a0 Here&#8217;s an example of using <code>nmap<\/code> to quickly determine which addresses in a single subnet are worthy of scanning:<\/p>\n<pre>nmap -F 192.168.1.0\/24<\/pre>\n<p><em>Note: If you don&#8217;t like waiting for the command to return results, press the a key (such as space bar) to show real-time results while waiting.<\/em><\/p>\n<p>Then, you can limit the search down to a more refined list.\u00a0 And whether you started with a specific list of addresses or a subnet list, you can scan and learn roughly what they are via the OS signature best-guess with <code>nmap<\/code>. \u00a0While you cannot completely trust the certainty of the <code>nmap<\/code> OS fingerprint, it&#8217;s certainly good enough to let you know if the machines are truly Linux. \u00a0Of course, they may be port-blocked too, or behind other firewalls, so you can&#8217;t necessarily trust that your results will be all-inclusive; but you can reasonably trust the positives that you get.\u00a0 From our above example, image we got four positive responses, so now we probe them further in this example:<\/p>\n<pre>nmap -O --osscan-guess 192.168.1.2,4,9,13,14<\/pre>\n<p><em>By the way, there&#8217;s <a href=\"https:\/\/www.cyberciti.biz\/networking\/nmap-command-examples-tutorials\/\">a great nmap tutorial with other quick tips here<\/a>.<\/em><\/p>\n<p>Imagine that two of those four turned out to be Linux in the field &#8220;<code>Aggressive OS guesses<\/code>&#8220;, &#8220;<code>OS CPE<\/code>&#8220;, or &#8220;<code>OS Details<\/code>&#8220;.\u00a0 Next, you can filter your list down to only the Linux systems, and you can run a loop to <code>ssh<\/code> and get the values of either a.) the contents of the modern distribution identifier file (<code>\/etc\/issue<\/code> or <code>\/etc\/os-release<\/code> for newer distros), or b.) whether or not an older-style distribution-specific identifier file exists and what&#8217;s in it (for example, <code>\/etc\/SuSE-release<\/code>).<\/p>\n<p>Of course, in order to do that loop ssh, you will need to know the credential to use to access the system. It matters whether they have key-based <code>ssh<\/code> or password-based, so you&#8217;ve have to adjust your scripting for that.\u00a0 But to give you the main idea, I whipped up this script that uses a all-systems user ID called &#8220;linuxadmin&#8221;, but you&#8217;ll have to type the password for each system unless the ssh keys are already set up.\u00a0 For example:<\/p>\n<pre>#!\/bin\/bash\r\n# Enter your refined list of Linux machines to check, separated by spaces\r\nHOSTLIST=\"192.168.1.2 192.168.1.13\"\r\n# Enter the username of your LInux management user ID\r\nUSERID=\"linuxadmin\"\r\n# Declre the output file\r\nOUTPUTFILE=\"\/tmp\/outputfile.txt\"\r\necho \"LINUX MACHINE LOCAL RELEASE FILE RESULTS\"&gt;$OUTPUTFILE\r\necho \"----------------------------------------\"&gt;&gt;$OUTPUTFILE\r\nfor HOST in $HOSTLIST\r\ndo\r\n  # Try on port custom port\r\n  RESULT=`ssh $USERID@$HOST -p 2222 \"grep PRETTY_NAME \/etc\/os-release || grep SUSE \/etc\/SuSE-release\"`\r\n  if [ \"XX$RESULT\" = \"XX\" ]; then\r\n    # If no result, try on regular old port\r\n    RESULT=`ssh $USERID@$HOST \"grep PRETTY_NAME \/etc\/os-release || grep SUSE \/etc\/SuSE-release\"`\r\n  fi\r\n  # Finally, we write the results to the file\r\n  echo \"For $HOST: $RESULT\"&gt;&gt;$OUTPUTFILE\r\ndone\r\necho \"\"\r\necho \"Displaying contents of the output file at $OUTPUTFILE:\"\r\necho \"\"\r\ncat $OUTPUTFILE<\/pre>\n<p>That should do it!\u00a0 This scrpt could certainly be improved, but I&#8217;d have to know more about the situation once you do some discovery.<\/p>\n<p>Good luck out there!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I want to start this post by pointing out that the title is like something you&#8217;d find in a classic German war movie.\u00a0 Except this time, it&#8217;s about servers.\u00a0 Anyway, I got this email from a reader: Hi Linux Guy,&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/yourLinuxGuy.com\/?p=1164\">Read more<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[12,67,47,92],"tags":[],"class_list":["post-1164","post","type-post","status-publish","format-standard","hentry","category-advanced","category-bash","category-linuxgeneral","category-nmap"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pnjn1-iM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/1164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1164"}],"version-history":[{"count":2,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/1164\/revisions"}],"predecessor-version":[{"id":1166,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/1164\/revisions\/1166"}],"wp:attachment":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}