首页 > 其他 > 详细

利用堡垒主机获取防火墙里面主机信息

时间:2015-06-05 01:00:51      阅读:508      评论:0      收藏:0      [点我收藏+]
#!/usr/bin/env python
# coding=utf-8
import paramiko
import os,sys,time

# define baolei
blip = "188.188.22.225"
bluser = "root"
blpassword = "1233434"

# define server info
hostname = "112.74.96.65"
username = "root"
password = "ddddddd"

port = 22
passinfo = ‘\‘s password: ‘
paramiko.util.log_to_file(‘syslogin.log‘)

# login blhost
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=blip,username=bluser,password=blpassword)

# Create session ,Open a channel
channel = ssh.invoke_shell()
channel.settimeout(60)

buff = ‘‘
resp = ‘‘

# start login in 

channel.send(‘ssh ‘ + username + ‘@‘ +hostname + ‘\n‘)
while not buff.endswith(passinfo):
    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 :
        channel.send(‘yes\n‘)
        buff = ‘‘

channel.send( password+‘\n‘ )

buff = ‘‘
while not buff.endswith(‘# ‘):
    resp = channel.recv(9999)
    if not resp.find(passinfo) == -1:
        print ‘Error info: Authentication failed.‘
        channel.close()
        sys.exit()
    buff += resp


channel.send( ‘ifconfig\n free -m \n  df -TH \n ‘)
buff = ‘‘
try:
    while buff.find( ‘# ‘) == -1:
        resp = channel.recv(9999)
        buff += resp
except Exception, e:
    print "error info:"+str(e)

print buff
channel.close()
ssh.close


本文出自 “Smurf Linux运维” 博客,请务必保留此出处http://jin771998569.blog.51cto.com/2147853/1658562

利用堡垒主机获取防火墙里面主机信息

原文:http://jin771998569.blog.51cto.com/2147853/1658562

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!