strange behaviour sys.argv

S

schnupfy

Hi,

I am not used to python and I am wondering about this thing:

If I execute this from the shell:

/root/mk/services.py 192.168.1.101 critical "192.168.1.101
192.168.1.101 SNMPv2-MIB::sysUpTime.0 14:13:02:57.06 SNMPv2-
MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.789.0.13 SNMPv2-
SMI::enterprises.789.0.2"cfCannotTakeover == 1 priority == critical"
SNMP-COMMUNITY-MIB::snmpTrapAddress.0 192.168.1.101 SNMP-COMMUNITY-
MIB::snmpTrapCommunity.0 "public""

I have the following cmd arguments:

['/root/mk/services.py', '192.168.1.101', 'critical', '192.168.1.101
192.168.1.101 SNMPv2-MIB::sysUpTime.0 14:13:02:57.06 SNMPv2-
MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.789.0.13 SNMPv2-
SMI::enterprises.789.0.2cfCannotTakeover', '==', '1', 'priority',
'==', 'critical SNMP-COMMUNITY-MIB::snmpTrapAddress.0 192.168.1.101
SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 public']

If I execute the same thing from a bash script:

#!/bin/bash
TRAP='192.168.1.101 192.168.1.101 SNMPv2-MIB::sysUpTime.0
14:13:02:57.06 SNMPv2-MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.
789.0.13 SNMPv2-SMI::enterprises.789.0.2"cfCannotTakeover == 1
priority == critical" SNMP-COMMUNITY-MIB::snmpTrapAddress.0
192.168.1.101 SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 "public"'
HOST=$(echo "$TRAP" | awk '{print $1}')
SEVERITY='critical'
/root/mk/services.py $HOST $SEVERITY \"$TRAP\"

I get the following result:

['/root/mk/services.py', '192.168.1.101', 'critical',
'"192.168.1.101', '192.168.1.101', 'SNMPv2-MIB::sysUpTime.0',
'14:13:02:57.06', 'SNMPv2-MIB::snmpTrapOID.0', 'SNMPv2-
SMI::enterprises.789.0.13', 'SNMPv2-SMI::enterprises.
789.0.2"cfCannotTakeover', '==', '1', 'priority', '==', 'critical"',
'SNMP-COMMUNITY-MIB::snmpTrapAddress.0', '192.168.1.101', 'SNMP-
COMMUNITY-MIB::snmpTrapCommunity.0', '"public""']

Can someone help me with that?

This is the output of echo /root/mk/services.py $HOST $SEVERITY \"$TRAP
\"

/root/mk/services.py 192.168.1.101 critical "192.168.1.101
192.168.1.101 SNMPv2-MIB::sysUpTime.0 14:13:02:57.06 SNMPv2-
MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.789.0.13 SNMPv2-
SMI::enterprises.789.0.2"cfCannotTakeover == 1 priority == critical"
SNMP-COMMUNITY-MIB::snmpTrapAddress.0 192.168.1.101 SNMP-COMMUNITY-
MIB::snmpTrapCommunity.0 "public""

Thank you.

Cheers

Marcus
 
M

Michael Hoffman

schnupfy said:
I am not used to python and I am wondering about this thing:

This is not a Python question. It is a question about how to use bash.

I would try to help anyway, but I am unsure what results you actually
want. Your example is too complicated as well. You should strip down
your example to only the arguments that change. In doing this you may
solve the problem on your own.
 
C

Charles Sanders

Michael said:
This is not a Python question. It is a question about how to use bash.
[snip]

Michael is correct, it is a bash thing, nothing to do with python.
bash (and other *nix like shells) generally break arguments on
white space. Quoting (both single and double) overrides this with
(slightly) different rules for different shells.
> /root/mk/services.py 192.168.1.101 critical "192.168.1.101
> 192.168.1.101 SNMPv2-MIB::sysUpTime.0 14:13:02:57.06 SNMPv2-
> MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.789.0.13 SNMPv2-
> SMI::enterprises.789.0.2"cfCannotTakeover == 1 priority == critical"
> SNMP-COMMUNITY-MIB::snmpTrapAddress.0 192.168.1.101 SNMP-COMMUNITY-
> MIB::snmpTrapCommunity.0 "public""

Assuming this has been folded and actually is one long
line (which the output confirms), you have passed the python
script seven arguments

'192.168.1.101' (blank seperated)
'critical' (also blank seperated)
a string extending from just after the first double quote to
just before the second, ie starting with '192.168.1.101' and
ending with '789.0.2', with the immediately following (no
white space) unquoted text 'fCannotTakeover' appended
'==' (blank seperated)
'priority"
'=='
a string starting with critical, with the quoted string from
'SNMP-COMMUNITY' to 'Community.0 ' (including the blank), the
unquoted string 'public', and the null quoted string "" all
appended.
> TRAP='192.168.1.101 192.168.1.101 SNMPv2-MIB::sysUpTime.0
> 14:13:02:57.06 SNMPv2-MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.
> 789.0.13 SNMPv2-SMI::enterprises.789.0.2"cfCannotTakeover == 1
> priority == critical" SNMP-COMMUNITY-MIB::snmpTrapAddress.0
> 192.168.1.101 SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 "public"'
> HOST=$(echo "$TRAP" | awk '{print $1}')
> SEVERITY='critical'
> /root/mk/services.py $HOST $SEVERITY \"$TRAP\"

Here, the variables are expanded, and then split into
arguments on white space unless quoted. The backslashes protect
the double quotes so they are treated as normal characters, so
the $TRAP variable is also split into arguments on white space.
Quotes resulting from the substitution of $TRAP are also protected
(ie are treated as ordinary characters).

The result is

'192.168.1.101" (From $HOST)
'critical' (From $SEVERITY)
'"192.168.1.101' (Leading '"' from \", rest from
$TRAP, blank seperated)
'192.168.1.101' (from $TRAP, blank seperated)
'SNMPv2-MIB::sysUpTime.0'
and so on for the rest of the $TARP string, splitting it at
white space. The last part of $TRAP, '"public"', has a double
quote appended from the \".

Python is giving exactly what the shell has given it in both cases.

Charles
 
S

schnupfy

Michael said:
schnupfy wrote:
This is not a Python question. It is a question about how to use bash.

[snip]

Michael is correct, it is a bash thing, nothing to do with python.
bash (and other *nix like shells) generally break arguments on
white space. Quoting (both single and double) overrides this with
(slightly) different rules for different shells.
/root/mk/services.py 192.168.1.101 critical "192.168.1.101
192.168.1.101 SNMPv2-MIB::sysUpTime.0 14:13:02:57.06 SNMPv2-
MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.789.0.13 SNMPv2-
SMI::enterprises.789.0.2"cfCannotTakeover == 1 priority == critical"
SNMP-COMMUNITY-MIB::snmpTrapAddress.0 192.168.1.101 SNMP-COMMUNITY-
MIB::snmpTrapCommunity.0 "public""

Assuming this has been folded and actually is one long
line (which the output confirms), you have passed the python
script seven arguments

'192.168.1.101' (blank seperated)
'critical' (also blank seperated)
a string extending from just after the first double quote to
just before the second, ie starting with '192.168.1.101' and
ending with '789.0.2', with the immediately following (no
white space) unquoted text 'fCannotTakeover' appended
'==' (blank seperated)
'priority"
'=='
a string starting with critical, with the quoted string from
'SNMP-COMMUNITY' to 'Community.0 ' (including the blank), the
unquoted string 'public', and the null quoted string "" all
appended.
TRAP='192.168.1.101 192.168.1.101 SNMPv2-MIB::sysUpTime.0
14:13:02:57.06 SNMPv2-MIB::snmpTrapOID.0 SNMPv2-SMI::enterprises.
789.0.13 SNMPv2-SMI::enterprises.789.0.2"cfCannotTakeover == 1
priority == critical" SNMP-COMMUNITY-MIB::snmpTrapAddress.0
192.168.1.101 SNMP-COMMUNITY-MIB::snmpTrapCommunity.0 "public"'
HOST=$(echo "$TRAP" | awk '{print $1}')
SEVERITY='critical'
/root/mk/services.py $HOST $SEVERITY \"$TRAP\"

Here, the variables are expanded, and then split into
arguments on white space unless quoted. The backslashes protect
the double quotes so they are treated as normal characters, so
the $TRAP variable is also split into arguments on white space.
Quotes resulting from the substitution of $TRAP are also protected
(ie are treated as ordinary characters).

The result is

'192.168.1.101" (From $HOST)
'critical' (From $SEVERITY)
'"192.168.1.101' (Leading '"' from \", rest from
$TRAP, blank seperated)
'192.168.1.101' (from $TRAP, blank seperated)
'SNMPv2-MIB::sysUpTime.0'
and so on for the rest of the $TARP string, splitting it at
white space. The last part of $TRAP, '"public"', has a double
quote appended from the \".

Python is giving exactly what the shell has given it in both cases.

Charles

ok, thanks for the answers. I try to hand over the 3rd part (the long
trap) as one cmd argument. I will ask in a shell ng. Thanks again.

Cheers

Marcus
 
C

Charles Sanders

schnupfy said:
ok, thanks for the answers. I try to hand over the 3rd part (the long
trap) as one cmd argument. I will ask in a shell ng. Thanks again.

Cheers

Should be as simple as removing the backslashes

/root/mk/services.py $HOST $SEVERITY "$TRAP"

should pass TRAP as a single arg

Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top