[英]Python: How to get rid of stdin lines in stdout
I have the following code where I want to put all of the folders in a specific directory into a list for a user to choose from. 我有以下代码,我要将特定目录中的所有文件夹放到列表中,供用户选择。 Below I have a working solution, but my concern is that it may be possible for the stdout information to change, and I don't want to hardcode my list to ignore stdin information.
下面我有一个可行的解决方案,但我担心的是stdout信息可能会更改,并且我不想对列表进行硬编码以忽略stdin信息。
The line below lineList = lineList[6:-3]
is my concern. 我关注的是
lineList = lineList[6:-3]
下面的行。
sshCommand = "plink root@255.255.255.255-pw PASSWORD"
lsCommand = "ls -1 --color=never -d */\nexit\n"
sshProcess = Popen(sshCommand,shell=False,stdin=PIPE,stdout=PIPE)
sshProcess.stdin.write("cd /mnt/PCAPS/GroupSetup\n")
sshProcess.stdin.write(lsCommand)
sshProcess.stdin.flush()
lineList = []
for line in sshProcess.stdout.readlines():
line = line.replace("/","")
line = line.strip('\n')
line = line.strip('\r')
lineList.append(str(line))
lineList = lineList[6:-3]
while(True):
x = 0
for line in lineList:
x+=1
print "(" + str(x) + ") " + line
network_name = raw_input("Please select which network to use: ")
try:
network_name = lineList[int(network_name)-1]
networkCorrect = raw_input('You have selected %s. Is this correct? (Y/N): ' %(network_name))
if inputYNChecker(networkCorrect):
return network_name
else:
print "\nPlease select a number listed above.\n"
except:
print "\nPlease select a number listed above.\n"
Which gives me the following output, which is what I want! 这给了我以下输出,这就是我想要的!
Using username "root". (1) A (2) B (3) C (4) D (5) E (6) F
However, when I do not have the lineList = lineList[6:-3]
in my code, I get: 但是,当我的代码中没有
lineList = lineList[6:-3]
时,我得到:
Using username "root". (1) Last login: Thu Dec 8 13:59:51 2016 from 255.255.255.255 (2) cd mntPCAPSGroupSetup (3) ls -1 --color=never -d * (4) exit (5) ←]0;root@Test_Control:~[root@Test_Control ~]# cd mntPCAPSGroupSetup (6) ←]0;root@Test_Control:mntPCAPSGroupSetup[root@Test_Control GroupSetup]# ls -1 --color=never -d * (7) A (8) B (9) C (10) D (11) E (12) F (13) ←]0;root@Test_Control:mntPCAPSGroupSetup[root@Test_Control GroupSetup]# exit (14) logout (15) ←[H←[2J
Is there a better way to get rid of the "garbage" return of stdout besides ignoring the specific lines? 除了忽略特定的行以外,还有没有更好的方法摆脱stdout的“垃圾”返回?
To eliminate the stdout info I did not want, I actually decided to use plink's -m feature which reads in a file as a bash. 为了消除我不想要的标准输出信息,我实际上决定使用plink的-m功能,该功能以bash形式读取文件。 I eliminated
stdin.write
commands. 我消除了
stdin.write
命令。 I now only have the following: 我现在只有以下几个:
sshCommand = "plink root@XXX.XXX.XXX.XXX -pw PASSWORD -m getNetworkName.txt "
sshProcess = Popen(sshCommand,shell=False,stdin=PIPE,stdout=PIPE)
Where getNetworkName.txt contains: 其中getNetworkName.txt包含:
cd /mnt/PCAPS/GroupSetup
ls -1 --color=never -d */
exit
which were the commands I was previously writing with sshProcess.stdin.write()
这是我以前使用
sshProcess.stdin.write()
编写的命令
This in turn gives me a clean return of 反过来,这给了我干净的回报
(1) A
(2) B
(3) C
(4) D
(5) E
(6) F
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.