patch proposal on Ssh probe

32 views
Skip to first unread message

mathias louiset

unread,
Feb 10, 2012, 12:21:45 PM2/10/12
to testerm...@googlegroups.com

 

Hi all,

 

I went to troubles while using the SSH probe: my command line was too long, and SSH probe split it at 150 characters. But it was just in the middle of a replacement string in my “sed” command… and of course the result was not the one I expected.

So I would like to suggest the following patch in file testerman/plugins/probe/ssh/SshProbe.py:

 

--- SshProbe.py.orig    2012-02-10 17:24:27.561633678 +0100

+++ SshProbe.py 2012-02-10 17:50:59.539372205 +0100

@@ -285,11 +285,13 @@

                status = None

                try:

                        # Split the command on multiple lines - the pseudo terminal may limit it ??

-                       i = 0

                        size = MAX_TERMINAL_LINE_LENGTH

                        splitcmd = []

-                       while i < len(self._command):

-                               splitcmd.append(self._command[i:i+size])

+                       presplitcmd = self._command.split('\n')

+                       for cmd in presplitcmd:

+                               i = 0

+                               while i < len(cmd):

+                                       splitcmd.append(cmd[i:i+size])

                                i += size

 

                        self._ssh.sendline('\\\n'.join(splitcmd))

 

Thanks,

Mathias

Sebastien Lefevre

unread,
Feb 11, 2012, 8:17:31 AM2/11/12
to testerm...@googlegroups.com, mathias louiset
Hi Mathias,

would you share the command line you're trying to execute ?

Actually, the patch you proposed breaks user-provided multiline commands; I played a little around it to keep this support, but would like to test with your specific case to understand why the initial version did not work with you.

thanks,
-seb



Le 10/02/2012 18:21, mathias louiset a �crit�:

�

Hi all,

�

I went to troubles while using the SSH probe: my command line was too long, and SSH probe split it at 150 characters. But it was just in the middle of a replacement string in my �sed� command� and of course the result was not the one I expected.

So I would like to suggest the following patch in file testerman/plugins/probe/ssh/SshProbe.py:

�

--- SshProbe.py.orig��� 2012-02-10 17:24:27.561633678 +0100

+++ SshProbe.py 2012-02-10 17:50:59.539372205 +0100

@@ -285,11 +285,13 @@

��������������� status = None

��������������� try:

����������������������� # Split the command on multiple lines - the pseudo terminal may limit it ??

-���������������������� i = 0

����������������������� size = MAX_TERMINAL_LINE_LENGTH

����������������������� splitcmd = []

-���������������������� while i < len(self._command):

-������������������������������ splitcmd.append(self._command[i:i+size])

+���������������������� presplitcmd = self._command.split('\n')

+���������������������� for cmd in presplitcmd:

+������������������������������ i = 0

+��������� ���������������������while i < len(cmd):

+�������������������������������������� splitcmd.append(cmd[i:i+size])

������������������������������� i += size

�

����������������������� self._ssh.sendline('\\\n'.join(splitcmd))

�

Thanks,

Mathias

mathias louiset

unread,
Feb 13, 2012, 4:09:27 AM2/13/12
to Sebastien Lefevre, testerm...@googlegroups.com

Sure,

 

Here was the situation before I tried to patch SSH probe:

 

I have a class which is in charge of replacing values in configuration files; in this class the function “changeValue” build the command line, and ask SSH probe to run the command remotely (this is done in sendSshCmd method):

                               

class RemoteConfFileManager:

                def changeValue(self, modifications, separator = '='):

                                """

                                Change the value of the key in a configuration-like file

                                @param modifications: list of tuple (<name>, <newValue>) where name is the name

                                of the key entry to change, and <newValue> is the new value for that entry key

                                @type modifications: tuple

                                @param separator: caracter used to separate key and value in target file format.

                                @type separator: string

                                @return: True if changes succeed, False otherwise.

                                @rtype: boolean

                                """

                                _sedCmd = ""

                                for _pair in modifications:

                                                _sedCmd += " -e 's|^%s.*%s.*|%s%s%s|'" % (_pair[0], separator, _pair[0], separator, _pair[1])

                                _ret, _output = ongManager.sendSshCmd(sshPort = self._sshPort, cmd = "sed -i %s %s" % (_sedCmd, self._remoteFileName),

                                                expectedResult = not_(pattern(r'No such file or directory')))

                                if not _ret:

                                                log("changeValue fails:\n%s" % _output)

                               

                                return _ret

                               

 

