总体思路:
调用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()
效果如下: