Python 自己写ssh客户端工具

2015/06/2514:18:52 1

因为之前一直习惯于用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:

  • 微信扫码赞助
  • weinxin
  • 支付宝赞助
  • weinxin

发表评论

您必须才能发表评论!

目前评论:1   其中:访客  0   博主  0

    • lhj205

      雁过留声,人过留评