1

I currently use Terminal to connect the cluster by:

>ssh -p port user_name@address
>user_name@addresspassword's:
>password 

I want to creat a user defined command, simply like connect and a configure file containing those essential elements

# These contents can be edited
port: xxxx
user_name: xxxx
address: xxxx
password: xxxx

After those setting, I want to connect the cluster by simply typing 'connect' in the terminal.

1
  • 2
    If you specifically don't want key authentication and instead want the less-secure password approach, take a look at cyberciti.biz/faq/…
    – steve
    Jan 28 '18 at 13:12
3

Install sshpass first.

apt-get/yum install sshpass -y

Add this to your ~/.bashrc:

connect()
(
    . ~/.ssh/variables_for_ssh.sh
    SSHPASS=$password sshpass -e ssh -p "$port" "$username@$address"
)

Create a file called variables_for_ssh.sh in ~/.ssh/ (or some other directory that's only readable by you), with this content:

password='yourpassword'
port=portnumber
username='your username'
address='your address'

And then change the permissions, so that only you and root can read and write the file.

chmod 600 ~/.ssh/variables_for_ssh.sh

When you change the content of variables_for_ssh.sh, just source your ~/.bashrc, before running connect.

7
  • 1
    I took the liberty of editing some things: You need to source the file with the variables to have them appear in the current shell, just sh would spawn an independent shell to lead them and then do nothing. Don't use sshpass -p, it makes the password visible in the output of ps (I think the man page even warns about that). Also, if you have '$password' in single quotes, it's not even expanded. I also think something like ~/.ssh might be a better place than /tmp for such a file.
    – ilkkachu
    Jan 28 '18 at 13:46
  • @ilkkachu, thanks for the edit! Just one thing, what if he does not have root access? Jan 28 '18 at 13:48
  • Does that matter other than for installing sshpass? They should have access to their own home directory regardless, and they need it to modify .bashrc, too
    – ilkkachu
    Jan 28 '18 at 13:50
  • It would matter if the permissions for variables_for_ssh.sh are 700 and the user's .bashrc is sourcing the variables. Jan 28 '18 at 13:52
  • 1
    (0600 would probably be better, there's no need for execute permission)
    – ilkkachu
    Jan 28 '18 at 13:55
0

Edit ~/.ssh/config:

Host Address
User user_name
Hostname address
Port xxx

Now you can connect with

 $ ssh Address 

If you want to shorten it further add an alias to

~/.bashrc:

 alias connect="ssh Address" 

You should set up an ssh public key and place it in the remote server to allow passwordless secure login.

It is not good idea to store the password in plaintext or shorten a command like the sshpass one with an alias (which again would store the password in plain text), see this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.