script wanted: print the command - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Forum & Off Topic (https://python-forum.io/forum-23.html) +--- Forum: Bar (https://python-forum.io/forum-27.html) +--- Thread: script wanted: print the command (/thread-6790.html) |
script wanted: print the command - Skaperen - Dec-08-2017 this one should be simple enough, maybe even for newbies. i would a script that is given a command in its arguments, prints the command to stderr, runs the command given to it, then if the given command it runs has an exit code different than zero, prints the exit code to stderr, then exits with the exit code of the given command it runs. RE: script wanted: print the command - DeaD_EyE - Dec-08-2017 It's like reading the docs from subprocess... #!/usr/bin/env python3 from subprocess import Popen, PIPE import sys args = sys.argv[1:] print('Program:', args[0]) print('Arguments:', args[1:]) proc = Popen(args, shell=False, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate() exitcode = proc.wait() print('Exitcode:', exitcode) print('Stdout:', stdout.decode()) print('Stderr:', stderr.decode()) sys.exit(exitcode)This example prints always: program, arguments, exitcode, stdout, stderr This program does not work with tools like midnight commander or screen. RE: script wanted: print the command - Skaperen - Dec-09-2017 i don't know about midnight commander, but, why would it not work with screen? oh, the output might not be seen until screen exits? my old bash version works with screen. RE: script wanted: print the command - DeaD_EyE - Dec-10-2017 Midnight Commander, screen, tmux and bash are interactive programs which are modifying the terminal. My example opens the process and then it uses the method communicate() .This is a blocking call until the process has been terminated. Conclusion: This program would never show the output from an interactive process and it would not end. RE: script wanted: print the command - Skaperen - Dec-11-2017 then. the problem is that the user cannot interact with the program being run, when it is run that way. also consider the ping command. i think you will have a problem with that command, too, unless you use the -c option with a reasonably low number.maybe you need to be using subprocess.call(sys.argv[1:]) to run the command.
RE: script wanted: print the command - wavic - Dec-12-2017 In [1]: from scapy.all import IP, ICMP, sr1 WARNING: No route found for IPv6 destination : : (no default route?). This affects only IPv6 In [2]: packet = IP() In [3]: packet.dst = '8.8.8.8' In [4]: icmp = ICMP() # echo request In [5]: response = sr1(packet/icmp) Begin emission: .Finished to send 1 packets. * Received 2 packets, got 1 answers, remaining 0 packets In [6]: response Out[6]: <IP version=4 ihl=5 tos=0x0 len=28 id=4349 flags= frag=0 ttl=59 proto=icmp chksum=0x3a29 src=8.8.8.8 dst=192.168.100.3 options= |<ICMP type=echo-reply code=0 chksum=0x0 id=0x0 seq=0x0 |>> |