155

I would like to automate ssh login from my Mac.

It does have a simple solution:

sshpass -p my_password ssh m_username@hostname

But my problem is installing sshpass on my Mac.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
Alt
  • 2,219
  • 5
  • 24
  • 32
  • 4
    Why bother with sshpass when you can use ssh keypairs? More secure and dosn't expose password in the process table. – alvits Aug 27 '15 at 21:02
  • 12
    On OSX run the command `ssh-keygen -b 2048`. It will generate a keypair and store them in `~/.ssh.` as `id_rsa` (private key) and `id_rsa.pub` (public key). Copy `id_rsa.pub` to linux in the directory `~/.ssh/` and name the file `authorized_keys`. From here on when you connect to linux you will not need a password. The private/public keypair will be used. – alvits Aug 28 '15 at 20:17
  • 1
    Just for someone else who might be trying to follow @alvits very helpful advice: 1) generate the `id_rsa` file on your **local** machine and 2) `scp` the `id_rsa.pub` to the **server's** corresponding folder as suggested and 3) rename the `id_ras.pub` file on the **server**. – yuqli Feb 16 '19 at 02:14
  • 15
    The above answers do NOT answer OP's question. Good alternative, but not explaining how to install sshpass on Mac. There are cases where you cannot copy to the remote system (mine is embedded read only filesystem). So, something like sshpass is a better means to get into the remote. – ChuckB Feb 20 '19 at 21:11
  • 1
    While it's good to comment to inform risks, it is unhelpful to withhold direct answers on assumption the user can simply install SSH keys. Try dealing with embedded OS whose root-directory is read-only. Your choices then are continually typing passwords, playing hunt-the-homebrew-tap game, or writing Expect. – Scott Prive Jun 24 '21 at 14:01
  • 1
    By making users go install this from "random GH repos" the Homebrew developers are actively pushing users toward greater risk since those third-party repos have no eyes on them. I too am targeting a read-only embedded Linux. – Scott Prive Jun 24 '21 at 14:05
  • 1
    This answer is still relevant in 2021 https://stackoverflow.com/a/63886356/885886 – Ladich Oct 06 '21 at 10:57
  • It seems with "normal" openssh you can pass in a password these days: https://superuser.com/a/1703039/39364 so you may not need sshpass :) – rogerdpack Feb 07 '22 at 21:23

11 Answers11

186

There are instructions on how to install sshpass here:

https://gist.github.com/arunoda/7790979

For Mac you will need to install xcode and command line tools then use the unofficial Homewbrew command:

curl -L https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb > sshpass.rb && brew install sshpass.rb && rm sshpass.rb
Josefine
  • 2,401
  • 3
  • 22
  • 36
