2016-03-22 3 views
0

Ich versuche, ein Scrip in Python zu machen, um die Aliase eines Benutzers, die ich ausgewählt habe, wie beim Eingeben von Alias ​​im Terminal zu zeigen. so weit geht der Code wie folgt:Wie Benutzer Aliases in Python zeigen

tt = open("/etc/passwd" , "r") 
with tt as f2: 
    with open("passwd" , "w+") as f1: 
     f1.write(f2.read()) 
     f1.seek(0,0) 
     command = f1.read() 
     print 
     print command 

chose = raw_input("select user's name from this list > ") 
rootlist = "1) Show user groups \n2) Show user id \n3) Show users alias\n4) Add new alias \n5) Change Password \n6) Back" 
print 
print rootlist 
print 
chose2 = int(raw_input("Choose a command > ")) 
if choose == 3: 
    os.system("alias ") 

jedoch os.system („Alias“) nicht funktioniert und ich kann nicht eine richtige Art und Weise t es tun zu finden scheinen.

Antwort

0

Alias ​​ist ein Shell-builtin, die hier zu sehen sind

$ type -a alias 
alias is a shell builtin 

Das ist das Problem, das Sie durch Hinzufügen eines Anrufs zu bash zu Ihrem Shell-Befehl lösen können

import os 
os.system('bash -i -c "alias"') 

oder die bevorzugte Weg mit Subprozessmodul

from subprocess import Popen, PIPE, STDOUT 

cmd = 'bash -i -c "alias"' 
event = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT) 
output = event.communicate()[0] 
print(output)