用python写个自动SSH登录远程服务器的小工具(实例)


Posted in Python onJune 17, 2017

很多时候我们喜欢在自己电脑的终端直接ssh连接Linux服务器,而不喜欢使用那些有UI界面的工具区连接我们的服务器。可是在终端使用ssh我们每次都需要输入账号和密码,这也是一个烦恼,所以我们可以简单的打造一个在Linux/Mac os运行的自动ssh登录远程服务器的小工具。

来个GIF动画示例下先:

用python写个自动SSH登录远程服务器的小工具(实例)

概述

我们先理一下我们需要些什么功能:

1. 添加/删除连接服务器需要的IP,端口,密码

2. 自动输入密码登录远程服务器

对,我们就做这么简单的功能

开始写代码

代码比较长,所以我也放在在Github和码云,地址在文章最底部:

1.我们建个模块目录osnssh(Open source noob ssh),然后在下面再建两个目录,一个用来放主程序取名叫bin吧,一个用来保存登录数据(IP, 端口,密码)叫data吧。

-osnssh
-bin
-data

1.设置程序:添加/删除IP,端口,密码. 建立py文件bin/setting.py:

#!/usr/bin/env python
#-*-coding:utf-8-*-
import re, base64, os, sys
path = os.path.dirname(os.path.abspath(sys.argv[0]))
'''

选项配置管理

__author__ = 'allen woo'
'''
def add_host_main():
 while 1:
  if add_host():
   break
  print("\n\nAgain:")

def add_host():
 '''
 添加主机信息
 :return: 
 '''
 print("================Add=====================")
 print("[Help]Input '#q' exit")
 # 输入IP
 host_ip = str_format("Host IP:", "^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$")
 if host_ip == "#q":
  return 1
 # 输入端口
 host_port = str_format("Host port(Default 22):", "[0-9]+")
 if host_port == "#q":
  return 1
 # 输入密码
 password = str_format("Password:", ".*")
 if password == "#q":
  return 1
 # 密码加密
 password = base64.encodestring(password)
 # 输入用户名
 name = str_format("User Name:", "^[^ ]+$")
 if name == "#q":
  return 1
 elif not name:
  os.system("clear")
  print("[Warning]:User name cannot be emptyg")
  return 0

 # The alias
 # 输入别名
 alias = str_format("Local Alias:", "^[^ ]+$")
 if alias == "#q":
  return 1
 elif not alias:
  os.system("clear")
  print("[Warning]:Alias cannot be emptyg")
  return 0
 # 打开数据保存文件
 of = open("{}/data/information.d".format(path))
 hosts = of.readlines()
 # 遍历文件数据,查找是否有存在的Ip,端口,还有别名
 for l in hosts:
  l = l.strip("\n")
  if not l:
   continue
  l_list = l.split(" ")
  if host_ip == l_list[1] and host_port == l_list[2]:
   os.system("clear")
   print("[Warning]{}:{} existing".format(host_ip, host_port))
   return 0
  if alias == l_list[4]:
   os.system("clear")
   print("[Warning]Alias '{}' existing".format(alias))
   return 0
 of.close()
 # save
 # 保存数据到数据文件
 of = open("{}/data/information.d".format(path), "a")
 of.write("\n{} {} {} {} {}".format(name.strip("\n"), host_ip.strip("\n"), host_port, password.strip("\n"), alias.strip("\n")))
 of.close()
 print("Add the success:{} {}@{}:{}".format(alias.strip("\n"), name.strip("\n"), host_ip.strip("\n"), host_port, password.strip("\n")))
 return 1

