如何解决如何执行命令并对其STDIN / TTY进行读/写一起?
我已经看到了有关如何单独执行这些操作的示例和问题。但是在这个问题上,我试图将它们全部一起做。
基本上我的情况是我有一个命令,需要我写它的STDIN,从它的STDOUT读取,并回答它的TTY提示。只需执行一次命令即可完成所有操作。没关系,但是如果您很好奇,命令为 **<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-sftp</artifactId>
</dependency>**
。
限制:必须是纯Python。
问题:怎么做?
我尝试了这些:
03 Sep 2020 01:47:56.688 ERROR o.s.i.handler.LoggingHandler - org.springframework.messaging.MessagingException: Problem occurred while synchronizing remote to local directory; nested exception is org.springframework.messaging.MessagingException: Failed to obtain pooled item; nested exception is java.lang.IllegalStateException: failed to create SFTP Session
at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:303)
at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource.doReceive(AbstractInboundFileSynchronizingMessageSource.java:200)
at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource.doReceive(AbstractInboundFileSynchronizingMessageSource.java:62)
at org.springframework.integration.endpoint.AbstractMessageSource.receive(AbstractMessageSource.java:134)
at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:224)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:245)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.access$000(AbstractPollingEndpoint.java:58)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:190)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:186)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:353)
at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:55)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:51)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:344)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.messaging.MessagingException: Failed to obtain pooled item; nested exception is java.lang.IllegalStateException: failed to create SFTP Session
at org.springframework.integration.util.SimplePool.getItem(SimplePool.java:178)
at org.springframework.integration.file.remote.session.CachingSessionFactory.getSession(CachingSessionFactory.java:123)
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:441)
at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:264)
... 22 more
Caused by: java.lang.IllegalStateException: failed to create SFTP Session
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:393)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:57)
at org.springframework.integration.file.remote.session.CachingSessionFactory$1.createForPool(CachingSessionFactory.java:81)
at org.springframework.integration.file.remote.session.CachingSessionFactory$1.createForPool(CachingSessionFactory.java:78)
at org.springframework.integration.util.SimplePool.doGetItem(SimplePool.java:188)
at org.springframework.integration.util.SimplePool.getItem(SimplePool.java:169)
... 25 more
Caused by: java.lang.IllegalStateException: failed to connect
at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:273)
at org.springframework.integration.sftp.session.DefaultSftpSessionFactory.getSession(DefaultSftpSessionFactory.java:388)
... 30 more
Caused by: com.jcraft.jsch.JSchException: java.io.IOException: Pipe closed
at com.jcraft.jsch.ChannelSftp.start(ChannelSftp.java:315)
at com.jcraft.jsch.Channel.connect(Channel.java:152)
at com.jcraft.jsch.Channel.connect(Channel.java:145)
at org.springframework.integration.sftp.session.SftpSession.connect(SftpSession.java:268)
... 31 more
Caused by: java.io.IOException: Pipe closed
at java.io.PipedInputStream.read(PipedInputStream.java:307)
at java.io.PipedInputStream.read(PipedInputStream.java:377)
at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2909)
at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2935)
at com.jcraft.jsch.ChannelSftp.start(ChannelSftp.java:262)
... 34 more
scrypt enc - out.enc
目标是完全包装TTY提示,使用户看不到它们,同时将一些密码输入进程TTY输入中,以使import pty
import os
import subprocess
master,slave = pty.openpty()
p = subprocess.Popen(['sudo','ls','-lh'],stdin=slave,stdout=master)
x= os.read(master)
print(x)
stdout,stderr = p.communicate(b'lol\r\n')
满意。
基于该目标,由于各种原因,这些尝试均无效。
但是更糟糕的是:假设,它们可以工作,我如何向该进程的STDIN及其TTY输入输入一些信息?令我感到困惑的是,import pty
import os
import sys
import subprocess
def read(fd):
data = os.read(fd,1024)
data_str = data.decode()
if data_str.find('[sudo] password for') == 0:
data_str = 'password plz: '
sys.stdout.write(data_str)
sys.stdout.flush()
def write(fd):
x = 'lol\r\n'
for b in x.encode():
os.write(fd,b)
pty.spawn(['sudo',read,write)
示例从字面上指出sudo
被映射到TTY(pty),那么它怎么知道是哪一个呢?如何知道某些输入是用于STDIN而不是TTY-in?
解决方法
免责声明:
详细讨论此主题将需要大量文本,因此我将尝试简化内容以使其简短。我将尝试包括尽可能多的“供进一步阅读”链接。
为了简短起见,只有一个输入流,即STDIN。在普通终端中,STDIN连接到TTY。因此,您在TTY上键入的内容将被外壳读取。然后,外壳程序决定如何处理它。如果有一个程序在运行,它将发送到该程序的STDIN。
如果您在python中使用Popen
运行某些程序,则不会有tty。您可以通过以下操作轻松检查:
from subprocess import Popen,PIPE
p = Popen("tty",stdin=PIPE,stdout=PIPE,stderr=PIPE)
o,e = p.communicate()
print(o)
它将产生以下输出:b'not a tty\n'
但是scrypt然后如何尝试使用TTY?因为那是它的作用。
您必须查看手册和代码,才能找到答案。
如果未指定-P,则scrypt会从其控制终端读取密码,否则会从stdin读取密码。
实际上,它只是打开/dev/tty
(请看the code)。即使该过程没有TTY,也存在。因此它可以打开它,并尝试从中读取密码。
现在如何解决您的问题?
好吧,在这种情况下这很容易。检查手册页中的-P
参数。
这是一个工作示例:
from subprocess import Popen,PIPE
p = Popen("scrypt enc -P - out.enc",stderr=PIPE,universal_newlines=True,shell=True)
p.communicate("pwd\nteststring")
这将使用密码“ pwd”对字符串“ teststring”进行加密。
ttys等周围有很多“骇客”,但是您应避免使用它们,因为它们可能会产生意想不到的结果。例如,启动一个shell并运行tty
,然后运行第二个shell并运行cat
,并带有tty
命令的输出(例如cat /dev/pts/7
)。然后在第一个外壳中输入内容,然后观察会发生什么情况。
如果您不想尝试,则某些字符将出现在第一个外壳中,而某些字符将出现在第二个外壳中。
检查this post和this article关于TTY是什么以及它来自哪里。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。