2015-02-24 29 views
0
from itertools import permutations 
from itertools import combinations 
import itertools 
import random 
import os 
def split(arr, size): 
    arrs = [] 
    while len(arr) > size: 
     pice = arr[:size] 
     arrs.append(pice) 
     arr = arr[size:] 
    arrs.append(arr) 
    return arrs 
print("Enter the GCAP utility command:") 
N=raw_input() 
A= [N] 
A1=[] 
S7=[] 
S6=[] 
X=[] 
X1=[] 
m1=[] 
G=0 
A1=0 
G1=[] 
g1=[] 
u=[] 

def HELP(): 
    print('SYNTAX : ABC') 

def comp(): 
    for x11, y11 in zip(Y, m1[G-1]): 
     if x11 == y11: 
      X1 = sorted(m1[G-1]) 
      A1= X1 
    return A1 

def my_containsAll(str1, Y): 
    for c in Y: 
     if c not in str1: return 0; 
    return 1; 

def mysplit(s): 
    head = s.rstrip('') 
    tail = s[len(head):] 
    return head, tail 

def imcon(u,U3): 
    U1= ['imconfig','-i'] 
    U2=U1+u+U3 
    return U2 


if N == 'GCOA.01':  ## for first GCOA.01 command 
    print("Enter the command in this format ") 
    S=raw_input() 
    print ("Enter the user interface for example : ") 
    U= raw_input() 
    for s in ['GCOA.01']: 
       g = mysplit(s) 
    g1.append('-c') 
    g1.append(g[1]) 
    u.append(U) 
    #print(g1) 
    if S == "GCOA.01": ## if no other parameter typed 
     U4= imcon(u,g1) 
     ss= ' '.join(U4) 
     #print(ss)  
    else: 
     S=S.split() 
     S1 =[i.split('\t', 1)[0] for i in S] ## spliting the command into list 
     #print(S1) 
     G1= S1[0] 
     #print(G1) 
     for s in ['GCOA.01']: 
       g = mysplit(s) 
     #print(g[1]) 
     S11=S1[0] 
     S5=[S11] 
     del S1[0] 
     for x in S1: ## separate the parameters from the whole command 
      print(x) 
      X.append(x[:2]) 
     Y=list(X) 
     S2=['-c','-a','-h'] 
     S12 =sorted(S2) 
     str1 = ''.join(S12) 
     #print(str1) 
     A2= my_containsAll(str1, Y) 
     if A2 == 1: 
      #print(S2) 
      S3=[] 
      for i in range(1, len(S2)+1): ## creating the permutation and combinations of the parameters 
        S3.extend(itertools.combinations(S2, i)) 
        if i >= 1: 
         for k in S3: 
          for c in permutations(k): 
           S6.append(c) 
          for l in S6: 
           if l not in S7: 
             S7.append(l) 
      # print(S7) 
      for m in S7: ## creating the list of the different possible combination of P&C 
        m=list(m) 
        m1.append(m) 
      #print (m1) 
      for i1 in m1: ## for finding the index of the parameters given by the user from the P&C list of parameters 
        G=G+1 
        if Y == i1: 
          #print (G) 
          break; 
      g1=g1+S1 
      #print(g1)   
      if Y == m1[0]: 
        R=imcon(u,g1) 
        ss=' '.join(R) 
        print(ss) ## this print are basically command which is to direct on terminal window 
      elif Y == m1[1]: 
        R=imcon(u,g1) 
        ss=' '.join(R) 
        print(ss) 

     else: 
      HELP() 

輸出: imconfig -i TH0 -c 01 -h -a如何Python腳本的印刷值重定向到Linux終端作爲命令

例如:(在命令提示)

c:/python34> python ssm9.py 
imconfig -i th0 -c 01 - h -a 

c:/python34> imconfig -i th0 -c 01 -h -a 
/// i want something like that means the above printed value redirect here. but in linux terminal window 

回答

0
`python ssm9.py` 

$(python ssm9.py) 

eval `python ssm9.py` 

eval $(python ssm9.py) 

應該共同努力。

(你可以嘗試像這樣:)

> $(echo "echo test") 
test 
+0

S0III0s ---我再次獲得相同的字符串..我希望行'imconfig -i th0 -c 01 -h -a'進入命令行並從此imconfig文件返回值。就好像我們正在命令行上輸入直接命令一樣。現在,eval函數不會返回任何結果... – SDSM 2015-02-24 07:01:55

+0

但是... eval執行您傳遞給它的命令作爲參數,就像我的示例中那樣。你在用什麼外殼? – sol 2015-02-24 14:19:35

0

我寫了一個示例代碼這樣

[email protected]:~$ cat sample.py 
#! /usr/bin/python 
import socket 
hostname = socket.gethostname() 

def getPingCommand(host): 
    return "ping %s" %host 

print getPingCommand(hostname) 

然後像這樣執行:

[email protected]:~$ `python sample.py` 
PING vivekUbuntu (127.0.1.1) 56(84) bytes of data. 
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=1 ttl=64 time=0.020 ms 
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=2 ttl=64 time=0.027 ms 
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=3 ttl=64 time=0.050 ms 
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=4 ttl=64 time=0.050 ms 
64 bytes from vivekUbuntu (127.0.1.1): icmp_seq=5 ttl=64 time=0.049 ms 
^C 
--- vivekUbuntu ping statistics --- 
5 packets transmitted, 5 received, 0% packet loss, time 4007ms 
rtt min/avg/max/mdev = 0.020/0.039/0.050/0.013 ms 
[email protected]:~$ 

即使是選項由S0III0提到的也是正確的。這個對我有用。

+0

'imconfig'是一個可執行文件,在代碼中它是簡單的字符串。所以有可能以某種方式'imconfig -i eth0 ...'這是一個字符串,將調用該可執行文件imconfig文件。我應該如何讓我的方法??????? – SDSM 2015-02-25 06:48:05

+0

@SDSM:確保您的python代碼打印所需的imconfig命令。那麼你的代碼將工作。 – kvivek 2015-02-25 06:59:02

+0

我的代碼打印所需的命令。但機器如何知道它的可執行文件?因爲根據代碼它是一個簡單的字符串。 – SDSM 2015-02-25 11:23:28