Re: [Python] Os.popen e comando route add
Grazie questo codice e molto utile per la mia autoformazione, credo che ne trarro benefico per il mio plugin di nagios. Grazie infinite. Il mio script per i check dei gw in nagios l'ho inserito in https://github.com/luupux/check_gw Devo ancora terminarlo con le procedure di route add e del Il 17 gennaio 2011 20:07, Daniele Tricoli ha scritto: > On Monday 17 January 2011 13:14:23 LuupuxAll wrote: >> Grazie per la dritta > > Leggo soltanto adesso, ma forse ti può interessare un wrapper che scrissi > tempo fa e che utilizza proprio subprocess: > http://dev.hinezumi.org/browser/netsukuku/trunk/pyntk/ntk/network/linux/adapt.py > > HTH, > > -- > Daniele Tricoli 'Eriol' > http://mornie.org > ___ > Python mailing list > Python@lists.python.it > http://lists.python.it/mailman/listinfo/python > ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Os.popen e comando route add
On Monday 17 January 2011 13:14:23 LuupuxAll wrote: > Grazie per la dritta Leggo soltanto adesso, ma forse ti può interessare un wrapper che scrissi tempo fa e che utilizza proprio subprocess: http://dev.hinezumi.org/browser/netsukuku/trunk/pyntk/ntk/network/linux/adapt.py HTH, -- Daniele Tricoli 'Eriol' http://mornie.org ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Os.popen e comando route add
Grazie per la dritta Il 17 gennaio 2011 12:47, Giampaolo Rodolà ha scritto: > Spesso mi porto dietro questa funzione in molti script per fare lavori > sporchi/veloci: > > import subprocess, warnings > > def sh(cmdline): > """run cmd in a subprocess and return its output. > raises RuntimeError on error. > """ > p = subprocess.Popen(cmdline, shell=True, stdout=subprocess.PIPE, > stderr=subprocess.PIPE) > stdout, stderr = p.communicate() > if p.returncode != 0: > raise RuntimeError(stderr) > if stderr: > warnings.warn(stderr, RuntimeWarning) > return stdout > > >>> sh("whoami") > 'giampaolo\n' > >>> sh("blabla") > Traceback (most recent call last): > File "", line 1, in > File "_utils.py", line 12, in sh > raise RuntimeError(stderr) > RuntimeError: /bin/sh: blabla: command not found > >>> > > A partire da python 2.7 puoi usare subprocess.check_output() che però > trovo meno comoda: > http://docs.python.org/library/subprocess.html#subprocess.check_output > > > --- Giampaolo > http://code.google.com/p/pyftpdlib/ > http://code.google.com/p/psutil/ > > > > Il 15 gennaio 2011 12:27, Paolo Bernardi ha scritto: >> Tra le altre cose, se vuoi restare aggiornato, ti consiglio di dare >> un'occhiata al modulo subprocess, particolarmente alla sezione su come >> rimpiazzare le chiamate os.popen*: >> >> http://docs.python.org/library/subprocess.html#replacing-os-popen-os-popen2-os-popen3 >> >> Con la popen, ad esempio, mi è capitato di fare qualcosa di simile: >> >> p = subprocess.Popen(['axel', '-o', file, self.url], shell=False, >> stdout=subprocess.PIPE) >> line = p.stdout.readline() >> while line != None and line != '' and not p.poll(): >> [...] >> line = p.stdout.readline() >> >> In questo caso ovviamente mi serviva l'output riga per riga (man mano >> che axel avanzava), ma puoi anche leggere tutto in blocco. >> >> Paolo >> >> On Fri, 2011-01-14 at 12:03 +0100, Stefano Dal Pra wrote: >>> Ciao, >>> prova con os.popen3 : >>> stdin,stdou,stderr = os.popen("ping 151.1.1.1") >>> >>> dovresti scoprire che il SIOCADDRT compare con stderr.readline() ... >>> >>> Stefano >> >> ___ >> Python mailing list >> Python@lists.python.it >> http://lists.python.it/mailman/listinfo/python >> >> > ___ > Python mailing list > Python@lists.python.it > http://lists.python.it/mailman/listinfo/python > ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Os.popen e comando route add
Spesso mi porto dietro questa funzione in molti script per fare lavori sporchi/veloci: import subprocess, warnings def sh(cmdline): """run cmd in a subprocess and return its output. raises RuntimeError on error. """ p = subprocess.Popen(cmdline, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() if p.returncode != 0: raise RuntimeError(stderr) if stderr: warnings.warn(stderr, RuntimeWarning) return stdout >>> sh("whoami") 'giampaolo\n' >>> sh("blabla") Traceback (most recent call last): File "", line 1, in File "_utils.py", line 12, in sh raise RuntimeError(stderr) RuntimeError: /bin/sh: blabla: command not found >>> A partire da python 2.7 puoi usare subprocess.check_output() che però trovo meno comoda: http://docs.python.org/library/subprocess.html#subprocess.check_output --- Giampaolo http://code.google.com/p/pyftpdlib/ http://code.google.com/p/psutil/ Il 15 gennaio 2011 12:27, Paolo Bernardi ha scritto: > Tra le altre cose, se vuoi restare aggiornato, ti consiglio di dare > un'occhiata al modulo subprocess, particolarmente alla sezione su come > rimpiazzare le chiamate os.popen*: > > http://docs.python.org/library/subprocess.html#replacing-os-popen-os-popen2-os-popen3 > > Con la popen, ad esempio, mi è capitato di fare qualcosa di simile: > > p = subprocess.Popen(['axel', '-o', file, self.url], shell=False, > stdout=subprocess.PIPE) > line = p.stdout.readline() > while line != None and line != '' and not p.poll(): > [...] > line = p.stdout.readline() > > In questo caso ovviamente mi serviva l'output riga per riga (man mano > che axel avanzava), ma puoi anche leggere tutto in blocco. > > Paolo > > On Fri, 2011-01-14 at 12:03 +0100, Stefano Dal Pra wrote: >> Ciao, >> prova con os.popen3 : >> stdin,stdou,stderr = os.popen("ping 151.1.1.1") >> >> dovresti scoprire che il SIOCADDRT compare con stderr.readline() ... >> >> Stefano > > ___ > Python mailing list > Python@lists.python.it > http://lists.python.it/mailman/listinfo/python > > ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Os.popen e comando route add
Tra le altre cose, se vuoi restare aggiornato, ti consiglio di dare un'occhiata al modulo subprocess, particolarmente alla sezione su come rimpiazzare le chiamate os.popen*: http://docs.python.org/library/subprocess.html#replacing-os-popen-os-popen2-os-popen3 Con la popen, ad esempio, mi è capitato di fare qualcosa di simile: p = subprocess.Popen(['axel', '-o', file, self.url], shell=False, stdout=subprocess.PIPE) line = p.stdout.readline() while line != None and line != '' and not p.poll(): [...] line = p.stdout.readline() In questo caso ovviamente mi serviva l'output riga per riga (man mano che axel avanzava), ma puoi anche leggere tutto in blocco. Paolo On Fri, 2011-01-14 at 12:03 +0100, Stefano Dal Pra wrote: > Ciao, > prova con os.popen3 : > stdin,stdou,stderr = os.popen("ping 151.1.1.1") > > dovresti scoprire che il SIOCADDRT compare con stderr.readline() ... > > Stefano signature.asc Description: This is a digitally signed message part ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Os.popen e comando route add
Grazie non ci avevo pensato il ping da lo stesso problema se si introduce un errore nella sintassi Il 14 gennaio 2011 12:44, ty ty ha scritto: > Grazie non ci avevo pensato il ping da lo stesso problema se si > introduce un errore nella sintassi > > RIsolto. > > > Il 14 gennaio 2011 12:03, Stefano Dal Pra ha scritto: >> Ciao, >> prova con os.popen3 : >> stdin,stdou,stderr = os.popen("ping 151.1.1.1") >> >> dovresti scoprire che il SIOCADDRT compare con stderr.readline() ... >> >> Stefano >> >> 2011/1/14 LuupuxAll >>> >>> CIao a tutti sono alle prime armi con python ed ho qualche problema >>> con os.open , nel particolare non riesco a capire quanto segue >>> >>> >>> otp=os.popen("ping 151.1.1.1") >>> >>> >>> >>> >>> E questo mi sta bene perche dopo con delle readline leggo il contenuto >>> mentre se provo questo >>> >>> >>> otp=os.popen("route add -host 3.3.3.3 10.0.1.250") >>> >>> >>> SIOCADDRT: No such device >>> >>> MI risponde con SIOCADDRT in output e questo è un problema, >>> Sapreste darmi qualche dritta , o meglio mi basterebbe anche capire la >>> differenza per il pitone tra i due comandi che ho dato . >>> >>> P.s >>> Sono legato alla versione 2.4.3 di python ma anche con la 2.6.5 ho lo >>> stesso problema , di seguito l'output dell'idle >>> >>> Python 2.4.3 (#1, Nov 11 2010, 13:34:43) >>> [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 >>> >>> Python 2.6.5 (r265:79063, Jun 4 2010, 21:43:07) >>> [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 >>> ___ >>> Python mailing list >>> Python@lists.python.it >>> http://lists.python.it/mailman/listinfo/python >> >> >> ___ >> Python mailing list >> Python@lists.python.it >> http://lists.python.it/mailman/listinfo/python >> >> > ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
Re: [Python] Os.popen e comando route add
Ciao, prova con os.popen3 : stdin,stdou,stderr = os.popen("ping 151.1.1.1") dovresti scoprire che il SIOCADDRT compare con stderr.readline() ... Stefano 2011/1/14 LuupuxAll > CIao a tutti sono alle prime armi con python ed ho qualche problema > con os.open , nel particolare non riesco a capire quanto segue > > >>> otp=os.popen("ping 151.1.1.1") > > >>> > > E questo mi sta bene perche dopo con delle readline leggo il contenuto > mentre se provo questo > > >>> otp=os.popen("route add -host 3.3.3.3 10.0.1.250") > > >>> SIOCADDRT: No such device > > MI risponde con SIOCADDRT in output e questo è un problema, > Sapreste darmi qualche dritta , o meglio mi basterebbe anche capire la > differenza per il pitone tra i due comandi che ho dato . > > P.s > Sono legato alla versione 2.4.3 di python ma anche con la 2.6.5 ho lo > stesso problema , di seguito l'output dell'idle > > Python 2.4.3 (#1, Nov 11 2010, 13:34:43) > [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 > > Python 2.6.5 (r265:79063, Jun 4 2010, 21:43:07) > [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 > ___ > Python mailing list > Python@lists.python.it > http://lists.python.it/mailman/listinfo/python > ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python
[Python] Os.popen e comando route add
CIao a tutti sono alle prime armi con python ed ho qualche problema con os.open , nel particolare non riesco a capire quanto segue >>> otp=os.popen("ping 151.1.1.1") >>> E questo mi sta bene perche dopo con delle readline leggo il contenuto mentre se provo questo >>> otp=os.popen("route add -host 3.3.3.3 10.0.1.250") >>> SIOCADDRT: No such device MI risponde con SIOCADDRT in output e questo è un problema, Sapreste darmi qualche dritta , o meglio mi basterebbe anche capire la differenza per il pitone tra i due comandi che ho dato . P.s Sono legato alla versione 2.4.3 di python ma anche con la 2.6.5 ho lo stesso problema , di seguito l'output dell'idle Python 2.4.3 (#1, Nov 11 2010, 13:34:43) [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 Python 2.6.5 (r265:79063, Jun 4 2010, 21:43:07) [GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2 ___ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python