Use SSH for non-interactive remote command execution script in Linux

Source: Internet
Author: User
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, you can query specific keywords in the log files on each node in the cluster environment. Www.2cto.com through SS...
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/
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.