In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you what SSH remote execution commands need to pay attention to, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
I believe that when you are working, you may encounter commands that use ssh to execute remote machines locally can easily handle some repetitive work. We hope to do this:
No need to enter the password manually
Support to execute multiple commands and execute shell scripts
Support the execution of sudo commands
No need to enter the password manually
We can use tools such as ssh Mutual Trust, sshpass and expect to avoid entering passwords manually.
During the usage process, you may encounter the following tedious scenarios where you need to enter yes manually:
$ssh username@hostnameThe authenticity of host... Can't be established.ECDSA key fingerprint is... Are you sure you want to continue connecting (yes/no)?
To avoid the above scenario, add the following parameters to the ssh command:
$ssh-o "StrictHostKeyChecking no" username@password
SSH mutual trust
The configuration of SSH mutual trust is very simple. First, generate ssh key:
$ssh-keygen
Copy the public key to the trusted party:
$ssh-copy-id-I ~ / .ssh/id_rsa.pub username@hostname
After that, execute the command without secret:
$ssh-o "StrictHostKeyChecking no" username@password cmd
Sshpass
Sshpass is a non-interactive ssh password authentication tool that is installed before use:
$yum install sshpass
The use is as follows:
$sshpass-p password ssh-o "StrictHostKeyChecking no" username@hostname cmd
Expect
Expect is a software tool for automatic control and testing. Although the cost of learning is high, the function of expect is powerful, and it is convenient to execute remote commands by using expect. Install before use:
$yum install expect
For example:
#! / usr/bin/expectspawn ssh-o "StrictHostKeyChecking no" username@hostnameexpect "* assword*" send "password\ n" expect "* $*" send "command\ n" expect "* $*" send "exit\ n" expect eof
Expect supports not only ssh, but also scp, ftp and other tools.
Support for multiple commands and scripts
Execute multiple commands
Sshpass and expect are very similar in supporting multiple commands, simply by using the & & connection command:
# ssh trust$ ssh-o "StrictHostKeyChecking no" username@password "cmd1 & & cmd2"
For example:
# sshpass$ sshpass-p password ssh-o "StrictHostKeyChecking no" username@password "ls-a & & mkdir test" # expect.expect "* $*" send "ls-a & & mkdir test\ n".
Execute local script
Ssh and sshpass are used similarly for executing local scripts.
# ssh trust$ ssh-o "StrictHostKeyChecking no" username@password bash-s
< shell_script.sh# sshpass$ sshpass -p password ssh -o "StrictHostKeyChecking no" username@password bash -s < shell_script.sh 对于 expect,首先需要把脚本拷贝到远程主机,然后在远程主机执行该脚本,步骤如下: ...# Copy script to remote hostspawn scp -o "StrictHostKeyChecking no" shell_script.sh username@hostname:~/expect "*assword*"send "password\n"expect "*100%*"expect eof# Execute the shell script at remote hostspawn ssh -o "StrictHostKeyChecking no" username@hostnameexpect "*assword*"send "password\n"expect "*$*"send "sh shell_script.sh\n"...... 支持执行 sudo 命令 有些命令需要 sudo 权限才能执行,但是我们不希望重复的输入密码,我们可以把每条命令修改为如下: cmd --->'echo password | sudo-S cmd'
For example:
$sshpass-p password ssh-o "StrictHostKeyChecking no" username@password "echo password | sudo-S mkdir / newdir"
For some commands such as echo and dd, the following failure scenarios sometimes occur:
$sshpass-p password ssh-o "StrictHostKeyChecking no" username@password 'echo password | sudo-S echo hello > / newdir/newfile'bash: / newdir/newfile: insufficient permissions
The solution is as follows:
Cmd-- > 'echo password | sudo-S sh-c "cmd" # For example$ sshpass-p password ssh-o "StrictHostKeyChecking no" username@password' echo WSfdl097018= | sudo-S sh-c "echo hello > / newdir/newfile"'
If you use expect, you need to copy the script to the remote host, and then execute the script on the remote host using sudo, which is simpler and more robust than sshpass:
... # Copy script to remote hostspawn scp-o "StrictHostKeyChecking no" shell_script.sh username@hostname:~/expect "* assword*" send "password\ n" expect "* 100% *" expect eof# Execute the shell script at remote hostspawn ssh-o "StrictHostKeyChecking no" username@hostnameexpect "assword*" send "password\ n" expect "* $*" send "sudo sh shell_script.sh\ n" expect "* assword*" send "password\ n". The above is all the contents of the article "what do you need to pay attention to in the remote execution of SSH commands?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.