183

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
  • 188,800
  • 56
  • 490
  • 992
Alt
  • 2,597
  • 5
  • 26
  • 36
  • 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
  • 13
    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
  • 21
    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
  • 4
    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
  • Instead of using scp, you can copy RSA public key to the server like so: `ssh-copy-id remoteuser@remotehostname` – gedijedi Jul 10 '22 at 19:09
  • 1
    I agree with @ScottPrive and in fact [my answer](https://stackoverflow.com/a/74602643/6854564) uses [MacPorts](https://www.macports.org/) instead of relying on random Github repositories or random non-popular websites that claim to be dedicated to open source software. – Kubuntuer82 Nov 28 '22 at 15:16
  • @Kubuntuer82 - TY. I don't know what's worse here, the lack of understanding what "read only embedded filesystem" means. Or homebrew's devs who do understand it, but actively don't care about the use case. – Scott Prive Dec 01 '22 at 20:49
  • Is it safe to use SSHPASS ? it doesn't guarantee that it does not share server user host and password ? – divine_rythm Apr 20 '23 at 06:14

12 Answers12

233

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,625
  • 1
  • 15
  • 11
  • 22
    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
  • 2
    `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
  • 1
    For what little it's worth, I've forked Aleks' repo above and updated to the latest version of sshpass on sourceforge (1.08). `brew install stevenhaddox/sshpass/sshpass` – stevenhaddox Nov 02 '22 at 16:34
  • looks like `sshpass` has to support `ssh` versions to get it working. – prayagupa Dec 16 '22 at 00:49
191

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
Sybil
  • 2,503
  • 3
  • 25
  • 36
JSimonsen
  • 2,642
  • 1
  • 13
  • 13
  • 38
    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
  • 10
    **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` – Konstantin Nikolskii Apr 27 '21 at 09:27
  • Brew has taps now, see some of the other answers... – rogerdpack Feb 03 '22 at 19:49
  • I did not need Xcode, the Command-Line Tools were enough. – nyg Feb 25 '23 at 10:39
45

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

brew install esolitos/ipa/sshpass
shiramy
  • 789
  • 8
  • 13
  • 2
    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
  • 4
    this is work for me on 2022. macbook pro m1 – shalk Mar 10 '22 at 02:29
31

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
  • 683
  • 1
  • 7
  • 15
  • 1
    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
  • 1
    Add `--insecure` to `curl` if you get: **SSL certificate problem: certificate has expired** – nezort11 Mar 18 '23 at 10:26
19

Solution provided by lukesUbuntu from github works for me:

Just use brew

$ brew install http://git.io/sshpass.rb
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
SHI Zhong Ping
  • 545
  • 4
  • 11
  • 11
    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
10

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,507
  • 2
  • 10
  • 4
  • `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
    This is better than other answers that use a specific and likely outdated version – ehecatl Jun 21 '22 at 22:13
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
  • 25,512
  • 7
  • 93
  • 64
5

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
  • 3,344
  • 2
  • 38
  • 63
5

Short answer

To avoid having to rely on unknown Github repositories (directly or via Homebrew taps) just use MacPorts :)

Just install MacPorts and then type

sudo port install sshpass  

Details / Long answer

The idea is to install sshpass via MacPorts instead of Homebrew.

You can have both Homebrew and MacPorts on the same machine, but be careful as some packages are available on both sources, and in that case you should be consistent in order to avoid conflicts.

In such cases, normally I give priority to Homebrew, but sshpass won't be provided by them, as they explicitly say.

In fact, if you type:

brew install sshpass

Then the output will also include this sentence:

We won't add sshpass because it makes it too easy for novice SSH users to ruin SSH's security.

Then in this case MacPorts is the only choice (if you really want to use sshpass).

Some links

Link to the MacPorts project
How to install MacPorts
Link to the sshpass Port on MacPorts

Kubuntuer82
  • 1,487
  • 3
  • 18
  • 40
  • 2
    Thank you! I wasn't even aware macports was still around. :-) This entire page is a shining example to how security can be made WORSE through blind, inflexible opinion and perspective. All these embedded Linux users had to seek lesser-known, fewer-eyeballs repos due to Homebrew's activist stance. – Scott Prive Dec 01 '22 at 20:58
  • 2
    Yes @ScottPrive you are totally right: perhaps they could put plenty of warnings, or make the package more difficult to install (similarly to what Chrome does when you visit websites with invalid SSL certificates, forcing you to type 'thisisunsafe' on the screen), but they should not completely forbid this, because the consequences will be exactly the ones you said. – Kubuntuer82 Dec 02 '22 at 09:43
3

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
  • 342
  • 2
  • 6
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,801
  • 4
  • 31
  • 29
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...