In my ATS, I then do something like this:

remoteConfFileManager.changeValue([ ("ManagementServer.URL", "http://%s/openacs/acs" % acsIpPort), ("ManagementServer.STUNEnable", "false")  ])

 

so my command line looks like:

sed -i -e 's|^ManagementServer.URL.*=.*|ManagementServer.URL=http://10.10.12.216:8181/openacs/acs|' -e 's|^ManagementServer.STUNEnable.*=.*|ManagementServer.STUNEnable=false|' /home/ong//install/factory/cfg/tr069Settings.cfg

 

My configuration file looks like this:

ManagementServer.URL=http://openacs-server.actility.com:8181/openacs/acs

ManagementServer.STUNEnable=true

 

and I got the following result with the original ssh probe:

ManagementServer.URL=http://10.10.12.216:8181/openacs/acs

Managemen

tServer.STUNEnable=false

 

 

So additionally to the patch I propose, I also modified my function this way:

                def changeValue(self, modifications, separator = '='):

                                """

                                Change the value of the key in a configuration-like file

                                @param modifications: list of tuple (<name>, <newValue>) where name is the name

                                of the key entry to change, and <newValue> is the new value for that entry key

                                @type modifications: tuple

                                @param separator: caracter used to separate key and value in target file format.

                                @type separator: string

                                @return: True if changes succeed, False otherwise.

                                @rtype: boolean

                                """

                                _sedCmd = ""

                                for _pair in modifications:

                                                _sedCmd += " -e 's|^%s.*%s.*|%s%s%s|' \n" % (_pair[0], separator, _pair[0], separator, _pair[1])

                                _ret, _output = ongManager.sendSshCmd(sshPort = self._sshPort, cmd = "sed -i %s %s" % (_sedCmd, self._remoteFileName),

                                                expectedResult = not_(pattern(r'No such file or directory')))

                                if not _ret:

                                                log("changeValue fails:\n%s" % _output)

                               

                                return _ret

                               

 

 

maybe ‘\n’ is not the better choice here…

 

Mathias

 

From: Sebastien Lefevre [mailto:seb.l...@gmail.com]
Sent: samedi 11 février 2012 14:18
To: testerm...@googlegroups.com
Cc: mathias louiset
Subject: Re: [testerman-users] patch proposal on Ssh probe

 

Hi Mathias,

would you share the command line you're trying to execute ?

Actually, the patch you proposed breaks user-provided multiline commands; I played a little around it to keep this support, but would like to test with your specific case to understand why the initial version did not work with you.

thanks,
-seb



Le 10/02/2012 18:21, mathias louiset a écrit :

 

Hi all,

 

I went to troubles while using the SSH probe: my command line was too long, and SSH probe split it at 150 characters. But it was just in the middle of a replacement string in my “sed” command… and of course the result was not the one I expected.

So I would like to suggest the following patch in file testerman/plugins/probe/ssh/SshProbe.py:

 

--- SshProbe.py.orig    2012-02-10 17:24:27.561633678 +0100

+++ SshProbe.py 2012-02-10 17:50:59.539372205 +0100

@@ -285,11 +285,13 @@

                status = None

                try:

                        # Split the command on multiple lines - the pseudo terminal may limit it ??

-                       i = 0

Sebastien Lefevre

unread,
Feb 13, 2012, 8:09:18 AM2/13/12
to mathias louiset, testerm...@googlegroups.com
Hi Mathias,

As I'll need to rework the command line splitting algorithm to correctly take into account quoted arguments (with support for multiple platforms), I committed a workaround for your case in http://www.testerman.fr/testerman/changeset/b4ea86cc088785e3992322906eeebf1dcce2c84c.

You may add the property max_line_length to your SSH probe binding, and set it to something bigger than the default (150) to avoid the line split, for instance 500.

Increasing this length should do the trick on most platforms, but splitting the line with a correct algorithm will still be needed for extreme cases or when running under Solaris and the likes.

thanks,
-seb



Le 13/02/2012 10:09, mathias louiset a �crit�:

Sure,

�

Here was the situation before I tried to patch SSH probe:

�

I have a class which is in charge of replacing values in configuration files; in this class the function �changeValue� build the command line, and ask SSH probe to run the command remotely (this is done in sendSshCmd method):

�������������������������������

class RemoteConfFileManager:

�

��������������� def changeValue(self, modifications, separator = '='):

