Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm using ubuntu 11.10. I'm using ssh for connecting to many servers daily, so i put their parameters in .ssh/config file; like this :

Host Home
User netmoon
Port 22
HostName test.com

Is there a way to put passwords in this file, for each connection? So when server asks for password, terminal put its pass and send it to server.

It's because sometimes I stand away from the pc and when I go back and type password, and press enter, terminal said CONNECTION CLOSED.

  • I don't want to use public/private key pair.
share|improve this question
    
I'm in the same situation and I cannot upload my public key because I have ssh access only for svn. That is if I try ssh svnhost I get "( success ( 2 2 ( ) ( edit-pipeline svndiff1 absent-entries commit-revprops depth log-revprops partial-replay ) ) )" svnserve response and not the shell –  Uberto Oct 25 '12 at 14:27

7 Answers 7

up vote 8 down vote accepted

If you don't really want to use a public/private key pair, you can write a expect script to enter the password for you automatically depending on the destination address.

Edit: What I mean is that you can have a script that, on one hand, uses expect to enter the password for you and, on the other hand, reads the password for a given user and host from a configuration file. For example, the following python script will work for the sunny day scenario:

#!/usr/bin/python                                                                                                                        
import argparse
from ConfigParser import ConfigParser
import pexpect

def main(args):
    url = args.url
    user, host = url.split('@', 1)

    cfg_file = 'ssh.cfg'
    cfg = ConfigParser()
    cfg.read(cfg_file)
    passwd = cfg.get(user, host)

    child = pexpect.spawn('ssh {0}'.format(url))
    child.expect('password:')
    child.sendline(passwd)
    child.interact()

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Run ssh through pexpect')
    parser.add_argument('url')
    args = parser.parse_args()
    main(args)

and the configuration file format would be as follows:

[user_1]
host1 = passwd_1
host2 = passwd_2

[user_2]
host1 = passwd_1
host2 = passwd_2

Note: As explained, the python script would need to be much more complex to handle all the possible errors and question messages from ssh and all the possible urls (in the example it's assumed that it will be something like user@host, but the user part isn't used most of the times), but the basic idea would still be the same. Regading the configuration file, you may use a different configuration file or use .ssh/config and write your own code to parse that file and get the password for a given user and host.

share|improve this answer
    
can you explain further? –  Netmoon Dec 16 '11 at 6:29
    
@Netmoon I added a small example to my answer to my it clearer. –  jcollado Dec 18 '11 at 13:14

Trading off security for convenience never ends well...

Could you use ssh-copy-id from the openssh-client package?

From man ssh-copy-id:

ssh-copy-id is a script that uses ssh to log into a remote 
machine and append the indicated identity file to that 
machine's ~/.ssh/authorized_keys file.
share|improve this answer
    
This doesn't work if the remote admin insists on disabling public key authorization... –  tomasz Sep 25 at 21:36
    
Yes, but being realistic on systems under your entire direct supervision and control is not making a compromise. Say, for example, on a vagrant virtual machine with no outside connections used solely for development purposes on a single seat. –  Scott Oct 30 at 18:29

No. This is not possible I'm afraid.

The only real alternative is to use private keys but you've said you don't want to (why not?).

share|improve this answer
1  
because i don't have permission to put another key on server. –  Netmoon Dec 15 '11 at 11:42
1  
@Netmoon: If you can log in, you can add a key, right? You only need write access to your home directory, unless the sysadmin set things up strangely. –  Scott Severance Dec 20 '11 at 4:07
    
@ScottSeverance I think that is the situation this question refers to. Not having the ability to add a key. Yes it is strange, but it often happens. –  user239558 May 16 '13 at 15:16
    
I've had the very common experience of shared hosting environments where public key access is disabled, so even though you can add keys, they are not used. it goes against reason yes, but that's the way many hosting providers set up their servers –  billynoah Jun 9 at 14:01
    
Thank you!! for the straight answer. –  laggingreflex 2 days ago

I use an application from VanDyke Software called SecureCRT.

http://www.vandyke.com/products/securecrt/

It is not free, but very reasonably priced. I have used it for years (running on Windows, or using Wine) for remote access, terminal emulation, and (dispersed) network management. They finally released a native Linux version of this at the beginning of 2011.

It has support for complex login settings (or scripts), stored passwords (or certificates), tabbed multiple sessions, etc.

At startup you can choose which remote target (and protocol) from a structured list (tree view) of stored remote (or local) machines, or just create a connection (which is then stored).

I have found it particularly useful for remote sites with advanced authentication, non-standard ports, or firewall-access negotiation.

If you are doing remote access a lot (part of your main role), then this application will justify its expense in the first month of use.

share|improve this answer
    
sorry, i don't understand. can you explain ? –  Netmoon Dec 16 '11 at 6:38
1  
reworded for you .. –  david6 Dec 16 '11 at 9:13
    
thanks, completely got it. –  Netmoon Dec 16 '11 at 15:04
1  
Since posting of the above response there has been several iterations of SecureCRT, including the latest one VanDyke just released in early December 2013. Each iteration has been improving the program making it even more versatile. It also has a rich API that allows the program to be controlled/interfaced with Python/VB scripts. SecureCRT has been part of my core toolkit for a good decade, and I highly recommend it. –  Ville Dec 19 '13 at 21:15
    
Agreed. I continue to beta-test each new release, and was heavily involved in early testing for porting to Ubuntu. –  david6 Dec 19 '13 at 22:36

This is not exactly an answer to your question but it may solve your problem.

SSH connection times out if you dont actively use it. It is not the server that times out but the underlying TCP connection which times out. To keep the connection alive you need to keep sending or receiving data over the ssh connection. One way to do this, which I have personally used, is to use ping command to ping google/yahoo/msn or any machine in the local network at few second interval. The output of the ping command is sent from server (where you are logged in) to your local machine and thus your connection wont die (timeout). So whenever I had to go away from pc for little longer interval, I used to use a command similar to following:

ping -i 5 google.com

You can use any other command of your choice, like if you want you can use date command with a similar effect:

while date ; do sleep 5 ; done
share|improve this answer
2  
Or you could just use the built-in keep-alive features.... –  ams Dec 15 '11 at 13:12
1  
@ams there are builtin keep alive features? can you plz mention them here. –  binW Dec 16 '11 at 5:36
    
@binW ; thanks for your answer, but i don't want do that.and for your question about KEEP ALIVE ,you just gotta add these two lines to the /etc/ssh/sshd_config file on your server: KeepAlive yes ClientAliveInterval 60 Now just restart sshd and your connection should never be interrupted again. –  Netmoon Dec 16 '11 at 6:33
    
@binW from man ssh_config ... look up TCPKeepAlive which would prevent the TCP connection dropping, and ServerAliveCountMax and ServerAliveInterval which together specify how long ssh should wait until a connection is declared dead (say if a cable gets pulled somewhere). Those are client-side options - the one @Netmoon mentioned are the server-side equivalents. –  ams Dec 16 '11 at 9:20
    
thanks to ams. it's my fault. –  Netmoon Dec 16 '11 at 15:02

The answer of @BrunoPereira to this question shows an alternative method to connect without explicitly entering a password and avoiding ssh keys.

You could create a script, an alias or a function in your ~/.bashrc to quickly execute that command.

Obviously, there are security consideration you should take into account with this approach.

share|improve this answer

There also is sshpass program for that. How to use: sshpass -p MyPa55word ssh [email protected]

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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