• Python安全脚本 ---- Linux主机基线查询


    总体思路:

    调用pexpect模块中的pxssh 与Linux主机实现交互,然后批量导入命令。

    能实现与Linux主机交互的方式:

    from pexpect import pxssh
    
    s = pxssh.pxssh()      
            s.login(host,username,passwd)    
            s.sendline(cmd)
            s.prompt()             
            print s.before     

    能实现批量导入命令的方式:

    dictionary = open(dir_name,'r')
    
    for line in dictionary.readlines():
         cmd = line.strip('
    '

    用于查询Linux主机基线命令的txt字典(可按需增减):

    cat /etc/issue
    
    ifconfig
    
    cat /etc/passwd
    
    cat /etc/pam.d/system-auth
    
    cat /etc/login.defs
    
    cat /etc/ssh/sshd_config
    
    ls -l /etc/passwd /etc/shadow /etc/group
    
    cat /etc/profile
    
    service --status-all
    
    chkconfig
    
    cat /etc/init/control-alt-delete.conf

    结合在一起:

    #! /usr/bin/python
    # coding=utf-8
    # __author__='Dou—wei'
    
    import sys
    from pexpect import pxssh
    
    
    host = sys.argv[1]
    username = sys.argv[2]
    passwd = sys.argv[3]
    dir_name = sys.argv[4]
    
    
    def try_ssh(host,username,passwd,cmd):
        try:
            s = pxssh.pxssh()      
            s.login(host,username,passwd)    
            s.sendline(cmd)
            print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' 
            s.prompt()             
            print s.before                  
        except Exception,e:
            print e
    
    
    def main():
        dictionary = open(dir_name,'r')
        for line in dictionary.readlines():
        cmd = line.strip('
    ')
        try:
           try_ssh(host,username,passwd,cmd)
        except:
           pass
            
    
    if __name__=='__main__':
        main()    
            

    效果如下:

  • 相关阅读:
    程序员偷偷深爱的9个不良编程习惯
    JQuery实现放大镜
    ACM1995
    liubo.im
    Linux中的一些点
    EPOLL使用详解
    Elays'Blog
    c#数据库解析
    codeforces #332 div 2 D. Spongebob and Squares
    类型
  • 原文地址:https://www.cnblogs.com/ScriptKid-Lu/p/10730866.html
Copyright © 2020-2023  润新知