侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python实现堡垒机模式下远程命令执行操作示例

Python  /  管理员 发布于 7年前   364

本文实例讲述了Python实现堡垒机模式下远程命令执行操作。分享给大家供大家参考,具体如下:

一 点睛

堡垒机环境在一定程度上提升了运营安全级别,但同时也提高了日常运营成本,作为管理的中转设备,任何针对业务服务器的管理请求都会经过此节点,比如SSH协议,首先运维人员在办公电脑通过SSH协议登录堡垒机,再通过堡垒机SSH跳转到所有的业务服务器进行维护操作。

我们可以利用paramiko的invoke_shell机制来实现通过堡垒机实现服务器操作,原理是SSHClient.connect到堡垒机后开启一个新的SSH会话 (session),通过新的会话运行“ssh user@IP”去实现远程执行命令的操作。

二 代码

#coding=utf-8#!/usr/bin/env pythonimport paramikoimport os,sys,timehostname="192.168.0.120"      # 定义业务服务器username="root"password="123456"blip="192.168.0.101"        # 定义业务堡垒机bluser="root"blpasswd="123456"port=22passinfo='\'s password: '      # 输入服务器密码的前标志串paramiko.util.log_to_file('syslogin.log')ssh=paramiko.SSHClient()      # ssh登录堡垒机ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect(hostname=blip,username=bluser,password=blpasswd)#new sessionchannel=ssh.invoke_shell()     # 创建会话,开启命令调用channel.settimeout(10)       # 会话命令执行超时时间,单位为秒buff = ''resp = ''channel.send('ssh '+username+'@'+hostname+'\n')    # 执行ssh登录业务主机while not buff.endswith(passinfo):           # ssh登录的提示信息判断,输出串尾含有"\'s password:"时退出while循环  try:    resp = channel.recv(9999)  except Exception,e:    print 'Error info:%s connection time.' % (str(e))    channel.close()    ssh.close()    sys.exit()  buff += resp  if not buff.find('yes/no')==-1:          # 输出串尾含有"yes/no"时发送"yes"并回车    channel.send('yes\n')print(buff)print("*************************************************************************************")channel.send(password+'\n')  # 发送业务主机密码buff=''while not buff.endswith('# '): # 输出串尾为"# "时说明校验通过并退出while循环  resp = channel.recv(9999)  if not resp.find(passinfo)==-1:          # 输出串尾含有"\'s password: "时说明 密码不正确,要求重新输入    print 'Error info: Authentication failed.'    channel.close()    # 关闭连接对象后退出    ssh.close()    sys.exit()  buff += respchannel.send('ifconfig\n')   # 认证通过后发送ifconfig命令来查看结果buff=''try:  while buff.find('# ')==-1:    resp = channel.recv(9999)    buff += respexcept Exception, e:  print "error info:"+str(e)print buff           # 打印输出串channel.close()ssh.close()

三 输出结果

E:\Python\python_auto_maintain\venv\Scripts\python.exe E:/Python/python_auto_maintain/6_3_2.py
Last login: Thu Feb 28 22:00:07 2019 from 192.168.0.106
hello cakin24!
ssh [email protected]
[root@slave2 ~]# ssh [email protected]
[email protected]'s password:
*************************************************************************************
ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.120  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::a00:27ff:fe0a:6e8a  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:0a:6e:8a  txqueuelen 1000  (Ethernet)
        RX packets 2046  bytes 179711 (175.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1408  bytes 148744 (145.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 404117  bytes 68752333 (65.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 404117  bytes 68752333 (65.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
qbr07d54630-64: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1450
        ether 42:76:47:57:b2:75  txqueuelen 1000  (Ethernet)
        RX packets 11  bytes 572 (572.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
qvb07d54630-64: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST>  mtu 1450
        inet6 fe80::4076:47ff:fe57:b275  prefixlen 64  scopeid 0x20<link>
        ether 42:76:47:57:b2:75  txqueuelen 1000  (Ethernet)
        RX packets 12  bytes 816 (816.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
qvo07d54630-64: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST>  mtu 1450
        inet6 fe80::dcbe:efff:feb7:5d52  prefixlen 64  scopeid 0x20<link>
        ether de:be:ef:b7:5d:52  txqueuelen 1000  (Ethernet)
        RX packets 8  bytes 648 (648.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 816 (816.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost ~]#

更多关于Python相关内容可查看本站专题:《Python字符串操作技巧汇总》、《Python常用遍历技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。


  • 上一条:
    深入浅析python 协程与go协程的区别
    下一条:
    python3.6使用tkinter实现弹跳小球游戏
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客