如何使用Python实现堡垒机模式下远程命令执行操作

  介绍

这篇文章主要介绍如何使用Python实现堡垒机模式下远程命令执行操作,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

具体如下:

<强>一点睛

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

如何使用Python实现堡垒机模式下远程命令执行操作

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

<强>二代码

#=utf - 8编码   # !/usr/bin/env  python   import  paramiko   import 操作系统、系统时间   主机名=?92.168.0.120",,,,,, #,定义业务服务器   用户名=皉oot"   密码=?23456”;   信号=?92.168.0.101",,,,,,,, #,定义业务堡垒机   布鲁斯=皉oot"   blpasswd=?23456”;   端口=22   passinfo=& # 39; \ & # 39; s 密码:,& # 39;,,,,,,#,输入服务器密码的前标志串   paramiko.util.log_to_file (& # 39; syslogin.log& # 39;)   ssh=paramiko.SSHClient (),,,,,, #, ssh登录堡垒机   ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ())   ssh.connect(主机名=波动,用户名=蓝调,密码=blpasswd)   # new 会话   频道=ssh.invoke_shell(),,,,, #,创建会话,开启命令调用   channel.settimeout(10),,,,,,, #,会话命令执行超时时间,单位为秒   时间=buff  & # 39; & # 39;   时间=resp  & # 39; & # 39;   channel.send (& # 39; ssh  & # 39; +用户名+ & # 39;@ # 39;+主机名+ & # 39;\ n # 39;),,,, #,执行ssh登录业务主机   while  not  buff.endswith (passinfo):,,,,,,,,,,, #, ssh登录的提示信息判断,输出串尾含有“\ & # 39;s 密码:“时退出而循环   尝试才能:   ,,,resp =, channel.recv (9999)   except 才能;例外,e:   ,,,print  & # 39; Error 信息:% s  connection 时间# 39;公司,%,(str (e))   ,,,channel.close ()   ,,,ssh.close ()   ,,,sys.exit ()   buff 才能+=,分别地   if 才能;not  buff.find(& # 39;是的/没有# 39;)==1:,,,,,,,,,,#,输出串尾含有“是的/no"时发送“yes"并回车   ,,,channel.send(& # 39;是的\ n # 39;)   打印(浅黄色)   print (“* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *“)   channel.send(密码+ & # 39;\ n # 39;),,,,,,,,,,,,,, #,发送业务主机密码   浅黄色=& # 39;& # 39;   while  not  buff.endswith(& # 39; #, & # 39;):,,,,,,,,,,,,, #,输出串尾为“#,“时说明校验通过并退出而循环   时间=resp 才能;channel.recv (9999)   if 才能;not  resp.find (passinfo)==1:,,,,,,,,,, #,输出串尾含有“\ & # 39;s 密码:,“时说明,密码不正确,要求重新输入   ,,,print  & # 39; Error 信息:,Authentication 失败强生# 39;   ,,,channel.close(),,,,,,,,,,,,,,,, #,关闭连接对象后退出   ,,,ssh.close ()   ,,,sys.exit ()   buff 才能+=,分别地   channel.send (& # 39; ifconfig \ n # 39;),,,,,,,,,,,,,,, #,认证通过后发送ifconfig命令来查看结果   浅黄色=& # 39;& # 39;   试一试:   while 才能;buff.find (& # 39; #, & # 39;)==1:   ,,,resp =, channel.recv (9999)   ,,,buff  +=,分别地   例外,except  e:   print 才能“error 信息:“+ str (e)   print  buff ,,,,,,,,,,,,,,,,,,,,,, #,打印输出串   channel.close ()   ssh.close ()

<强>三输出结果

E: \ Python脚本\ python_auto_maintain \ venv \ \ Python。exe E:/Python/python_auto_maintain/6 _3_2。py
最后登录:2019年2月28日22:00:07星期四从192.168.0.106
你好cakin24 !
ssh root@192.168.0.120
[root@slave2 ~] # ssh root@192.168.0.120
root@192.168.0.120& # 39;密码:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
ifconfig
enp0s3:=4163 & lt;旗帜,广播,跑步,MULTICAST>,mtu 1500
,,,,,,,inet 192.168.0.120 子网掩码255.255.255.0 广播192.168.0.255

如何使用Python实现堡垒机模式下远程命令执行操作