首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导致错误的子进程: TypeError:应为字符串、字节或os.PathLike对象,而不是元组

导致错误的子进程: TypeError:应为字符串、字节或os.PathLike对象,而不是元组
EN

Stack Overflow用户
提问于 2021-09-11 16:49:57
回答 1查看 183关注 0票数 0

我正在为Hack the box,Try Hack Me等上的服务器构建一个枚举工具。当我尝试自动化端口扫描时,我遇到了子进程和将输出写入文件的问题。

代码语言:javascript
复制
import os
import sys
import traceback
import subprocess as sub
import re

ip_addr = ''
nickName = ''
Dir = ''


def getIP():
    global ip_addr
    ip_addr = str(input('[+] Please enter the IP address you would like to enumerate: \n'))
    if not re.match("^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$", ip_addr):
        print('[-] That is not the correct format for an IP address. \n    [-] Please try again.')
        getIP()
    
def mk_nickname():
    global nickName
    nickName = str(input('[+] Please give this IP a nickname. \n    [+] This will be used to create a directory to keep you notes organized. \n    [+] This will be found in your documents folder within your home directory.\n'))
    if nickName == '':
        mk_nickname()
    return

#add if file already exsists clause (exsit_ok may have done the trick)
def mkdir():
    global Dir
    Dir = f"{os.getenv('HOME')}/Documents/" + nickName
    os.makedirs(Dir, mode=0o700, exist_ok=True)
    

def PortScan():
    YN = str(input('[+] Would you like to run a port scan? '))
    portDir = Dir +'/portscan.txt'
    print(portDir)
    if YN == 'y' or YN == 'yes':
        print('[+] Starting portscan.\n    [+] The results can be found here: ' + portDir )
        cmd = "rustscan", "-a", ip_addr, "--", "-sV", "-sC", "-A"
        print(cmd)
        sub.Popen([cmd], stdout=sub.PIPE, stderr=sub.PIPE, text=True)
        with open(portDir, w) as f:
            file.write(result.stdout)

        
    elif YN == 'n' or YN == 'no':
        return
    else:
        print('[-] Invalid input!\n[-] Please try again.')
    

        
print('[+] Lets start enumerating!!!')
getIP()
mk_nickname()
mkdir()
PortScan()

我尝试了许多不同的方法,但似乎不能让它工作。

下面是接收到的错误:

代码语言:javascript
复制
[+] Lets start enumerating!!!
[+] Please enter the IP address you would like to enumerate: 
10.10.10.75
[+] Please give this IP a nickname. 
    [+] This will be used to create a directory to keep you notes organized. 
    [+] This will be found in your documents folder within your home directory.
nibbles
[+] Would you like to run a port scan? yes
/home/kali/Documents/nibbles/portscan.txt
[+] Starting portscan.
    [+] The results can be found here: /home/kali/Documents/nibbles/portscan.txt
('rustscan', '-a', '10.10.10.75', '--', '-sV', '-sC', '-A')
Traceback (most recent call last):
  File "/home/kali/Desktop/OSCPENUM.py", line 57, in <module>
    PortScan()
  File "/home/kali/Desktop/OSCPENUM.py", line 41, in PortScan
    sub.Popen([cmd], stdout=sub.PIPE, stderr=sub.PIPE, text=True)
  File "/usr/lib/python3.9/subprocess.py", line 951, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.9/subprocess.py", line 1698, in _execute_child
    and os.path.dirname(executable)
  File "/usr/lib/python3.9/posixpath.py", line 152, in dirname
    p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not tuple

我可以把这归因于没有完全理解子进程,但在查看了文档和许多不同的fourms/栈溢出帖子后,我仍然无法找到解决方案。这就是为什么我求助于Stack Overflow的领主们!:)

EN

回答 1

Stack Overflow用户

发布于 2021-09-11 18:11:44

将Popen行替换为sub.Popen(cmd,stdout=sub.PIPE,stderr=sub.PIPE,text=True)。(注意,我去掉了方括号)- Flimm 1

这解决了这个问题。现在开始调试程序的其余部分。这很容易,因为这个问题已经过去了。

根据我对这个问题的理解:子流程文档指定使用括号[]作为"arg“的值。当您通过subprocess.Popen()而不是直接命令传递变量时,这不适用。

代码语言:javascript
复制
def PortScan():
    YN = str(input('[+] Would you like to run a port scan?\n'))
    portDir = Dir +'/portscan.txt'
    print(portDir)
    if YN == 'y' or YN == 'yes':
        print('[+] Starting portscan.\n    [+] The results can be found here: ' + portDir )
        cmd = "rustscan", "-a", ip_addr, "--", "-sV", "-sC", "-A"
        print(cmd)
        f = open(portDir, "w")
        sub.Popen(cmd, stdout=f, text=True)

非常感谢Flimm的快速响应!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69144976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档