自动登录ssh


ubuntu下连接服务器老是敲用户名密码和 ip很郁闷,自己写了个python脚本:如下:


#!/usr/bin/python
import os,sys,pexpect,string

user_ip_pass={
"104":["10.8.8.104","wuxiaohua","aspire-tech.com"],
"107":["10.8.8.107","sudawei_cs","sudw.1004]"],
"110":["10.8.8.110","sudawei_cs","sudw.1004]"]
}

def ssh_server():
    while True:       
        address=raw_input("please choose server you want to connect,\ncurrently supports:%s:\n"%str(user_ip_pass.keys()))
        if user_ip_pass.has_key(address):
            break
        elif address=="bye":
            return   
        else:
            print "currently do not supports:%s"%address
           
    url='''ssh %s@%s'''%(user_ip_pass[address][1],user_ip_pass[address][0]) 
    try: 
        p = pexpect.spawn(url) 
        p.expect("password:") 
        p.sendline(user_ip_pass[address][2]) 
        p.interact() 
    except: 
        print "connection close()" 

ssh_server()


将此文件保存到  ~/bin/easy_ssh  文件中
这样就可以输入文件名执行该脚本了.
如果有增加服务器的化,直接在字典里添加数据即可
当然,你需要安装python运行环境和pexpect扩展包,这个也很简单,到新得利软件包里搜索安装就行了。