2014-01-08 33 views
0

我對編程有點新,所以我需要幫助。期望:如何抓住輸出的一部分

這裏有一個樣本會話:

CMD> disable inactive 
Accounts to be disabled are: 
    albert_a - UUID abcd-11-2222 
    brian_b - UUID bcde-22-3333 
    charley_c - UUID cdef-33-4444 
Starting processing 
    ...hundreds of lines of processing... 
CMD> quit 
Done. 

我需要獲取用戶名和UUID的存在(的UUID是無法可以通過其他方式),然後要麼將它們保存到一個文件中。我該如何做到這一點?

編輯:在- UUID(空格破折號空間「UUID」)列表的一部分是靜態的,而不是在「上百處理的行」隨處可見,所以我認爲我可以匹配對抗模式...但是如何?

+0

而我猜你可以有任何數量的帳戶/ UUID行在輸出,正確的? – James

+0

@James對不起,我剛剛從一個出城旅行回來......沒有時間檢查SO.com – pepoluan

回答

1

假設評論中我的問題的答案是'是',這是我的建議。

首先,你需要產生任何程序將你連接到服務器(SSH,TELNET或其他),並登錄(期待用戶提示,發送密碼,期望提示)。你會發現很多樣本,所以我會跳過那部分。

一旦你做到這一點,並有一個命令提示符下,這裏的我會怎樣發送命令,並期望&匹配輸出:

set file [open /tmp/inactive-users w]  ;# open for writing and set file identifier 

send "disable inactive\r" 

expect { 
     -re "(\[a-z0-9_\]+) - UUID" {  ;# match username followed by " - UUID" 
      puts $file "$expect_out(1,string)"  ;# write username matched within parenthesis to file identifier 
      exp_continue  ;# stay in the same expect loop in case another UUID line comes 
     } 
     -re "CMD>" {  ;# if we hit the prompt, command execution has finished -> close file 
      close $file 
     } 
     timeout {  ;# reasonably elegant exit in case something goes bad 
      puts $file "Error: expect block timed out" 
      close $file 
     } 
} 

請注意,正則表達式我假設用戶名可以僅由小寫字母,數字和下劃線組成。

如果您需要登錄幫助,請告訴我,但您應該確定。那裏有很多樣品。

希望有幫助!

+0

它有效奇蹟!謝謝! – pepoluan

+1

很高興聽到! – James