提交 a2b5c57d 编写于 作者: G Guido Günther

Autodetect if the remote nc command supports the -q option

Based on a patch by Marc Deslauriers <marc.deslauriers@ubuntu.com>

RH: https://bugzilla.redhat.com/show_bug.cgi?id=562176
Ubuntu: https://bugs.launchpad.net/ubuntu/+source/libvirt/+bug/517478
Debian: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=573172
上级 8f8258e1
...@@ -634,9 +634,29 @@ int virNetSocketNewConnectSSH(const char *nodename, ...@@ -634,9 +634,29 @@ int virNetSocketNewConnectSSH(const char *nodename,
"-e", "none", NULL); "-e", "none", NULL);
if (noVerify) if (noVerify)
virCommandAddArgList(cmd, "-o", "StrictHostKeyChecking=no", NULL); virCommandAddArgList(cmd, "-o", "StrictHostKeyChecking=no", NULL);
virCommandAddArgList(cmd, nodename,
netcat ? netcat : "nc", if (!netcat)
"-U", path, NULL); netcat = "nc";
virCommandAddArgList(cmd, nodename, "sh", "-c", NULL);
/*
* This ugly thing is a shell script to detect availability of
* the -q option for 'nc': debian and suse based distros need this
* flag to ensure the remote nc will exit on EOF, so it will go away
* when we close the connection tunnel. If it doesn't go away, subsequent
* connection attempts will hang.
*
* Fedora's 'nc' doesn't have this option, and defaults to the desired
* behavior.
*/
virCommandAddArgFormat(cmd,
"'if %s -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
"ARG=-q0;"
"else "
"ARG=;"
"fi;"
"%s $ARG -U %s'",
netcat, netcat, path);
return virNetSocketNewConnectCommand(cmd, retsock); return virNetSocketNewConnectCommand(cmd, retsock);
} }
......
...@@ -496,7 +496,12 @@ mymain(void) ...@@ -496,7 +496,12 @@ mymain(void)
struct testSSHData sshData1 = { struct testSSHData sshData1 = {
.nodename = "somehost", .nodename = "somehost",
.path = "/tmp/socket", .path = "/tmp/socket",
.expectOut = "somehost nc -U /tmp/socket\n", .expectOut = "somehost sh -c 'if nc -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
"ARG=-q0;"
"else "
"ARG=;"
"fi;"
"nc $ARG -U /tmp/socket'\n",
}; };
if (virtTestRun("SSH test 1", 1, testSocketSSH, &sshData1) < 0) if (virtTestRun("SSH test 1", 1, testSocketSSH, &sshData1) < 0)
ret = -1; ret = -1;
...@@ -509,7 +514,13 @@ mymain(void) ...@@ -509,7 +514,13 @@ mymain(void)
.noTTY = true, .noTTY = true,
.noVerify = false, .noVerify = false,
.path = "/tmp/socket", .path = "/tmp/socket",
.expectOut = "-p 9000 -l fred -T -o BatchMode=yes -e none somehost netcat -U /tmp/socket\n", .expectOut = "-p 9000 -l fred -T -o BatchMode=yes -e none somehost sh -c '"
"if netcat -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
"ARG=-q0;"
"else "
"ARG=;"
"fi;"
"netcat $ARG -U /tmp/socket'\n",
}; };
if (virtTestRun("SSH test 2", 1, testSocketSSH, &sshData2) < 0) if (virtTestRun("SSH test 2", 1, testSocketSSH, &sshData2) < 0)
ret = -1; ret = -1;
...@@ -522,7 +533,13 @@ mymain(void) ...@@ -522,7 +533,13 @@ mymain(void)
.noTTY = false, .noTTY = false,
.noVerify = true, .noVerify = true,
.path = "/tmp/socket", .path = "/tmp/socket",
.expectOut = "-p 9000 -l fred -o StrictHostKeyChecking=no somehost netcat -U /tmp/socket\n", .expectOut = "-p 9000 -l fred -o StrictHostKeyChecking=no somehost sh -c '"
"if netcat -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
"ARG=-q0;"
"else "
"ARG=;"
"fi;"
"netcat $ARG -U /tmp/socket'\n",
}; };
if (virtTestRun("SSH test 3", 1, testSocketSSH, &sshData3) < 0) if (virtTestRun("SSH test 3", 1, testSocketSSH, &sshData3) < 0)
ret = -1; ret = -1;
...@@ -538,7 +555,13 @@ mymain(void) ...@@ -538,7 +555,13 @@ mymain(void)
struct testSSHData sshData5 = { struct testSSHData sshData5 = {
.nodename = "crashyhost", .nodename = "crashyhost",
.path = "/tmp/socket", .path = "/tmp/socket",
.expectOut = "crashyhost nc -U /tmp/socket\n", .expectOut = "crashyhost sh -c "
"'if nc -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
"ARG=-q0;"
"else "
"ARG=;"
"fi;"
"nc $ARG -U /tmp/socket'\n",
.dieEarly = true, .dieEarly = true,
}; };
if (virtTestRun("SSH test 5", 1, testSocketSSH, &sshData5) < 0) if (virtTestRun("SSH test 5", 1, testSocketSSH, &sshData5) < 0)
...@@ -549,7 +572,13 @@ mymain(void) ...@@ -549,7 +572,13 @@ mymain(void)
.path = "/tmp/socket", .path = "/tmp/socket",
.keyfile = "/root/.ssh/example_key", .keyfile = "/root/.ssh/example_key",
.noVerify = true, .noVerify = true,
.expectOut = "-i /root/.ssh/example_key -o StrictHostKeyChecking=no example.com nc -U /tmp/socket\n", .expectOut = "-i /root/.ssh/example_key -o StrictHostKeyChecking=no example.com sh -c '"
"if nc -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
"ARG=-q0;"
"else "
"ARG=;"
"fi;"
"nc $ARG -U /tmp/socket'\n",
}; };
if (virtTestRun("SSH test 6", 1, testSocketSSH, &sshData6) < 0) if (virtTestRun("SSH test 6", 1, testSocketSSH, &sshData6) < 0)
ret = -1; ret = -1;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册