def remove_host():
 '''
 删除主机信息
 :return: 
 '''
 while 1:
  # 打开数据文件
  of = open("{}/data/information.d".format(path))
  hosts = of.readlines()
  of.close
  l = len(hosts)
  if l <= 0:
   os.system("clear")
   print("[Warning]There is no host")
   return

  print("================Remove================")
  print("+{}+".format("-"*40))
  print("|  Alias UserName@IP:PORT")
  hosts_temp = []
  n = 0
  # 遍历输出所以信息(除了密码)供选择
  for i in range(0, l):
   if not hosts[i].strip():
    continue
   v_list = hosts[i].strip().split(" ")
   print("+{}+".format("-"*40))
   print("| {} | {} {}@{}:{}".format(n+1, v_list[4], v_list[0], v_list[1], v_list[2]))
   n += 1
   hosts_temp.append(hosts[i])
  hosts = hosts_temp[:]
  print("+{}+".format("-"*40))
  c = raw_input("[Remove]Choose the Number or Alias('#q' to exit):")
  is_alias = False
  is_y = False
  try:
   c = int(c)
   if c > l or c < 1:
    os.system("clear")
    print("[Warning]:There is no")
    continue
   del hosts[c-1]
   is_y = True

  except:
   is_alias = True
  if is_alias:
   if c.strip() == "#q":
    os.system("clear")
    break 
   n = 0
   for l in hosts:
    if c.strip() == l.split(" ")[4].strip():
     del hosts[n]
     is_y = True 
    n += 1
  if not is_y:
   os.system("clear")
   print("[Warning]:There is no")
   continue
  else: 
   # save
   # 再次确认是否删除
   c = raw_input("Remove?[y/n]:")
   if c.strip().upper() == "Y":
    of = open("{}/data/information.d".format(path), "w")
    for l in hosts:
     of.write(l)
    print("Remove the success!")
    of.close()

def str_format(lable, rule):
 '''
 用于验证输入的数据格式
 :param lable: 
 :param rule: 
 :return: 
 '''
 while 1:
  print("{} ('#q' exit)".format(lable))
  temp = raw_input().strip()
  m = re.match(r"{}".format(rule), temp)
  if m:
   break
  elif "port" in lable:
   temp = 22
   break
  elif temp.strip() == "#q":
   os.system("clear")
   break
  os.system("clear")
  print("[Warning]:Invalid format")

 return temp

2. 我们再添加一个函数在setting.py用于输出我们的信息,也就是about me。

def about():
 '''
 输出关于这个程序的信息
 :return: 
 '''
 of = open("{}/bin/about.dat".format(path))
 rf = of.read()
 try:
  info = eval(rf)
  os.system("clear")
  print("================About osnssh================")
  for k,v in info.items():
   print("{}: {}".format(k, v))
 except:
  print("For failure.")
 return

然后在bin目录下面建立个文件about.dat写入我们的一些信息,比如:

{
 "auther":"Allen Woo",
 "Introduction":"In Linux or MAC using SSH, do not need to enter the IP and password for many times",
 "Home page":"",
 "Download address":"https://github.com/osnoob/osnssh",
 "version":"1.1.0",
 "email":"xiaopingwoo@163.com"
}

好了设置程序就这样了:

2. 自动登录远程服务器程序:在bin建个py文件叫auto_ssh.py:

注意:这里我们需要先安装个包叫:pexpect, 用户终端交互,捕捉交互信息实现自动输入密码。

安装pexpect:

pip install pexpect

然后开始写代码:

#!/usr/bin/env python
#-*-coding:utf-8-*-
import os, sys, base64
import pexpect
path = os.path.dirname(os.path.abspath(sys.argv[0]))

