简体   繁体   中英

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.

The line below lineList = lineList[6:-3] is my concern.

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:

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?

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. I eliminated stdin.write commands. 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:

cd /mnt/PCAPS/GroupSetup
ls -1 --color=never -d */
exit

which were the commands I was previously writing with sshProcess.stdin.write()

This in turn gives me a clean return of

(1) A
(2) B
(3) C
(4) D
(5) E
(6) F

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM