2015-02-24 62 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