{"id":300,"date":"2009-09-07T02:26:20","date_gmt":"2009-09-07T07:26:20","guid":{"rendered":"http:\/\/yourlinuxguy.com\/?p=300"},"modified":"2009-09-07T13:00:28","modified_gmt":"2009-09-07T18:00:28","slug":"how-to-automate-scripted-commands-to-a-cisco-asa-via-ssh","status":"publish","type":"post","link":"https:\/\/yourLinuxGuy.com\/?p=300","title":{"rendered":"How to automate scripted commands to a Cisco ASA via ssh"},"content":{"rendered":"<p>The premise *seemed* simple enough&#8230;\u00a0 I just needed to send 4 or 5 commands to a Cisco ASA from a CentOS box in a cron job.\u00a0 I mean, people write scripts all the time, and use <code>ssh<\/code> keys to automate those scripts against remote systems, right?\u00a0 I do it all the time with Linux boxes, right?\u00a0 Well&#8230;<\/p>\n<p>I quickly discovered that there are two huge things that stop this standard practice from working with an ASA.<\/p>\n<ol>\n<li>The ASA does not understand semi-colons as replacements for returns.\u00a0 This means you cannot use a standard single-line-style command in your ssh call.\u00a0 each command must be sent with a <em>real<\/em> return.<\/li>\n<li>Though the ASA can make a set of keys to use <em>with<\/em> ssh, it cannot *store* any <em>other<\/em> keys for remote hosts, as needed when using key-based <code>ssh<\/code> from a remote linux box.\u00a0 So, no key-based <code>ssh<\/code>; it must be password-only.\u00a0 And there&#8217;s no way to provide the password to <code>ssh<\/code> in advance.<\/li>\n<\/ol>\n<p>So what now?\u00a0 Well, I worked on this one for a while&#8230; I almost gave up on it&#8230;\u00a0 almost&#8230;<\/p>\n<p>&#8230;until I stumbled across the &#8220;<code>expect<\/code>&#8221; program one day.\u00a0 While it appears to have been originally designed for use with modems, it solved my problem perfectly.\u00a0 It acts as a wrapper in a way, almost like a sub-shell, in which you stuff some instructions and your commands.\u00a0 Since the virtual sub-shell has control, it runs your commands and listens for prompts popping up in STDOUT, and can respond to them.\u00a0 Ah-hah!\u00a0 A way to capture the password prompt with ssh!<\/p>\n<p>Cutting to the chase, as I worked through it, I discovered that the <code>expect<\/code> program could not handle the ASA&#8217;s strange behavior on its own.\u00a0 Since no matter how I tried to separate out the commands that I was trying to stuff into my ASA via expect, I could see in the debug that <code>expect<\/code> just kept putting them back together, separated by semi-colons (which, as I mentioned, does not work with the ASA).\u00a0 The good news, is that after some troubleshooting, I came to the conclusion that the only way around that was to put my actual session commands into a sub-script, and call that sub-script from <code>expect<\/code>.\u00a0 The logic looks like this:<\/p>\n<p style=\"padding-left: 30px;\"><code>crontab<\/code> &#8211;&gt;\u00a0 <code>expectScript.sh<\/code> &#8211;&gt;\u00a0 <code>commandScript.sh<\/code><\/p>\n<p>Is all this making sense so far?<\/p>\n<p>So anyway, here are the mechanics of it all, and hopefully it will become more clear:<\/p>\n<p>The contents of the root <code>crontab<\/code>:<\/p>\n<p style=\"padding-left: 30px;\"><code>6 *\/8 * * * \/home\/jpavlov\/expectScript.sh<\/code><\/p>\n<p>The contents of <code>\/home\/jpavlov\/expectScript.sh<\/code>:<\/p>\n<p style=\"padding-left: 30px;\"><code>#!\/bin\/bash<br \/>\nPASSWORD=\"asaSshPassword\"<br \/>\nexpect -d -c \"set timeout -1;\\<br \/>\nspawn \/home\/jpavlov\/commandScript.sh;\\<br \/>\nmatch_max 100000;\\<br \/>\nexpect *password:*;\\<br \/>\nsend -- $PASSWORD\\r;\\<br \/>\ninteract;\"<\/code><\/p>\n<p>The contents of <code>\/home\/jpavlov\/commandScript.sh<\/code>:<\/p>\n<p style=\"padding-left: 30px;\"><code>ssh routerUser@192.168.1.1 &lt;&lt; EOF<br \/>\nen<br \/>\nrouterEnablePassword<br \/>\nclear crypto ipsec sa peer 192.168.66.6<br \/>\nexit<br \/>\nexit<br \/>\nEOF<\/code><\/p>\n<p>So, what do you think?\u00a0 It works great for me.\u00a0 Honestly, there&#8217;s too much here to go into much detail about in one post, but I want to pass along a couple random-but-related thoughts:<\/p>\n<ol>\n<li>If you want more detail, just Google something like &#8220;<code>expect ssh<\/code>&#8220;, and you&#8217;ll find good stuff out there.\u00a0 Believe it or not, the <code>man<\/code> page is not the best place for this, since it seems to all be pointed toward handling modem requests and such.<\/li>\n<li>I tried using a series of &#8220;<code>spawn<\/code>&#8221; commands in the expect section for each of the session commands, and it works great when you do it against a Linux\/Unix destination, but not the ASA.\u00a0 That&#8217;s where, if you watch the debug with the &#8220;<code>-d<\/code>&#8220;, you&#8217;ll see that it is concatenating it all with semi-colons (as mentioned above).<\/li>\n<li>Be careful with the &#8220;<code>expect<\/code>&#8221; <em>filter<\/em> inside the &#8220;<code>expect<\/code>&#8221; <em>command set<\/em>.\u00a0 The filter is reg-ex aware and case-sensitive.\u00a0 Make sure your filter matches, or it won&#8217;t work, obviously.<\/li>\n<\/ol>\n<p>Just make sure you do a <code>chmod 700<\/code> on the two scripts to keep it away from prying eyes.\u00a0 You really shouldn&#8217;t do this on multi-use servers, but is less of a concern on a restricted box.<\/p>\n<p>I hope that helps you!<\/p>\n<p>\ud83d\ude42<\/p>\n<p><span style=\"color: #888888;\">And for all you that didn&#8217;t read the post clearly and are going to rave that I should use ssh keys (I just know you will)&#8230;\u00a0 I&#8217;ll spare you the embarrassment by removing your post when you post (and you will)&#8230;<\/span><\/p>\n<p><span style=\"color: #888888;\">And for all you that insist that this method is insecure because the passwords are stored in a file&#8230; I assure you, my CentOS box is accessed by only administrators, and really, only me&#8230;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The premise *seemed* simple enough&#8230;\u00a0 I just needed to send 4 or 5 commands to a Cisco ASA from a CentOS box in a cron job.\u00a0 I mean, people write scripts all the time, and use ssh keys to automate&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/yourLinuxGuy.com\/?p=300\">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,77,54,75],"tags":[],"class_list":["post-300","post","type-post","status-publish","format-standard","hentry","category-advanced","category-asa","category-centos","category-ssh"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pnjn1-4Q","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/300","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=300"}],"version-history":[{"count":5,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/300\/revisions"}],"predecessor-version":[{"id":305,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/300\/revisions\/305"}],"wp:attachment":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}