Apple - How to SSH in one line

You should be using SSH keys to authenticate with rather than putting your password on the command line as it's extremely insecure.

The way this works is once you have your SSH keys set up, all you have to do is issue the command:

ssh user@host

and without typing another thing, you will be automatically logged in.


Copy SSH Public Key to Mac/FreeBSD/Linux from macOS

This assumes you have access to the remote server via password based authentication (typing in a password), and that you have already generated your private/public keypair (if not, see below). In the following example, we are using RSA. To start with let's copy the key over (be aware that the "home" directory differs between macOS, Linux, BSD, etc.):

Using SCP:

scp ~/.ssh/id_rsa.pub username@hostname:/Users/username/.ssh/

Or simply cat-ing the file to authorized_keys (I prefer this method):

cat id_rsa.pub | ssh username@hostname ' cat >>.ssh/authorized_keys'

(Your key name may differ)  If the .ssh directory does not exist on the remote server you will need to login and create it.

Now the key has been copied from the mac to the remote server. Set correct permissions for the SSH Public Key on the remote server:

chmod 600  ~/.ssh/id_rsa.pub

Next add the key to the SSH authorized_keys file, if the file does not exist create it.

If the file authorized_keys already exists in ~/.ssh the use the following command:

cat id_rsa.pub >> authorized_keys

If the file does not exist enter the following commands:

cat id_rsa.pub > authorized_keys

chmod 600 authorized_keys
chown user:group authorized_keys


Generate SSH Public/Private key on macOS

Open up the Terminal by going to Applications -> Utilities -> Terminal

In the terminal, use the following command to start the key generation

ssh-keygen -t rsa

Next you will be prompted to provide the location where you want to create the private key file:

Enter file in which to save the key (/Users/username/.ssh/id_rsa):

Leave this empty to create the key in the default location, which is /Users/username/.ssh/id_rsa. The public key file will be created in the very same location, and with the same name, but with the .PUB extension.

After you will be prompted to choose a passphrase. This is the password optional to use the private key.

Enter passphrase (empty for no passphrase):

Your SSH key is generated.

Now, keep in mind, if you put in a passphrase you will be required to enter it each time you connect. The utility ssh-agent will keep the passphrase in memory alleviating the need to manually enter it every time you connect while you are in the same session. For more details see man ssh-agent


There are several possibilities. Your example will obviously not work, but you can achieve something similar using sshpass utility:

sshpass -p password ssh host@IP

Note, this is not recommended because the password will be visible for other processes or in the shell history.

A much better way to do the same is to set up the passwordless authentication using SSH keys. In short:

ssh-keygen -t rsa -f ~/.ssh/id_rsa
ssh-copy-id IP

I have spent a long time looking for the answer to this too. Despite it being insecure and all these people telling you to use RSA keys (which IS a more secure and reliable idea), it is quite possible.

Use a program called expect for this. Expect will watch stdout (and I think stderr if configured correctly) for you, waiting for certain messages and responding to them with output. Expect itself is actually a scripting language, and when I was doing this same thing, I had a very hard time getting my own script to work properly because of timing. However, expect also includes a handy utility called autoexpect.

With autoexpect, it will watch you and generate an expect script for you. Simply run autoexpect and the command you want:

autoexpect ssh host@ip 

and do what you'd normally do. When you exit the program (by typing exit in the ssh'd shell), it will generate the script. In case that you don't want the whole script you're writing to be in an expect script, you can edit the script from autoexpect (called script.exp) to exit before typing the exit command into the shell. The line you want to move to change the script ending is:

expect eof

which means expect end of file. Hope this helps!

Tags:

Terminal

Ssh