{"id":592,"date":"2011-01-02T16:43:07","date_gmt":"2011-01-02T15:43:07","guid":{"rendered":"http:\/\/blog.unelink.es\/?p=592"},"modified":"2019-05-16T10:30:12","modified_gmt":"2019-05-16T08:30:12","slug":"como-conectarse-por-ssh-a-traves-de-php","status":"publish","type":"post","link":"https:\/\/hosting.airetech.es\/blog\/wiki\/como-conectarse-por-ssh-a-traves-de-php\/","title":{"rendered":"C\u00f3mo conectarse por SSH a trav\u00e9s de PHP"},"content":{"rendered":"<p>Lo primero: <strong>\u00bfPara que sirve conectarse por ssh a una m\u00e1quina?<\/strong><\/p>\n<p>La respuesta es f\u00e1cil, para ejecutar comandos en m\u00e1quinas remotas. Y como cuales?<\/p>\n<p>Por ejemplo, imaginate que quieres averiguar el tiempo que lleva un servidor o tu propia m\u00e1quina (en linux) encendida. Para eso tenemos el comando <strong>uptime. <\/strong>Entonces desde php escribiriamos esto:<\/p>\n<pre>ssh2_exec($ssh, \"uptime\");<\/pre>\n<p>Lo que nos devolver\u00eda lo siguiente:<\/p>\n<pre>16:37:31 up 2 days,\u00a0 3:59,\u00a0 2 users,\u00a0 load average: 0.18, 0.20, 0.21<\/pre>\n<p>Para hacerlo bien a trav\u00e9s de PHP hay que escribir las siguientes instrucciones:<\/p>\n<div>\n<div><code> &lt;?php<\/code><\/div>\n<div><code>\/\/Conexi\u00f3n al servidor d\u00f3nde \"shell.example.com\" es el servidoy y 22 el puerto. $connection\u00a0=\u00a0ssh2_connect('shell.example.com',\u00a022);<\/code><\/div>\n<div><code>\/\/Una vez conectados hay que autenticarse d\u00f3nde $conecction es el manejador de la conexi\u00f3n y los otros valores son el usuario y la contrase\u00f1a. ssh2_auth_password($connection,\u00a0'username',\u00a0'password'); <\/code><\/div>\n<div><code>\/\/Por \u00faltimo ejecutamos el c\u00f3digo en la m\u00e1quina remota, d\u00f3nde el primer parametro es el manejador de la conexi\u00f3n y el segundo el parametro en cuesti\u00f3n $stream\u00a0=\u00a0ssh2_exec($connection,\u00a0'\/usr\/local\/bin\/php\u00a0-i');<\/code><\/div>\n<div><code>\/\/Todo esto se guardar\u00eda en la variable $stream que podemos mostrar de la siguiente forma<\/code><\/div>\n<div><code>echo \"&lt;pre&gt;;<\/code><\/div>\n<div><code>print_r($stream);<\/code><\/div>\n<div><code>echo \"&lt;\/pre&gt;\"; ?&gt; <\/code><\/div>\n<\/div>\n<p>M\u00e1s informaci\u00f3n acerca de la funci\u00f3n ssh <a href=\"http:\/\/php.net\/manual\/es\/ref.ssh2.php\" target=\"_blank\" rel=\"noopener noreferrer\">aqu\u00ed<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"Lo primero: \u00bfPara que sirve conectarse por ssh a una m\u00e1quina? La respuesta es f\u00e1cil, para ejecutar comandos en m\u00e1quinas remotas. Y como cuales? Por ejemplo, imaginate que quieres averiguar el tiempo que lleva un servidor o tu propia m\u00e1quina (en linux) encendida. Para eso tenemos el comando uptime. Entonces desde php escribiriamos esto: ssh2_exec($ssh, \"uptime\"); Lo que nos devolver\u00eda&nbsp;<a href=\"https:\/\/hosting.airetech.es\/blog\/wiki\/como-conectarse-por-ssh-a-traves-de-php\/\" class=\"read-more\">Seguir leyendo<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[39,349,6],"tags":[47,46],"class_list":["post-592","post","type-post","status-publish","format-standard","hentry","category-php","category-ssh","category-wiki","tag-conectar-a-maquina-desde-php","tag-ssh-desde-php","cat-39-id","cat-349-id","cat-6-id"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/posts\/592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/comments?post=592"}],"version-history":[{"count":12,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/posts\/592\/revisions"}],"predecessor-version":[{"id":8390,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/posts\/592\/revisions\/8390"}],"wp:attachment":[{"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/media?parent=592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/categories?post=592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hosting.airetech.es\/blog\/wp-json\/wp\/v2\/tags?post=592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}