������������������������������� """

������������������������������� Change the value of the key in a configuration-like file

������������������������������� @param modifications: list of tuple (<name>, <newValue>) where name is the name

������������������������������� of the key entry to change, and <newValue> is the new value for that entry key

������������������������������� @type modifications: tuple

������������������������������� @param separator: caracter used to separate key and value in target file format.

������������������������������� @type separator: string

������������������������������� @return: True if changes succeed, False otherwise.

������������������������������� @rtype: boolean

������������������������������� """

������������������������������� _sedCmd = ""

������������������������������� for _pair in modifications:

����������������������������������������������� _sedCmd += " -e 's|^%s.*%s.*|%s%s%s|'" % (_pair[0], separator, _pair[0], separator, _pair[1])

������������������������������� _ret, _output = ongManager.sendSshCmd(sshPort = self._sshPort, cmd = "sed -i %s %s" % (_sedCmd, self._remoteFileName),

����������������������������������������������� expectedResult = not_(pattern(r'No such file or directory')))

������������������������������� if not _ret:

����������������������������������������������� log("changeValue fails:\n%s" % _output)

�������������������������������

������������������������������� return _ret

�������������������������������

�

In my ATS, I then do something like this:

remoteConfFileManager.changeValue([ ("ManagementServer.URL", "http://%s/openacs/acs" % acsIpPort), ("ManagementServer.STUNEnable", "false")� ])

�

so my command line looks like:

sed -i -e 's|^ManagementServer.URL.*=.*|ManagementServer.URL=http://10.10.12.216:8181/openacs/acs|' -e 's|^ManagementServer.STUNEnable.*=.*|ManagementServer.STUNEnable=false|' /home/ong//install/factory/cfg/tr069Settings.cfg

�

My configuration file looks like this:

ManagementServer.URL=http://openacs-server.actility.com:8181/openacs/acs

ManagementServer.STUNEnable=true

�

and I got the following result with the original ssh probe:

ManagementServer.URL=http://10.10.12.216:8181/openacs/acs

Managemen

tServer.STUNEnable=false

�

�

So additionally to the patch I propose, I also modified my function this way:

��������������� def changeValue(self, modifications, separator = '='):

������������������������������� """

������������������������������� Change the value of the key in a configuration-like file

������������������������������� @param modifications: list of tuple (<name>, <newValue>) where name is the name

������������������������������� of the key entry to change, and <newValue> is the new value for that entry key

������������������������������� @type modifications: tuple

������������������������������� @param separator: caracter used to separate key and value in target file format.

������������������������������� @type separator: string

������������������������������� @return: True if changes succeed, False otherwise.

������������������������������� @rtype: boolean

������������������������������� """

������������������������������� _sedCmd = ""

������������������������������� for _pair in modifications:

����������������������������������������������� _sedCmd += " -e 's|^%s.*%s.*|%s%s%s|' \n" % (_pair[0], separator, _pair[0], separator, _pair[1])

������������������������������� _ret, _output = ongManager.sendSshCmd(sshPort = self._sshPort, cmd = "sed -i %s %s" % (_sedCmd, self._remoteFileName),

����������������������������������������������� expectedResult = not_(pattern(r'No such file or directory')))

������������������������������� if not _ret:

����������������������������������������������� log("changeValue fails:\n%s" % _output)

�������������������������������

������������������������������� return _ret

�������������������������������

�

�

maybe �\n� is not the better choice here�

�

Mathias

�

From: Sebastien Lefevre [mailto:seb.l...@gmail.com]
Sent: samedi 11 f�vrier 2012 14:18
To: testerm...@googlegroups.com
Cc: mathias louiset
Subject: Re: [testerman-users] patch proposal on Ssh probe

�

Hi Mathias,

would you share the command line you're trying to execute ?

Actually, the patch you proposed breaks user-provided multiline commands; I played a little around it to keep this support, but would like to test with your specific case to understand why the initial version did not work with you.

thanks,
-seb



Le 10/02/2012 18:21, mathias louiset a �crit�:

�

Hi all,

�

I went to troubles while using the SSH probe: my command line was too long, and SSH probe split it at 150 characters. But it was just in the middle of a replacement string in my �sed� command� and of course the result was not the one I expected.

So I would like to suggest the following patch in file testerman/plugins/probe/ssh/SshProbe.py:

�

--- SshProbe.py.orig��� 2012-02-10 17:24:27.561633678 +0100

+++ SshProbe.py 2012-02-10 17:50:59.539372205 +0100

@@ -285,11 +285,13 @@

��������������� status = None

Reply all
Reply to author
Forward
0 new messages