[Tutor] string not found in variable
jark AJ
jarkmx at gmail.com
Sun Mar 21 20:55:43 EDT 2021
Thank you so much Cameron,Dennis,dn and Alan
Hi Cameron/Dennis,
Yes, it's logged in the stderr :). Thank you so much once
again!!!
Hi Alan,
Yes, Understood, the idea behind using a class instead of a function
is that looking to use the same class for any commands that we may need to
run in future.
Thank you again for your feedback, I will try to improve it.
THANK YOU ALL!!!!!
____
('Executing Command >>', 'nc -zvw 4 www.google.com 443')
Connection to www.google.com port 443 [tcp/https] succeeded!
<type 'str'>
True
___
import subprocess
class Command(object):
def __init__(self, cmd):
self.cmd = cmd
def run(self, shell=False):
try:
print("Executing Command >>", self.cmd)
output = subprocess.Popen(
self.cmd.split(), stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=shell
)
return output.communicate()
except subprocess.SubprocessError as error:
return error
def __str__(self):
return str(self.cmd)
for i in range(5):
command = Command("nc -zvw 4 www.google.com 443")
output, my_check = command.run()
print(my_check)
print(type(output))
print("succeeded!" in my_check)
__
On Sun, Mar 21, 2021 at 8:34 PM Dennis Lee Bieber <wlfraed at ix.netcom.com>
wrote:
> o/~ Talking to myself in public o/~
>
> On Sun, 21 Mar 2021 19:28:41 -0400, Dennis Lee Bieber
> <wlfraed at ix.netcom.com> declaimed the following:
>
> > As mentioned above, I'd likely just replace the above with the
> contents
> >of the run method, modified to put the actual command in needed places.
> >
>
> On a whim, I installed netcat into a Windows Subsystem for Linux
> Debian
> instance, and tried to produce "my version" of the above.
>
> Unfortunately, that version isn't producing any usable output -- a
> lot
> of warnings to stderr...
>
> wulfraed at ElusiveUnicorn:~$ python3 nctest.py
> DNS fwd/rev mismatch: www.google.com != dfw06s48-in-f100.1e100.net
> www.google.com [216.58.194.100] 443 (https) open
> DNS fwd/rev mismatch: www.google.com != dfw25s25-in-f4.1e100.net
> www.google.com [172.217.1.228] 443 (https) open
> DNS fwd/rev mismatch: www.google.com != dfw25s41-in-f4.1e100.net
> www.google.com [142.250.68.164] 443 (https) open
> DNS fwd/rev mismatch: www.google.com != dfw06s49-in-f4.1e100.net
> www.google.com [216.58.194.132] 443 (https) open
> DNS fwd/rev mismatch: www.google.com != dfw28s04-in-f4.1e100.net
> www.google.com [172.217.12.36] 443 (https) open
> b''
> False
> b''
> False
> b''
> False
> b''
> False
> b''
> False
> wulfraed at ElusiveUnicorn:~$
>
> The code is:
>
> wulfraed at ElusiveUnicorn:~$ cat nctest.py
> import subprocess
>
> def runner():
> try:
> myProcess = subprocess.Popen(
> "nc -zvw 4 www.google.com 443".split(),
> stdout=subprocess.PIPE,
> shell=False)
> return myProcess.communicate()[0]
> except subprocess.CalledProcessError as e:
> return str(e.returncode)
>
> output = [runner() for _ in range(5)]
> for ln in output:
> print(ln)
> print(b"success!" in ln)
>
> wulfraed at ElusiveUnicorn:~$
>
> Trying it in a full Debian running in Oracle VirtualBox produces
> similar results...
>
> wulfraed at debian:~$ python3 /media/VB_Shared/nctest.py
> DNS fwd/rev mismatch: www.google.com != dfw28s04-in-f4.1e100.net
> www.google.com [172.217.12.36] 443 (https) open
> Warning: inverse host lookup failed for 142.250.138.106: Host name lookup
> failure
> Warning: inverse host lookup failed for 142.250.138.103: Host name lookup
> failure
> Warning: inverse host lookup failed for 142.250.138.105: Host name lookup
> failure
> Warning: inverse host lookup failed for 142.250.138.104: Host name lookup
> failure
> Warning: inverse host lookup failed for 142.250.138.147: Host name lookup
> failure
> Warning: inverse host lookup failed for 142.250.138.99: Host name lookup
> failure
> www.google.com [142.250.138.106] 443 (https) open
> DNS fwd/rev mismatch: www.google.com != dfw28s04-in-f4.1e100.net
> www.google.com [172.217.12.36] 443 (https) open
> DNS fwd/rev mismatch: www.google.com != ord38s09-in-f4.1e100.net
> www.google.com [172.217.9.68] 443 (https) open
> DNS fwd/rev mismatch: www.google.com != ord38s09-in-f4.1e100.net
> www.google.com [172.217.9.68] 443 (https) open
> b''
> False
> b''
> False
> b''
> False
> b''
> False
> b''
> False
> wulfraed at debian:~$
>
> Changing the code to make the output more legible (or meaningful)
>
> wulfraed at debian:~$ cat /media/VB_Shared/nctest.py
> import subprocess
>
> def runner():
> try:
> myProcess = subprocess.Popen(
> "nc -zvw 4 www.google.com 443".split(),
> stdout=subprocess.PIPE,
> stderr=subprocess.PIPE,
> shell=False)
> return myProcess.communicate()
> except subprocess.CalledProcessError as e:
> return str(e.returncode)
>
> output = [runner() for _ in range(5)]
> for ln in output:
> print("\n\nstdout = %s\nstderr = %s\n" % ln)
> print("\tTest: %s" % (b"success!" in ln[0]))
>
> wulfraed at debian:~$ python3 /media/VB_Shared/nctest.py
>
>
> stdout = b''
> stderr = b'DNS fwd/rev mismatch: www.google.com !=
> lga15s49-in-f4.1e100.net\nwww.google.com [172.217.5.4] 443 (https) open\n'
>
> Test: False
>
>
> stdout = b''
> stderr = b'DNS fwd/rev mismatch: www.google.com !=
> ord37s07-in-f4.1e100.net\nwww.google.com [172.217.1.36] 443 (https)
> open\n'
>
> Test: False
>
>
> stdout = b''
> stderr = b'DNS fwd/rev mismatch: www.google.com !=
> dfw25s17-in-f4.1e100.net\nwww.google.com [172.217.6.164] 443 (https)
> open\n'
>
> Test: False
>
>
> stdout = b''
> stderr = b'DNS fwd/rev mismatch: www.google.com !=
> lga15s46-in-f36.1e100.net\nwww.google.com [172.217.4.36] 443 (https)
> open\n'
>
> Test: False
>
>
> stdout = b''
> stderr = b'DNS fwd/rev mismatch: www.google.com !=
> ord37s08-in-f4.1e100.net\nwww.google.com [172.217.8.164] 443 (https)
> open\n'
>
> Test: False
> wulfraed at debian:~$
>
>
>
> --
> Wulfraed Dennis Lee Bieber AF6VN
> wlfraed at ix.netcom.com
> http://wlfraed.microdiversity.freeddns.org/
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list