Python Forum
script wanted: print the command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
script wanted: print the command
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
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 |>>
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  a tree command using Shell Script that displays all the directories recursively apollo 1 1,925 Nov-18-2021, 10:04 PM
Last Post: BashBedlam
  script wanted: UNboxing database output Skaperen 0 1,677 Oct-27-2018, 03:04 AM
Last Post: Skaperen
  command line progam wanted: clock Skaperen 2 2,674 Apr-18-2018, 06:54 AM
Last Post: Gribouillis
  script wanted: clock for text mode screen Skaperen 2 2,970 Dec-26-2017, 01:57 AM
Last Post: Skaperen
  script wanted: ping and wait Skaperen 0 2,278 Dec-10-2017, 05:16 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020