简体   繁体   English

Traceroute的Python脚本和在文件中打印输出显示Linux Mint中的错误(OSError:[Errno 2]没有这样的文件或目录)

[英]Python Script for Traceroute and printing the output in file shows error( OSError: [Errno 2] No such file or directory) in Linux Mint

I am trying to perform traceroute on google.com using python script and write the output to a file ie output.txt 我正在尝试使用python脚本在google.com上执行traceroute并将输出写入文件,即output.txt

If I directly use subprocess.call('traceroute','google.com') it works perfectly fine and the output gets printed on the screen. 如果我直接使用subprocess.call('traceroute','google.com'),它的工作完全正常,输出将打印在屏幕上。 Since I want to get the output in a file, I am using 因为我想在文件中获取输出,所以我正在使用

subprocess.Popen(["tracert", '-w', '100', hostname],stdout=subprocess.PIPE, stderr=subprocess.STDOUT) subprocess.Popen([“tracert”,' - w','100',hostname],stdout = subprocess.PIPE,stderr = subprocess.STDOUT)

and then writing the pipe data to file. 然后将管道数据写入文件。 However I get an error in this line ie OSError: [Errno 2] No such file or directory 但是我在这行中遇到错误,即OSError:[Errno 2]没有这样的文件或目录

Code : 代码:

import urllib
import time 
import datetime, threading
from bs4 import BeautifulSoup
import urllib2
import subprocess
import socket


fp2=open("output.txt",'w')


#This function will be executed after every x minutes


def TraceRoute():



        hostname="google.com"
        fp2.write(hostname+"    :   ")
        print(hostname)

        #subprocess.call(['traceroute',hostname])

        traceroute = subprocess.Popen(["tracert", '-w', '100',          hostname],stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        while (True):
                hop = traceroute.stdout.readline()
                if not hop: break
                print '-->',hop
                fp2.write( hop )


        threading.Timer(60*50, TraceRoute).start() #Ensures periodic execution of TraceRoute( ) x=60*50 seconds

TraceRoute()    

Error : 错误:

Traceback (most recent call last): Traceback(最近一次调用最后一次):

File "./scr3.py", line 87, in TraceRoute() TraceRoute()中的文件“./scr3.py”,第87行

File "./scr3.py", line 76, in TraceRoute traceroute = subprocess.Popen(["tracert", '-w', '100', hostname],stdout=subprocess.PIPE, stderr=subprocess.STDOUT) File "/usr/lib/python2.7/subprocess.py", line 710, in init errread, errwrite) 文件“./scr3.py”,第76行,在TraceRoute中traceroute = subprocess.Popen([“tracert”,' - w','100',主机名],stdout = subprocess.PIPE,stderr = subprocess.STDOUT)文件“/usr/lib/python2.7/subprocess.py”,第710行, init errread,errwrite)

File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child raise child_exception 文件“/usr/lib/python2.7/subprocess.py”,第1327行,在_execute_child中引发child_exception

OSError: [Errno 2] No such file or directory OSError:[Errno 2]没有这样的文件或目录

How to resolve this ? 怎么解决这个? I am stuck on this since forever. 我永远坚持这个。 Please help 请帮忙

Pretty sure it should be traceroute not tracert . 很确定它应该是traceroute而不是tracert tracert is a windows command , you can also use iter and stdout.readline to read the output: tracert是一个windows命令,你也可以使用iterstdout.readline来读取输出:

traceroute = subprocess.Popen(["traceroute", '-w', '100',hostname],stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

for line in iter(traceroute.stdout.readline,""):
    print(line)

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

相关问题 在终端中运行 python 脚本时出错:OSError: [Errno 2] No such file or directory - Error running python script in terminal: OSError: [Errno 2] No such file or directory Python OSError:[Errno 2]没有这样的文件或目录错误 - Python OSError: [Errno 2] No such file or directory ERROR Python:OSError:[Errno 2] 没有那个文件或目录:'' - Python: OSError: [Errno 2] No such file or directory: '' Python OSError:[Errno 2]没有这样的文件或目录 - Python OSError: [Errno 2] No such file or directory Python:OSError:[Errno 2]没有这样的文件或目录 - Python : OSError: [Errno 2] No such file or directory linux命令产生Python OSError:[Errno 2]没有这样的文件或目录 - linux command produce Python OSError: [Errno 2] No such file or directory Python:“ OSError:[Errno 2]没有这样的文件或目录:”…找到了它的文件? - Python: “OSError: [Errno 2] No such file or directory:”… with the file it found? OSError: [Errno 2] 没有那个文件/目录 - OSError: [Errno 2] No such file/directory Python多处理池,OSError:Errno 2没有这样的文件或目录 - Python multiprocessing Pool, OSError: Errno 2 No such file or directory Odoo:OSError:[Errno 2]没有这样的文件或目录 - Odoo: OSError: [Errno 2] No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM