En algunos casos, necesitamos ejecutar comandos en un servidor remoto.
Ejemplos:
Obtener información del disco
Obtener todo el proceso en ejecución
...
Sintaxis para ejecutar comandos en un servidor remoto:
1 | ssh [USER@][HOSTNAME-OR-IP] command |
Opciones completas:
1 2 3 4 5 6 7 8 | [root@tutorialspots ~]# ssh usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] [user@]hostname [command] |
Ejemplo 1: obtener información del disco
1 | ssh root@tutorialspots.com df -h |
Resultado:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@tutorialspots ~]# ssh root@tutorialspots.com df -h The authenticity of host 'tutorialspots.com (51.91.115.214)' can't be established. ECDSA key fingerprint is SHA256:yxg/X3UwKkrQWI8LoQdmerXPgr6IMM2mc2XxRpPpslk. ECDSA key fingerprint is MD5:86:09:6d:c1:05:1f:e0:e7:04:6c:6f:33:8d:7a:9e:33. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'tutorialspots.com' (ECDSA) to the list of known hosts. root@tutorialspots.com's password: Filesystem Size Used Avail Use% Mounted on devtmpfs 16G 0 16G 0% /dev tmpfs 16G 0 16G 0% /dev/shm tmpfs 16G 556K 16G 1% /run tmpfs 16G 0 16G 0% /sys/fs/cgroup /dev/md2 5.4T 1.6T 3.6T 31% / /dev/md1 487M 148M 314M 32% /boot tmpfs 3.2G 0 3.2G 0% /run/user/0 |
Para omitir la verificación estricta de HostKey:
1 | ssh -o StrictHostKeyChecking=no root@tutorialspots.com df -h |
Ejemplo 2: comprobar el promedio de carga
1 2 3 | [root@tutorialspots ~]# ssh root@tutorialspots.com cat /proc/loadavg root@tutorialspots.com's password: 0.75 0.52 0.58 1/296 15713 |
Si desea utilizar la contraseña en la línea de comandos, debe instalar sshpass:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 dieciséis 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | [root@tutorialspots ~]# yum install -y sshpass Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile epel/x86_64/metalink | 13 kB 00:00 * base: linux.darkpenguin.net * epel: mirror.imt-systems.com * extras: mirror.checkdomain.de * updates: mirror.fra10.de.leaseweb.net base | 3.6 kB 00:00 epel | 4.7 kB 00:00 extras | 2.9 kB 00:00 ius | 1.3 kB 00:00 nodesource | 2.5 kB 00:00 updates | 2.9 kB 00:00 (1/3): updates/7/x86_64/primary_db | 2.9 MB 00:00 (2/3): epel/x86_64/primary_db | 6.8 MB 00:00 (3/3): epel/x86_64/updateinfo | 1.0 MB 00:00 Resolving Dependencies --> Running transaction check ---> Package sshpass.x86_64 0:1.06-2.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: sshpass x86_64 1.06-2.el7 extras 21 k Transaction Summary ================================================================================ Install 1 Package Total download size: 21 k Installed size: 38 k Downloading packages: sshpass-1.06-2.el7.x86_64.rpm | 21 kB 00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : sshpass-1.06-2.el7.x86_64 1/1 Verifying : sshpass-1.06-2.el7.x86_64 1/1 Installed: sshpass.x86_64 0:1.06-2.el7 Complete! |
Ejemplo 3: usar contraseña en la línea de comando
1 | sshpass -p yourpassword ssh -o StrictHostKeyChecking=no root@ip "free -m" |
Resultado:
1 2 3 4 | [root@tutorialspots ~]# sshpass -p yourpassword ssh -o StrictHostKeyChecking=no root@tutorialspots.com 'free -m' total used free shared buff/cache available Mem: 31923 888 471 0 30563 30643 Swap: 32733 5 32728 |
Para ignorar esta advertencia:
1 | Warning: Permanently added '171.19.110.177' (ECDSA) to the list of known hosts. |
Siga el Ejemplo 4:
Ejemplo 4:
1 | ssh -o LogLevel=quiet user@ip_or_hostname |
Ejemplo completo:
1 | sshpass -p yourpass ssh -o StrictHostKeyChecking=no -o LogLevel=quiet root@137.213.62.215 "cat /root/ok/okdead.txt" > /root/ok/ok2.txt
|
0 Comentarios
Dejanos tu comentario para seguir mejorando!