Monday, October 24, 2016

auto login ssh

1- script


import os
import sys
import re
import pexpect
import ruamel.yaml
from ruamel.yaml.util import load_yaml_guess_indent

sample = """
hlist:
    - name: srv1       
      ip: 1.1.1.1    
      user: u1
      pass: p1 

    - name: srv2         
      ip: 1.1.1.2   
      user: u1
      pass: p1   
"""
homedir =  os.getenv("HOME")
if os.path.exists(homedir +"/.sshauto/") == True:
    if os.path.isfile(homedir +"/.sshauto/config.yml"):
        pass
    else:
        print "update your configureation first"
        print "~/.sshauto/config.yml"
        print "samples:"
        print sample
        sys.exit()
else:
    os.system("mkdir ~/.sshauto")
    print "update your configureation first"
    print "~/.sshauto/config.yml"
    print "sample:"
    print sample
    sys.exit()
  
comm = sys.argv[1]

configf = homedir + '/.sshauto/config.yml'
config, ind, bsi = load_yaml_guess_indent(open(configf))
hlist = config['hlist']

def uploadkey(key,ip,user,passwd):
    child = pexpect.spawn("ssh-copy-id -i " + key + " " + user+"@"+ip)
    index = child.expect(['password','yes/no','verification failed',"already exist"])
    print index
    if index == 0:
        child.sendline(passwd)
        child.sendline("exit")
      
    elif index == 1:
        child.sendline("yes")
        child.expect("password")
        child.sendline(passwd)
        child.sendline("exit")
      
    elif index==2:
        os.system("ssh-keygen -R "+ ip)
        uploadkey(key,ip,user,passwd)
    else:
        pass
  
def login(key,ip,user,passwd):
    os.system("ssh -i " + key + " " + user+"@"+ip)

if comm == "genkey":
    if os.path.isfile(homedir + "/.sshauto/sshprivate.key") == True:
        print "key already exist"
    else:
        os.system("ssh-keygen -f ~/.sshauto/sshprivate.key")
elif comm == "list":
    for i in hlist:
        print i['name'], i['ip'], i['user']
elif comm == "upkey":
    comm2 = sys.argv[2]
    #print hlist
    pubkey = homedir + "/.sshauto/sshprivate.key.pub"
  
    if comm2 == "all":
        for i in hlist:
            ip = i['ip']
            user = i['user']
            passwd = i['pass']
            print "upload public key to " + i['name']
            print ip, user
            uploadkey(pubkey,ip,user,passwd)
    else:
        for i in hlist:
            if comm2 == i['name']:
              
                ip = i['ip']
                user = i['user']
                passwd = i['pass']
                print "upload public key to " + i['name']
                print ip, user
                uploadkey(pubkey,ip,user,passwd)
              
                break
            else:
                print "there's no host like that"
elif comm == "login":
    key = homedir + "/.sshauto/sshprivate.key"
    comm2 = sys.argv[2]
    for i in hlist:
        if comm2 == i['name']:
            ip = i['ip']
            user = i['user']
            passwd = i['pass']
            print "login to " + i['name']
            print ip, user
            login(key,ip,user,passwd)
          
            break
        else:
            print "there's no host like that"


2- sample configuration file
hlist:
- name: srv1
ip: 1.1.1.1
user: u1
pass: p1

- name: srv2
ip: 1.1.1.2
user: u1
pass: p1


3- usage
- generate key pair
$ sshauto genkey

- upload public keys to all host
$ sshauto upkey all

- upload public keys to a host
$ sshauto upkey srv1

- now the auto login can be performed
$ sshauto login srv1

No comments:

Post a Comment