{"id":415,"date":"2009-09-02T20:47:00","date_gmt":"2009-09-02T23:47:00","guid":{"rendered":"http:\/\/wordpress.matbra.com\/?p=415"},"modified":"2011-05-14T12:10:56","modified_gmt":"2011-05-14T15:10:56","slug":"conectar-ao-ssh-pelo-python","status":"publish","type":"post","link":"https:\/\/wordpress.matbra.com\/en\/2009\/09\/02\/conectar-ao-ssh-pelo-python\/","title":{"rendered":"Connect to SSH using Python"},"content":{"rendered":"<p>Good Night,<\/p>\n<p>If you want to execute commands in console using python you can use the pexpect library. You can get it in <a href=\"http:\/\/sourceforge.net\/projects\/pexpect\/\" target=\"_blank\">http:\/\/sourceforge.net\/projects\/pexpect\/<\/a>. To install it use: <\/p>\n<blockquote><p>\n$ sudo python setup.py install\n<\/p><\/blockquote>\n<p>After install it, you can have fun. I did a small script to test it.<\/p>\n<p>[code lang=&#8221;python&#8221;]import pexpect<\/p>\n<p>class SSH():<br \/>\ndef connect(self):<br \/>\nx = True<br \/>\nsshConnection = pexpect.spawn(&#8216;ssh -l matheus -p 22 192.168.0.254&#8217;)<br \/>\nsshConnection.expect(&#8216;matheus@192.168.0.254\\&#8217;s password:&#8217;)<br \/>\nsshConnection.sendline(&#8216;password&#8217;)<br \/>\nwhile True:<br \/>\nprint sshConnection.readline();<br \/>\nif x is True:<br \/>\nsshConnection.sendline(&#8216;uptime&#8217;)<br \/>\nx = False<br \/>\nsshConnection.readline();<\/p>\n<p>s = SSH()<br \/>\ns.connect()[\/code]<\/p>\n<p>If you have any problem try to use:<\/p>\n<blockquote><p>\n$ sudo apt-get install python-dev\n<\/p><\/blockquote>\n<p><a href='http:\/\/wordpress.matbra.com\/wp-content\/uploads\/exemplo-conectar-ssh-em-python.py'>Example connect to SSH using Python<\/a><\/p>\n<p>Matheus<\/p>","protected":false},"excerpt":{"rendered":"<p>Good Night, If you want to execute commands in console using python you can use the pexpect library. You can get it in http:\/\/sourceforge.net\/projects\/pexpect\/. To&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/wordpress.matbra.com\/en\/2009\/09\/02\/conectar-ao-ssh-pelo-python\/\">Continue reading<span class=\"screen-reader-text\">Connect to SSH using Python<\/span><\/a><\/div>\n","protected":false},"author":1,"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":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[156],"tags":[157,328,88],"class_list":["post-415","post","type-post","status-publish","format-standard","hentry","category-python","tag-pexpect","tag-python","tag-ssh","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/psjGE-6H","_links":{"self":[{"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/posts\/415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/comments?post=415"}],"version-history":[{"count":2,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/posts\/415\/revisions"}],"predecessor-version":[{"id":1137,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/posts\/415\/revisions\/1137"}],"wp:attachment":[{"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/media?parent=415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/categories?post=415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.matbra.com\/en\/wp-json\/wp\/v2\/tags?post=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}