商务合作加Q:411239339

Python 自己写ssh客户端工具

浏览:425次阅读
一条评论

共计 1482 个字符,预计需要花费 4 分钟才能阅读完成。

因为之前一直习惯于用 CentOS, 这次 CentOS 7 出来有一段时间了,自己才在物理机上安装上,之前有使用过 Ubuntu 一段时间,发现包的管理方式不太喜欢,果断换成了自己熟悉的 CentOS,使用了几天后,发现特别符合我的使用习惯,总算找到自己喜欢的发行版本了。

由于是新版本的 CentOS,除了启动方式使用的是 systemctl 管理工具,以及做了一些优化,系统使用方面其它的基本没什么变化,首先想到的是找一个 GUI 的 ssh 工具,发现安装了 putty 和 SecureCRT 都无法安装上,putty 本来我也不太喜欢,SecureCRT 需要 python 2.6 的版本,但我的系统中是 python 2.7 的版本,无法安装,未果。于是果断自己写一个简单的 ssh 命令行客户端工具,思路很简单,就是使用系统中自带的 ssh 命令,只是用 python 包装了下,没有做太多的异常处理,反正是自己使用。

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import os
import sys
#for test
#os.system('ssh root@192.168.46.143');
conf_file='/etc/cssh.conf'
def ReadConf(conf_file):
    config_info = []
    with open(conf_file,"r") as fd:
        while True:
            c = fd.readline().strip('\n')
            if c:
                config_info.append(c.split(' '))
            else:
                break
    return config_info
def ConnectServer():
    info = ReadConf(conf_file)
    index = 1
    for i in info:
        print str(index) + ' . ' + i[1]
        index = index + 1
    select = raw_input("Which server do you want to select:")
    os.system('ssh '+ info[int(select)-1][0] + '@' + info[int(select)-1][1])
ConnectServer()

配置文件 /etc/cssh.conf 内容格式如下, 第一列为用户名,第二列为 IP 地址:

root 192.168.46.153 

root 192.168.46.142 

root 192.168.46.143 

root 192.168.46.151 

root 172.17.192.201 

root 192.168.46.77

将该脚本命名为 cssh,然后放到 /usr/bin 目录下边,就可以直接在命令名输入 cssh 命令来使用了:

1 . 192.168.46.153

2 . 192.168.46.142

3 . 192.168.46.143

4 . 192.168.46.151

5 . 172.17.192.201

6 . 192.168.46.77

Which server do you want to select:

输入前边的序号即可直接使用:

Which server do you want to select:6
root@192.168.46.77's password:

正文完
扫码赞助
post-qrcode
 0
果子
版权声明:本站原创文章,由 果子 于2015-06-25发表,共计1482字。
转载说明:除特殊说明外本站文章皆由果较瘦原创发布,转载请注明出处。
评论(一条评论)
2015-06-25 17:20:55 回复

雁过留声,人过留评

 Windows  Chrome  中国山东省菏泽市联通