(与这个问题有关 )
在Windows上使用sshd服务器( Win32-OpenSSH )(使用基于密钥的auth进行自动化)时,如果没有STDIN(或PTY),它将不起作用。 例如,运行这个(从Debian Jessie openssh客户端)工作正常(返回Windows用户名):
ssh -4 -T -o Batchmode=yes winserver whoami
但是,这两个不(他们只是终止而不执行命令,没有返回任何输出):
ssh -4 -T -o Batchmode=yes winserver whoami < /dev/null ssh -4 -n -T -o Batchmode=yes winserver whoami
这提出了问题,因为不可能从没有打开STDIN的程序(如cron(8)或atd(8) )运行非交互式ssh命令。
当使用Debian openssh服务器时,它当然没有任何问题。 问题只发生在Windows的SSH服务器( Win32-OpenSSH和FreeSSHD失败。Bitvise SSHD似乎工作正常,但我们正在寻找免费的解决scheme的Windows SSH服务器;开源,简单和维护奖金)
这已经被报道 ,但是有没有人同时有解决方法或解决方法?
最后,我已经写了这个ssh wrapper来解决bug的Windows OpenSSH服务器,它需要STDIN,甚至对于不使用它的非交互式命令:
#!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; my $HOSTNAME = shift; my $ssh = Net::OpenSSH->new($HOSTNAME); my ($in_pipe, undef, undef, $pid) = $ssh->open_ex( { stdin_pipe => 1 }, @ARGV) or die "open_ex failed: " . $ssh->error; waitpid($pid, 0);
它只使用主机名和带参数的命令来运行,没有其他参数被允许(我把它们设置在~/.ssh/config ),所以你运行脚本(例如) myssh winserver whoami < /dev/null 。 它只是一个包装ssh客户端,它提供了伪STDIN,因此允许非交互ssh会话从cron(8)和atd(8)运行,
感谢Matija-Nalis提出的问题和解决scheme。 对于其他的收益,这里是一个粗糙的python实现。
ssh.py
import sys import paramiko client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(sys.argv[2], username=sys.argv[1]) stdin, stdout, stderr = client.exec_command(sys.argv[3]) errlines = stderr.readlines() outlines = stdout.readlines() for l in outlines: print l.rstrip() if len(errlines): print "WARNING: stderr generated:" for l in errlines: print l.rstrip() exit(stdout.channel.recv_exit_status())
python ssh.py <user> <hostname> <command>
我的用例是相似的:Jenkins作业未能执行远程命令。