Sometimes, the command to execute a remote machine locally using SSH can easily handle some repetitive work. We want to do this:
No manual input password
Supports execution of multiple commands, executing shell scripts
Support to execute sudo command
No manual input password
We can use SSH mutual trust, sshpass and expect tools to avoid manual transmission of passwords. The use process may encounter the following tedious scenario where you need to enter Yes manually:
$ ssh Username@hostname
The authenticity of host ... can ' t be established.
ECDSA Key fingerprint is ...
Are you sure your want to continue connecting (yes/no)?
To avoid this scenario, add the following parameters to the SSH command:
$ ssh-o "Stricthostkeychecking No" username@password
SSH Mutual trust
SSH Trust configuration is very simple, first generated SSH key:
$ ssh-keygen
Copy public key to the trusted party:
$ ssh-copy-id-i ~/.ssh/id_rsa.pub Username@hostname
After the execution of the command:
$ ssh-o "stricthostkeychecking no" username@password cmd
Sshpass
Sshpass is a non-interactive SSH password verification tool that is installed before use:
$ yum Install Sshpass
For example:
$ sshpass-p Password Ssh-o "stricthostkeychecking no" username@hostname cmd
Expect
Expect is a software tool for automating control and testing. Although the study cost is high, but the expect function is formidable, uses the expect to be possible to execute the remote command conveniently. Install before use:
$ yum Install expect
For example:
#!/usr/bin/expect
Spawn Ssh-o "Stricthostkeychecking No" username@hostname
Expect "*assword*"
Send "Password"
Expect "*$*"
Send "command"
Expect "*$*"
Send "Exit"
Expect EOF
Expect not only supports SSH, but also supports SCP, FTP and other tools.
Support for multiple commands and scripts
Execute more than one command
Sshpass and expect are very similar in support of multiple commands, just use 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"
......
Execute local Script
For executing local scripts, SSH and Sshpass are similar in usage.
# 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
For expect, you first need to copy the script to the remote host, and then execute the script on the remote host, as follows:
...
# Copy script to remote host
Spawn Scp-o "Stricthostkeychecking no" shell_script.sh username@hostname:~/
Expect "*assword*"
Send "Password"
Expect "*100%*"
Expect EOF
# Execute The shell script at remote host
Spawn Ssh-o "Stricthostkeychecking No" username@hostname
Expect "*assword*"
Send "Password"
Expect "*$*"
Send "sh shell_script.sh"
......
Support for sudo command execution
Some commands require sudo permission to execute, but we do not want to enter the password repeatedly, we can change each command as follows: