How to use the ssh command to count distributed cluster information in Linux

07-04-2023

The knowledge points of this article on how to use the ssh command to count distributed cluster information in Linux are not well understood by most people, so the editor summarizes the following content for you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this article on how to use the ssh command to count distributed cluster information in Linux.


1. The idea of counting distributed cluster information

Use the password-free remote login command to realize a machine accessing other cluster machines. Therefore, there are several important steps to achieve:

  • Learn how to use shh

  • Realize password-free login

  • Writing a shell program

Second, SSH encryption principle

Asymmetric encryption technology is a kind of encryption and decryption Encryption methods with different keys. Among them, RSA and DSA are asymmetric encryption methods. Of course, the asymmetric encryption algorithm has a premise that there is a pair of keys that can encrypt and decrypt each other. Usually, this pair of keys will become the holder's public key and private key. A public key is a key known to others, while a private key is known only to the holder. Of course, if the private key is leaked to outsiders, there may be information security issues.

If: A wants to send B an encrypted data. The first method is: A encrypts with its own private key, and B decrypts with A's public key after obtaining the data. The second method is: A encrypts data with B's public key, and B obtains the data and then decrypts it with his own private key.

SSH remote login uses the first method.

Third, SSH command

The commonly used method is

ssh -i [private key file name] -p [port number] user@hostname [Executed command]

The private key file name defaults to $HOME/.ssh/id_rsa or $HOME/.ssh/id_dsa, and the port number defaults to 22.

For example:

ssh [email protected] #Enter the password of the target host for remote operation, exit command to exit

Fourth, use ssh-keygen and ssh-copy-id Realize password-free login

ssh-keygen #Directly enter all, first default

Then a key pair file will be generated in the $HOME/.ssh/ directory, where * .pub is unified as a public key file, and the other is a private key file.

Next, put the public key fileCopy the content in $HOME/.ssh/authorized_keys of the target host user. The copy method can be operated manually. It can also be implemented with the following command

ssh-copy-id -i [public key file name] user$hostname

For example:

ssh-copy-id -i $HOME/.ssh/id_rsa.pub [email protected]

Then execute the remote login command to realize password-free login. If a password is required for the second login, it may be due to file and file directory permission issues. It could also be because the private and public keys for the connection are not a pair.

Five, Linux Shell script implementation

sshtest.sh file:

#/ Bin/bashfunc___ Setenv(){
RTS_ ECT_ DIR="$HOME" #....}######################################## Program Body ########################################### Calling Environment Variable func___ Setenv
#While reading machine connection information_ Cmd=
Ssh_ Linkhost=` echo ${line}`
While read cmdline
Do # skip if [- z "$cmdline"] on empty command line; Then continue
Fi
#Read command line concatenation; If [! - n "$ssh_cmd"]; Then ssh_ Cmd=` echo ${$cmdline} `;
Else ssh_ Cmd="${ssh_cmd};" ` echo ${$cmdline};
Fi done<${RTS-ECT-DIR}/sshcmd. cfg # Execute remote command ssh $ssh_ Linkhost $ssh_ Cmd</dev/nulldone<${RTS-ECT-DIR}/sshlink.cfg


sshlink.cfg file: the premise is to configure the key before you can log in without password

[email protected] 

[email protected] 

[email protected]

sshcmd.cfg file: fill in according to your needs

ps -ef|grep java df -h

Six, ssh only loops once

If the penultimate line of the above code is changed to

ssh $ssh_linkhost $ssh_cmd

Then the while loop only loops once. Because the while read line will put the loop information into the standard input, and the ssh command will read all the input buffers, so the while is not looping, so the ssh command needs to be redirected.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us