In Linux, executing commands or scripts on a remote host using SSH non-interactive remote command execution scripts can help us quickly complete some tasks. For example
Environment, and query specific keywords in the log files on each node. Www.2cto.com
To remotely execute commands through SSH, you must first establish a trust relationship between hosts. Otherwise, SSH
The command prompts you to enter the password of the remote host, which leads to the interaction between the system and people, which is not conducive to script automation. Create
The following describes how to use the trust relationship between hosts: assume that we have two hosts. The host names are linuxa and linuxb. First, run the following command on linuxa to generate
The public key and private key file of the Host: After the ssh-keygen-t rsa command is executed, the directory ~ /. Ssh contains two files: idrsa and idrsa. pub. Where idrsa. pub is
Public key file. Append the content of this file to the peer host linuxb ~ /. The authorizedkeys file under the ssh directory. If
The file does not exist. You can create it yourself. The following is an example of an id_rsa.pub file: www.2cto.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtbW/example +
Ck6CB09l4BoFujpI0 + omL4Nptxk forward + Q = viscent @ viscent next, you can run the command on the remote host without entering the password. Command syntax: ssh remote username @ Remote Host IP address 'remote command or script'
For example, after the ssh userA@192.168.0.6 'hostname' command is executed, the terminal outputs the Host Name of the Peer host, rather than the Host Name of the host you are currently logged on. Description
The hostname command is actually run on the peer host. To remotely execute a script, you only need to change the third parameter of the preceding command to the full name of the file name of the remote script to be executed.
For example: www.2cto.com ssh userA@192.168.0.6 '/home/userA/script/test. sh' Note: when some commands are used in remote scripts, these commands are identified by the Shell parser and depend on
PATH environment variable, the script needs to include the command for executing the profile file in its first line. For example, in Bash,
The first behavior of the script: source ~ /. Bashrc otherwise, remote Scripts may report errors that cannot be found by commands. Source http://blog.viscenthuang.info/