7

I can SSH from my pc using putty, 1 line like so

ssh [email protected] -pw abc123

But when I try this from OSX terminal, I get an error Bad port 'w'

How can make this work on macOS?

1
  • 1
    Are you using -p in the hopes it will prompt you for a password?
    – rogerdpack
    Feb 7, 2022 at 21:24

5 Answers 5

20

ssh does not support passing a password on the command line, it is interpreting the -pw as "Connect to port w".

In order to do automated logins via ssh, use .ssh/authorized_keys:

  • On the OSX terminal, create a key using ssh-keygen. If you accept the default filenames, your ssh client will automatically try this key when it connects.
  • Copy the contents of the resultant .pub file and add it to .ssh/authorized_keys on the destination host (eg myname.mydomain.us:~root/.ssh/authorized_keys)

If your heart is set on passwords via the commandline, the expect toolset can be used to interact with stdin/stdout on running commands.

12

What you're trying to do is impossible with the built-in SSH client in OS X. The OpenSSH client is incapable of accepting a password from the command line.

The reason you're getting the "Bad port" error is because the -p flag is used to specify the port to connect to and the -pw flag does not exist. See the man page for more details.

Additionally, it's almost always a bad idea to specify a password from the command line. Have you looked into using public key authentication?

2
  • I'm trying to tell my mom over the phone how to SSH to my computer so I can help her with stuff remotely. She can copy an SSH command, but the blind password entry confuses her, and there's zero chance I'm walking her through setting up public key auth. Besides, there are other weird reasons to need to send the password in one line; why can't SSH just let me use it the way I want?
    – sudo
    Mar 26, 2021 at 20:37
  • OpenSSH can take a password from the command line these days: superuser.com/a/1703039/39364 (but not the built-in one, like you say)
    – rogerdpack
    Feb 7, 2022 at 21:24
1

If the above is not enough, you can install sshpass (see howto: https://stackoverflow.com/questions/32255660/how-to-install-sshpass-on-mac) then use sshpass -p YOUR-PWD ssh USER-NAME@HOST-NAME

1

I do this way:

  1. create small bash file in your home dir (cd ~/ - to jump into home dir) use: nano 'connection_name'.sh then write this code:
#!/usr/bin/expect -f
spawn ssh user_name@{ip addr or server name}
# expect "login:" 
# send "{put username here}\r"
expect "password:"
send "{put password here}\r"
interact
  1. go to bash_profile nano ~/.bash_profile put at the end alias connection_name="~/'connection_name'.sh"
  2. re-open terminal
  3. just enter connection_name

(connection_name - your shorthand command such as raspberry, banana, or something as you like)

0

I would strongly recommend using the macOS built-in

 $ ssh-copy-id 

will ask you to enter the password (of course, to the target system)
then,
appends the exchanged key to ~/.ssh/authorized_keys, so that you only have to validate credentials that first time


1
  • 1
    Adding a few more lines to answer the question that was asked would make this into an answer rather than simply a helpful comment. This area is reserved for answers, and comments, even helpful ones, will be removed from this area. Feb 23, 2023 at 5:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .