1

There is a way that we can connect a remote computer without password by using ssh-keygen.(for more info look at this)

In my problem, there is two computer (let's say computer A and B) and computer A can connect computer B without ssh password. When computer B's ip address is changed(but I don't know if new ip is really belong to it.), I am editing known_host file as replacing old ip with new one. If new ip is really belong to computer B, computer A can continue to connect without password so there is no problem. But if new ip is not belong to computer B, ssh asks password to connect computer B.

Since all above progress is performed by a python code, SSH is waiting for password and my program stops running if I tried to connect computer B. What I am trying to do is check if ssh asks password without entering password.

Is there any ssh options or any other way to do that?

1
  • 1
    Use paramiko rather than calling ssh through commands (which has been deprecated for years in any case...) Commented Sep 10, 2012 at 11:25

2 Answers 2

2

You can do this on using python pexpect module.

Example:

import pexpect

ssh_new_key = "Are you sure you want to continue connecting"

cmd_connect = "ssh -p22 %s@%s" % (account_name, host)

try:

    p = pexpect.spawn(cmd_connect)

    i = p.expect([ssh_new_key, 'asswor'])

    if i == 0:
        p.send('yes\r')
        i = p.expect([ssh_newkey, 'assword:'])

    if i == 1:
        p.send(account_pwd)

except Exception, exp:  
    print = "Could not connect to %s with account %s" % (host, account_name)
Sign up to request clarification or add additional context in comments.

Comments

1

After reading ssh man page I discovered PasswordAuthentication option. It is used like that

import commands
ret,out = commands.getstatusoutput("ssh -n -o PasswordAuthentication=no "+username+"@"+ip)
if ret == 0:
   #success
else:
   #fail

If connection needs password, it returns fail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.