JSimonsen
  • 2,574
  • 1
  • 12
  • 12
  • 37
    In 2020 I found [this](https://stackoverflow.com/a/62623099/667301) as the best answer – Mike Pennington Aug 31 '20 at 14:42
  • 9
    Unfortunately, that didn't work for me ```Error: Calling Non-checksummed download of sshpass formula file from an arbitrary URL is disabled! Use 'brew extract' or 'brew create' and 'brew tap-new' to create a formula file in a tap on GitHub instead.``` – Alex Nov 24 '20 at 13:09
  • 9
    **Update for 2021**: `curl -L https://raw.githubusercontent.com/kadwanev/bigboybrew/master/Library/Formula/sshpass.rb > sshpass.rb && brew install sshpass.rb && rm sshpass.rb` – Constantine Nikolsky Apr 27 '21 at 09:27
  • Brew has taps now, see some of the other answers... – rogerdpack Feb 03 '22 at 19:49
178

Update 2022: Unfortunately, Aleks Hudochenkov is no longer updating his repo. There are a bunch of other repos on GitHub that purport to contain a Homebrew recipe for sshpass. It's up to you which of them (if any) to trust.

Some years have passed and there is now a proper Homebrew Tap for sshpass, maintained by Aleks Hudochenkov. To install sshpass from this tap, run:

brew install hudochenkov/sshpass/sshpass

Tap source

smammy
  • 2,062
  • 1
  • 13
  • 10
  • 17
    Is the source code somewhere? Using something like sshpass without knowing the code is critical – muuvmuuv Sep 17 '20 at 07:07
  • 11
    @muuvmuuv, yes. If you look at [the formula](https://github.com/hudochenkov/homebrew-sshpass/blob/master/sshpass.rb) in [that tap](https://github.com/hudochenkov/homebrew-sshpass), you will see that it downloads the source from https://downloads.sourceforge.net/project/sshpass/sshpass/1.06/sshpass-1.06.tar.gz and builds it locally. – smammy Sep 18 '20 at 15:44
  • 1
    Error: Your CLT does not support macOS 11. I use 11.2. – ikreb Feb 08 '21 at 07:25
  • Same error as @ikreb, `Your CLT does not support macOS 11.` On latest version of Big Sur. – kjdion84 Feb 08 '21 at 22:12
  • 3
    @kjdion84 This helps me: ``sudo rm -rf /Library/Developer/CommandLineTools`` and ``sudo xcode-select --install``. – ikreb Feb 09 '21 at 13:54
  • Works on Catalina with XCode 12.3 on 2021-02-18. – Earl Ruby Feb 18 '21 at 22:06
  • 1
    `This repository has been archived by the owner. It is now read-only.` so I can't request bump its version to latest sshpass :( – rogerdpack Feb 03 '22 at 20:01
  • @rogerdpack Ah, that's unfortunate. There are a [bunch of repos on GitHub](https://github.com/search?o=desc&q=homebrew+sshpass&s=updated&type=Repositories) that purport to contain a Homebrew recipe for sshpass. It's up to you which of them (if any) to trust. – smammy Mar 26 '22 at 19:24
39

Another option in 2020 is this homebrew tap, maintained by esolitos

brew install esolitos/ipa/sshpass
shiramy
  • 689
  • 7
  • 13
  • 1
    This is the only answer which literally answers the question. There are concerns with that - a tiny repo could go rogue (and not be noticed right away). But it is an answer. User is advised to exhaust every possible use case first, as there could be other simpler answers (like maybe if you're dealing with a Dropbear host that doesn't store `authorized_keys` under home directories and that's why your staged public key was rejected, etc) – Scott Prive Aug 26 '21 at 21:16
  • this is work for me on 2022. macbook pro m1 – shalk Mar 10 '22 at 02:29
22

Following worked for me

curl -O -L  https://sourceforge.net/projects/sshpass/files/sshpass/1.06/sshpass-1.06.tar.gz && tar xvzf sshpass-1.06.tar.gz
cd sshpass-1.06/
./configure
sudo make install
Suresh Kumar
  • 503
  • 4
  • 12
  • Just make sure you already have xcode installed. Please google how to do that, here is one of the link (https://www.freecodecamp.org/news/install-xcode-command-line-tools/) – Tzunghsing David Wong Nov 16 '21 at 18:16
20

Solution provided by lukesUbuntu from github works for me:

Just use brew

$ brew install http://git.io/sshpass.rb
OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
SHI Zhong Ping
  • 537
  • 4
  • 10
  • 10
    note: this is out of date and points to an old version of sshpass – Devis L. Nov 06 '18 at 01:17
  • 1
    @DevisLucato Yes, for the one LF the latest version of sshpass, xcode with sshpass src will lead the way. https://sourceforge.net/projects/sshpass/ Good luck. – SHI Zhong Ping Nov 06 '18 at 03:51
  • 4
    This is now out of date - the comment here: https://stackoverflow.com/a/62623099/1817774 is more current. Thanks @smammy – babelmonk Aug 10 '20 at 21:17
  • `Error: Non-checksummed download of sshpass formula file from an arbitrary URL is unsupported! `brew extract` or `brew create` and `brew tap-new` to create a formula file in a tap on GitHub instead.` – rogerdpack Feb 03 '22 at 20:02
9

Please follow the steps below to install sshpass in mac.

curl -O -L https://fossies.org/linux/privat/sshpass-1.06.tar.gz && tar xvzf sshpass-1.06.tar.gz

cd sshpass-1.06

./configure

sudo make install
SuperNova
  • 21,204
  • 6
  • 80
  • 55
4

I just followed the instructions from this article and it helped,

curl -O -L http://downloads.sourceforge.net/project/sshpass/sshpass/1.05/sshpass-1.05.tar.gz && tar xvzf sshpass-1.05.tar.gz

//This creates a directory sshpass-1.05

cd sshpass-1.05
./configure
make
sudo make install
Tom Taylor
  • 2,887
  • 2
  • 34
  • 55
2

For the simple reason:

Andy-B-MacBook:~ l.admin$ brew install sshpass
Error: No available formula with the name "sshpass"
We won't add sshpass because it makes it too easy for novice SSH users to
ruin SSH's security.

Thus, the answer to do the curl / configure / install worked great for me on Mac.

user1318024
  • 303
  • 2
  • 5
2

Just a slight update from the previous answer

curl -O -L  https://fossies.org/linux/privat/sshpass-1.09.tar.gz && tar xvzf sshpass-1.09.tar.gz
cd sshpass-1.09/
./configure
sudo make install

This Worked as on OCT 2021

Elijah W. Gagne
  • 2,681
  • 4
  • 30
  • 28
Alvis F
  • 31
  • 4
2

I found that most of the answers listed here are out of date. To install the latest, I ran this and downloaded directly from sourceforge.net, based on other answers here.

curl -L https://sourceforge.net/projects/sshpass/files/latest/download -o sshpass.tar.gz && tar xvzf sshpass.tar.gz
cd sshpass-*
./configure
sudo make install
tcbcw
  • 1,397
  • 2
  • 8
  • 3
  • `configure: error: in /Users/ME/sshpass-1.08: configure: error: C compiler cannot create executables See config.log for more details` – Khom Nazid Mar 15 '22 at 21:45
1

Aargh, the problem with the outdated links. Simply go to https://sourceforge.net/projects/sshpass/ Download latest version and then tar xvzf it and finally cd to the dir where it got unpackedand install with: ./configure make sudo make install I suppose this will also work on every OS with supported C sdk installed...