def choose():
 # 打开我们的数据文件
 of = open("{}/data/information.d".format(path))
 hosts = of.readlines()
 hosts_temp = []
 for h in hosts:
  if h.strip():
   hosts_temp.append(h)
 hosts = hosts_temp[:]
 l = len(hosts)
 if l <= 0:
  os.system("clear")
  print("[Warning]Please add the host server")
  return
 while 1:

  print("=================SSH===================")
  print("+{}+".format("-"*40))
  print("|  Alias UserName@IP:PORT")
  for i in range(0, l):
   v_list = hosts[i].strip().split(" ")
   print("+{}+".format("-"*40))
   print("| {} | {} {}@{}:{}".format(i+1, v_list[4], v_list[0], v_list[1], v_list[2]))
  print("+{}+".format("-"*40))
  c = raw_input("[SSH]Choose the number or alias('#q' exit):")
  is_alias = False
  is_y = False
  try:
   c = int(c)
   if c > l or c < 1:
    os.system("clear")
    print("[Warning]:There is no")
    continue
   l_list = hosts[c-1].split(" ")
   name = l_list[0]
   host = l_list[1]
   port = l_list[2]
   password = l_list[3]
   is_y = True

  except:
   is_alias = True
  if is_alias:
   if c.strip() == "#q":
    os.system("clear")
    return
   for h in hosts:
    if c.strip() == h.split(" ")[4].strip():
     l_list = h.split(" ")
     name = l_list[0]
     host = l_list[1]
     port = l_list[2]
     password = l_list[3]
     is_y = True
  if not is_y:
   continue
  # ssh
  # 将加密保存的密码解密
  password = base64.decodestring(password)
  print("In the connection...")
  # 准备远程连接,拼接ip:port
  print("{}@{}".format(name, host))
  if port == "22":
   connection("ssh {}@{}".format(name, host), password)

  else:
   connection("ssh {}@{}:{}".format(name, host, port), password)

def connection(cmd, pwd):
 '''
 连接远程服务器
 :param cmd: 
 :param pwd: 
 :return: 
 '''
 child = pexpect.spawn(cmd)
 i = child.expect([".*password.*", ".*continue.*?", pexpect.EOF, pexpect.TIMEOUT])
 if( i == 0 ):
  # 如果交互中出现.*password.*,就是叫我们输入密码
  # 我们就把密码自动填入下去
  child.sendline("{}\n".format(pwd))
  child.interact()
 elif( i == 1):
  # 如果交互提示是否继续,一般第一次连接时会出现
  # 这个时候我们发送"yes",然后再自动输入密码
  child.sendline("yes\n")
  child.sendline("{}\n".format(pwd))

  #child.interact() 
 else:
  # 连接失败
  print("[Error]The connection fails")

好了,现在我们只需要启动文件了,也就是打开程序后的第一个菜单

3.再osnssh目录下建个osnssh.py 文件:

#!/usr/bin/env python
#-*-coding:utf-8-*-
import os, sys
sys.path.append("../")
from bin import setting, auto_ssh
path = os.path.dirname(os.path.abspath(sys.argv[0]))
'''
方便在LINUX终端使用ssh,保存使用的IP:PORT , PASSWORD
自动登录
__author__ = 'allen woo'
'''
def main():
 while 1:

  print("==============OSNSSH [Menu]=============")
  print("1.Connection between a host\n2.Add host\n3.Remove host\n4.About\n[Help]: q:quit clear:clear screen")
  print("="*40)
  c = raw_input("Please select a:")
  if c == 1 or c == "1":
   auto_ssh.choose()
  if c == 2 or c == "2":
   setting.add_host_main()
  if c == 3 or c == "3":
   setting.remove_host()
  if c == 4 or c == "4":
   setting.about()
  elif c == "clear":
   os.system("clear")
  elif c == "q" or c == "Q" or c == "quit":
   print("Bye")
   sys.exit()
  else:
   print("\n")

if __name__ == '__main__':
 try:
  of = open("{}/data/information.d".format(path))
 except:
  of = open("{}/data/information.d".format(path), "w")
 of.close()
 main()

终于写完了,我们可以试一试了:

$python osnssh.py

