the remote execution of commands via the SSH command first requires establishing a trust relationship between the relevant hosts. Otherwise, the SSH command prompts you to enter the password for the remote host before executing the command. The method of establishing a trust relationship between hosts is as follows-SSH without password input:
Let's say we have two hosts. The host names are Linuxa and LINUXB, respectively. First, the public and private key files of the host are born with the current user running the following command on Linuxa:
ssh-keygen-t RSA
after the above command executes, two files appear under hidden directory ~/.ssh: Id_rsa and Id_rsa.pub. Whereid_rsa.pub is a public key file .
Append the contents of the file to the Authorized_keys file in the ~/.ssh directory on the peer host LINUXB. If the file does not exist, you can create it yourself .
here is the file content for an example of a id_rsa.pub file:
Ssh-rsa aaaab3nzac1yc2eaaaabiwaaaqeatbw/vkjriktffjsjp9fyvb3kqstc31obuikvaczzoejxsm2+ck6cb09l4bofujpi0+ oml4nptxkeagkcgnmco2yxrvsoqhqyaqv2bndpkymoeq2mgb9hsc9xqka+q== [email protected]
Next, you can execute the command privately on the remote host without entering a password. The command format is as follows:
For IP in IPList
SCP file name $ip:/directory Name
SSH $i/full path file name
ssh remote user name @ remote host IP address ' remote command or script '
For example,
ssh [email protected] ' hostname '
after the above command is executed, the terminal outputs the host name of the peer host, not the host name of the host you are currently logged on to. Description hostname This command is actually running on the peer host. To execute a script remotely, simply change the third parameter of the command above to the full name of the remote script you want to execute. For example:
ssh [email protected] '/home/usera/script/test.sh '
It is important to note that when a command is used in a remote script that is dependent on the PATH environment variable by the shell parser's recognition, the script needs to include the command that executes the profile in its first row. For example, in bash, the first behavior of the script:
source ~/.BASHRC
Otherwise, the remote script may report errors that some commands cannot find.
SSH non-interactive remote execute command script under Linux---SSH login with no password