---
title: "【转】ubuntu自动登录ssh"
author: "hhhk"
date: "2012-11-01T19:47:42Z"
tags: []
categories: ["操作系统"]
slug: "ubuntu-ssh"
draft: false
lastmod: "2012-11-01T19:48:26Z"
id: 1711828
url: "https://hhhk.iteye.com/blog/1711828"
---

本文转自 [http://huqilong.blog.51cto.com/53638/198570/](http://huqilong.blog.51cto.com/53638/198570/)

 

动登录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扩展包，这个也很简单，到新得利软件包里搜索安装就行了。