随心一记

一二三四五,上山打老鼠


  • 首页

  • 归档

  • 标签
ywcsb

ywcsb

游戏可以不玩,小说不能不看。

153 日志
3 分类
42 标签
RSS
GitHub 知乎 随心一记
Links
  • 随心一记
  • 追梦人物的
  • MSDN

使用Python3编译一个跳板机

发表于 2019-01-03 | 阅读 1479 | 分类于 Python |

出于方便管理公司服务器,能够快速的登录服务器管理运维。在确保安全性的情况下就想出了使用跳板机来登录公司服务器,好了废话不不多说了直接上代码!

IP地址:主机名称缩写:主机名称
IP地址:主机名称缩写:主机名称

上面是代码里iplist文件的格式

#!/usr/bin/env python3
#-*-coding:utf-8-*-
'''
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    File Name:   ssh_tunnel.py
    Description :   Tunnel Server SSH Tunnel
    Author :    angell
    date:   2018/3/8
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    Change Activity:
                    2018/3/8:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
'''
__author__ = 'xiaobing'
import os,re,getpass,platform
def ssh(ip):
    user_get = getpass.getuser()
    user = ["lihuabing","lihuabing"] #配置运维人员用户名,让运维可以直接是用wddssa账号登录服务器
    try:
        useradd = input("Please Input User Name:")
    except:
        return written_square()
    if user_get in user:
        if useradd == "wddssa":
            #print("sudo -u %s ssh %s" %(useradd,ip))
            os.system("sudo -u %s ssh %s" %(useradd,ip))
        else:
            #print("ssh %s@%s"%(useradd,ip))
            os.system("ssh %s@%s"%(useradd,ip))
    else:
        #print("ssh %s"%ip)
        os.system("ssh %s"%ip)
def ip_1(dic,pas3):
    lists = re.split(":", dic[pas3])
    return lists[2]
def ip_if (dic):
    try:
        conut =0
        while True:
            if conut == 5:
                return written_square()
            pas3 = int(input("Please Choose Server's NUM ( Ctrl D to exit ):"))
            if pas3 in dic:
                return ip_1(dic,pas3)
            else:
                print('\terr')
                return ip_if(dic)
    except:
        return written_square()
def output(a):  #把列表转换成字典输出
    cont = 0
    dic = {}
    for i in a:
        cont += 1
        ii = ":".join(i)
        dic[cont] = ii
    for b in dic:
        print("(%s):%s"%(b,dic[b]))
    return ip_if(dic)
def read_file(ab):
    a = []
    # 读取文件内容
    with open(r'iplist', 'r', encoding='UTF-8') as f:    #iplist主要存放主机名称IP地址
        # 以每行为列表的形式保存
        gc_nn = f.readlines()
    for i in gc_nn:  # 遍历列表,列出每行的数据
        resuit = re.split(":", i)  # 以:把字符串分割成列表
        if len(resuit) == 3:  # 判断列表元素个数是否等于3
            if ab in resuit[1]:  # 模糊判断列表是否包含字符
                abc = re.split("\n", i)
                abc_1 = ''.join(abc)  # 把列表转换成字符串
                abc_2 = abc_1.split(':')  # 把字符串按照:的形式分割成列表
                a.append([abc_2[1],abc_2[2],abc_2[0]])
    if a:
        return output(a)
    else:
        return written_square()
def written_square_1():   #输入广场首写字母
    os.system("clear")
    try:
        ab = input('''Tunnel server 10.77.130.201 new login shell,pls call 8888 if something occur!
        Please Input Plaza Name ( 'q' to exit ):''')
    except:
        return written_square()
    if ab == "wanda!@#$":
        exit()
    elif ab == "q":
        kill()
    elif re.findall('\D',ab):
        return read_file(ab)
    else:
        return written_square()
def kill():
    ppid = os.getppid()
    os.system('kill -9 %s' % ppid)
def written_square():
    connt = 0
    while True:
        connt += 1
        if connt == 10:
            kill()
        ip = written_square_1()
        ssh(ip)
if  __name__ == '__main__':
    written_square()



vim /etc/profile.d/ss_tunnel.sh



#!/bin/bash
/usr/bin/python3 /etc/profile.d/ssh_tunnel.py

编写一个启动shell脚本然后放到profile.d文件夹里面,每打开一个shell终端就会运行脚本 IP地址就不放出来了

觉得不错,支持一下!
geerniya WeChat Pay

微信打赏

geerniya Alipay

支付宝打赏

# Python # shell # 脚本
CentOS6 lamp+zabbix安装
使用shadowsocks实现访问google

发表评论

共 0 条评论

    暂无评论
© 2018 - 2022 ywcsb
冀ICP备17022045号-1
Supported by 腾讯云