以上这篇用python写个自动SSH登录远程服务器的小工具(实例)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
通过代码实例展示Python中列表生成式的用法
Mar 31 Python
使用Python设置tmpfs来加速项目的教程
Apr 17 Python
python学习之第三方包安装方法(两种方法)
Jul 30 Python
在Linux命令行终端中使用python的简单方法(推荐)
Jan 23 Python
Python3之读取连接过的网络并定位的方法
Apr 22 Python
python socket网络编程之粘包问题详解
Apr 28 Python
用Django写天气预报查询网站
Oct 21 Python
Python实现定制自动化业务流量报表周报功能【XlsxWriter模块】
Mar 11 Python
Python实现猜年龄游戏代码实例
Mar 25 Python
Python多线程 Queue 模块常见用法
Jul 04 Python
Python天气语音播报小助手
Sep 25 Python
Python的三个重要函数详解
Jan 18 Python
python中Pycharm 输出中文或打印中文乱码现象的解决办法
Jun 16 #Python
Python生成随机密码的方法
Jun 16 #Python
Python操作SQLite数据库的方法详解
Jun 16 #Python
使用python实现个性化词云的方法
Jun 16 #Python
linux环境下python中MySQLdb模块的安装方法
Jun 16 #Python
Django中利用filter与simple_tag为前端自定义函数的实现方法
Jun 15 #Python
Python中关于Sequence切片的下标问题详解
Jun 15 #Python
You might like
Terran建筑一览
2020/03/14 星际争霸
生成静态页面的PHP类
2006/11/25 PHP
基于PHP的cURL快速入门教程 (小偷采集程序)
2011/06/02 PHP
在项目中寻找代码的坏命名
2012/07/14 PHP
jQuery 性能优化指南 (1)
2009/05/21 Javascript
jquery ztree实现下拉树形框使用到了json数据
2014/05/14 Javascript
JavaScript字符串对象的concat方法实例(用于连接两个或多个字符串)
2014/10/16 Javascript
JavaScript DOM元素尺寸和位置
2015/04/13 Javascript
javascript获取系统当前时间的方法
2015/11/19 Javascript
validationEngine 表单验证插件使用实例代码
2017/06/15 Javascript
seajs模块压缩问题与解决方法实例分析
2017/10/10 Javascript
AngularJS实现的获取焦点及失去焦点时的表单验证功能示例
2017/10/25 Javascript
10个经典的网页鼠标特效代码
2018/01/09 Javascript
JS+HTML5 Canvas实现简单的写字板功能示例
2018/08/30 Javascript
解决js相同的正则多次调用test()返回的值却不同的问题
2018/10/10 Javascript
Vue基本使用之对象提供的属性功能
2019/04/30 Javascript
小程序调用微信支付的方法
2019/09/26 Javascript
浅谈vue中组件绑定事件时是否加.native
2019/11/09 Javascript
解决vue下载后台传过来的乱码流的问题
2020/12/05 Vue.js
浅谈python中的占位符
2017/11/09 Python
Python爬虫包BeautifulSoup学习实例(五)
2018/06/17 Python
Python实现的逻辑回归算法示例【附测试csv文件下载】
2018/12/28 Python
IronPython连接MySQL的方法步骤
2019/12/27 Python
在pytorch 中计算精度、回归率、F1 score等指标的实例
2020/01/18 Python
python 画条形图(柱状图)实例
2020/04/24 Python
CSS实现雨滴动画效果的实例代码
2019/10/08 HTML / CSS
Mio Skincare中文官网:肌肤和身体护理
2016/10/26 全球购物
如何从一个文件档案的尾端新增记录
2016/12/02 面试题
研发工程师的岗位职责
2013/11/18 职场文书
集团公司人力资源部岗位职责
2014/01/03 职场文书
竞聘书怎么写,如何写?
2014/03/31 职场文书
个人年终总结范文
2015/03/09 职场文书
幼儿园教师求职信
2015/03/20 职场文书
学校开除通知书
2015/04/25 职场文书
感恩教育观后感
2015/06/17 职场文书
利用For循环遍历Python字典的三种方法实例
2022/03/25 Python