2019-05-13

background

One thing that Mac lacks in terminal, as well as the better iTerm2 command-line tool, is the ability to save passwords over remote SSH connections. One way is to place the local sSH_key on a remote server for passwordless login. This approach is not possible in many cases because the remote server is mostly owned by the client. This article introduces a simple, lightweight command-line tool, sshpass, that allows you to provide passwords (non-interactive password authentication) to the command prompt itself, so that you can automatically connect to a remote server and automatically execute remote commands.

Install sshpass

  1. Download sshpass:sourceforge.net/projects/ss…
  2. Go to the sshpass directory
  3. Run [./configure]
  4. Sudo make install
  5. Run sshpass to test whether the installation is successful

Sshpass use

Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
-f filename   Take password to use from file
-d number     Use number as file descriptor for getting password
-p password   Provide password as argument (security unwise)
-e            Password is passed as env-var "SSHPASS"
With no parameters - password will be taken from stdin
-h            Show help (this screen)
-V            Print version information
At most one of -f.-d, -p or -e should be used
Copy the code

Log in to the remote Linux SSH server (10.42.0.1) using the user name and password, and check the disk usage of the file system, as shown in the figure.

$ sshpass -p 'my_pass_here'SSH [email protected]'df -h' 
Copy the code

You can also use sshpass to transfer files via SCP or rsync to backup/synchronize files, as shown below:

------- Transfer Files Using SCP ------- 
$ scp -r /var/www/html/example.com --rsh="sshpass -p 'my_pass_here' ssh -l aaronkilik"10.42.0.1:/var/www/html ------- Backup or Sync Files Using Rsync ------- $Rsync -- RSH ="sshpass -p 'my_pass_here' ssh -l aaronkilik"10.42.0.1: / data/backup / / backup /Copy the code

ITerm2 integrates sshpass for fast SSH connections

  • Open the Profiles menu for iTerm2 and go to Profiles Settings.
  • Click Edit Profiles.
  • Example Add an SSH connection.
    • Name: Name of the
    • Tags: Group or tag name
    • Title: Sets the window name
    • The Command:/usr/local/bin/sshpass -p 'xxxx' ssh [email protected]
  • Fast connection

The resources

  1. Sshpass: a great interactive SSH login tool, but don’t use it on production servers
  2. ITerm2 Saves the SSH user name and password