{"id":2188,"date":"2017-12-07T12:52:08","date_gmt":"2017-12-07T12:52:08","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2188"},"modified":"2020-01-09T09:45:38","modified_gmt":"2020-01-09T09:45:38","slug":"how-to-check-ssh-is-working-with-public-key-private-key-2","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/how-to-check-ssh-is-working-with-public-key-private-key-2\/","title":{"rendered":"How to check ssh is working with public key \/ private key"},"content":{"rendered":"<p><strong>rajeshkumar created the topic: How to check ssh is working with public key \/ private key<\/strong><br \/>\nCode &#8211; ssh serverhostname -o &#8216;BatchMode=yes&#8217; -o &#8216;ConnectionAttempts=1&#8217; true<\/p>\n<p>Every time a ssh attempt fails, it rights a string &#8220;Failed&#8221; to the \/var\/log\/secure.<br \/>\nMaybe you can just count that number, and if it goes above a threshold you can alert. Obviously you will need some more logic to check for the day to get your full requirement.<\/p>\n<p><code>*** this is bash code ***<br \/>\n[root@jralph-linux jralph]# cat check_ssh.sh<br \/>\n#!\/bin\/bash<\/p>\n<p>COUNT=$(tr -s ' ' '\\n' < \/var\/log\/secure | grep -c 'Failed')\n \nif [ $COUNT -ge \"1\" ]\nthen\n    echo \"Failed SSH $COUNT\"\n    echo \"We have some ssh failures\"\nelse\n    echo \"Things seem to be ok\"\nfi<\/code><\/p>\n<p>[root@jralph-linux jralph]# .\/check_ssh.sh<br \/>\nFailed SSH 2<br \/>\nWe have some ssh failures<br \/>\nRegards,<br \/>\nRajesh Kumar<br \/>\nTwitt me @ <a href=\"http:\/\/twitter.com\/RajeshKumarIn\" target=\"_blank\" rel=\"noopener\">twitter.com\/RajeshKumarIn<\/a><\/p>\n<p><strong>rajeshkumar replied the topic: How to check ssh is working with public key \/ private key<\/strong><br \/>\nHow to check if any errors occurred during ssh?<br \/>\n#!\/bin\/bash<br \/>\nresult=`ssh myapp 'mkdir \/some\/dir' 2>&1`<br \/>\nif -n $result; then<br \/>\necho \"the following error occurred: $result\"<br \/>\nfi<br \/>\nRegards,<br \/>\nRajesh Kumar<br \/>\nTwitt me @ <a href=\"http:\/\/twitter.com\/RajeshKumarIn\" target=\"_blank\" rel=\"noopener\">twitter.com\/RajeshKumarIn<\/a><\/p>\n<p><strong>rajeshkumar replied the topic: How to check ssh is working with public key \/ private key<\/strong><br \/>\nHow to check if ssh works without connecting to server?<br \/>\nsu -c \"ssh oracle@$MY_IP -o 'BatchMode=yes' -o 'ConnectionAttempts=1' true\" oracle<br \/>\nreturnCode=$?<\/p>\n<p>echo \"$returnCode\"<br \/>\nif [ $returnCode != 0 ]<br \/>\nthen<br \/>\necho \"Configuration is not valid\"<br \/>\nreturn 1;<br \/>\nelse<br \/>\necho \"Configuration is valid\"<br \/>\nreturn 0;<br \/>\nfi<br \/>\nRegards,<br \/>\nRajesh Kumar<br \/>\nTwitt me @ <a href=\"http:\/\/twitter.com\/RajeshKumarIn\" target=\"_blank\" rel=\"noopener\">twitter.com\/RajeshKumarIn<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>rajeshkumar created the topic: How to check ssh is working with public key \/ private key Code &#8211; ssh serverhostname -o &#8216;BatchMode=yes&#8217; -o &#8216;ConnectionAttempts=1&#8217; true Every time a ssh attempt&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[454],"tags":[],"class_list":["post-2188","post","type-post","status-publish","format-standard","hentry","category-shell-script"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=2188"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2188\/revisions"}],"predecessor-version":[{"id":2189,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2188\/revisions\/2189"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}