2013-02-22 59 views
1

我有CSV文件中的文件conatainning列aranged像IPS:從Python中的文件循環播放一些ips?

10.38.227.233,VLAN-A,23 
10.38.227.233,VLAN-XYZ,27 
10.38.227.233,VLAN-XZ,27 
10,38.169.103,VLAN-ABCD,11 
10,38.169.103,VLAN-ABCD,16 
10,38.169.103,VLAN-ABD,17 

爲每個IP我需要登錄到Cisco交換機和IP 10.38.227.233執行某些命令( VLAN-A,23 | VLAN-XYZ,27 | VLAN-XZ,27)

所有ips都有相同的密碼,所以我試圖只從密碼提示一次,並從文件中循環播放ips。

我是這方面的新手,已經開始這樣,但似乎沒有工作。 現在,我通過在遠程的* nix機器上執行簡單LS開始一些測試,但它不工作

#!/usr/bin/python 
import pexpect 
import getpass 
import time 

iplist = ['10.39.5.41', '10.38.164.103', '10.38.227.229'] 

for ip in iplist: 
       sshCmd = "ssh " + "auto21" + "@" + ip 
       #auto21 is a username 
       print "Command: " + sshCmd + "\n" 

       answer = 'yes/no' 
       prompt = 'password:' 
       password = getpass.getpass('password:') 

       #Sends answer based on target server response/known host 
       p = pexpect.spawn(sshCmd) 
       i = p.expect([answer, prompt]) 
       print i 
       if i==0: 
        print 'Sending yes...' 
        p.sendline('yes') 
        p.expect(prompt) 
        print 'Sending password...' 
        p.sendline(password) 
        p.sendline('ls\r') 
        p.expect(pexpect.EOF,timeout=20) 
        print p.before,p.after 
       if i==1: 
        print 'Sending password...' 
        p.sendline(password) 
        p.sendline(password) 
        p.sendline('ls\r') 
        p.expect(pexpect.EOF) 


       try: 
        p.interact() 
        sys.exit(0) 
       except: 
        sys.exit(1) 

這就是我得到:

bash-3.00# python ip.py 
Command: ssh [email protected] 

password: 
1 
Sending password... 
Traceback (most recent call last): 
    File "ip.py", line 35, in <module> 
    p.expect(pexpect.EOF,timeout=20) 
    File "/G4_Automation/user_mgmt/test/pexpect.py", line 1311, in expect 
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize) 
    File "/G4_Automation/user_mgmt/test/pexpect.py", line 1325, in expect_list 
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize) 
    File "/G4_Automation/user_mgmt/test/pexpect.py", line 1409, in expect_loop 
    raise TIMEOUT (str(e) + '\n' + str(self)) 
pexpect.TIMEOUT: Timeout exceeded in read_nonblocking(). 
<pexpect.spawn object at 0x15aef0> 
version: 2.3 ($Revision: 399 $) 
command: /usr/bin/ssh 
args: ['/usr/bin/ssh', '[email protected]'] 
searcher: searcher_re: 
    0: EOF 
buffer (last 100 chars): st login: Fri Feb 22 08:55:05 2013 from p13adv 
Testgfs2 
-sh-3.2$ ls 
-sh-3.2$ 
-sh-3.2$ 
before (last 100 chars): st login: Fri Feb 22 08:55:05 2013 from p13adv 
Testgfs2 
-sh-3.2$ ls 
-sh-3.2$ 
-sh-3.2$ 
after: <class 'pexpect.TIMEOUT'> 
match: None 
match_index: None 
exitstatus: None 
flag_eof: False 
pid: 29911 
child_fd: 3 
closed: False 
timeout: 30 
delimiter: <class 'pexpect.EOF'> 
logfile: None 
logfile_read: None 
logfile_send: None 
maxread: 2000 
ignorecase: False 
searchwindowsize: None 
delaybeforesend: 0.05 
delayafterclose: 0.1 
delayafterterminate: 0.1 

請幫

此外,我如何循環訪問文件中的ips並在ssh作用域中執行一些命令。

注意:每個IP將有多個VLAN,但每個VLAN將只有一個ID(如11,12)

編輯:我需要在SSH範圍內運行一些命令

ssh [email protected] "config t 
         int VLAN-A 
         switchport access VLAN-A 23 
         wr 
        #then for next VLAN for the current ip 
         config t 
         int VLAN-A 
         switchport access VLAN-A 23 
         wr 
        #and so on...............       
         " 
+0

請打印輸出您期望的結果以及您所看到的錯誤消息。 – jknupp 2013-02-22 14:38:10

回答

0

首先,您需要將getpass行移至開始循環之前,它將只會要求密碼一次。

你得到的錯誤是超時的結果,基本上ssh的響應時間太長。

因爲你想要做的事很簡單。結賬支票(https://pypi.python.org/pypi/spur)。它有一個非常乾淨的接口來發送命令。

+0

我不知道我是否可以使用'spur'這種方式來使用pexpect,因爲我沒有安裝pexpect的權限,我只是將'pexpect.py'模塊複製到了我正在運行該腳本的目錄中,並且它工作正常。 – munish 2013-02-22 15:20:31

+0

是否安裝了virtualenv?如果是這樣,你應該可以使用它來創建本地軟件包回購,然後安裝spur。或者讓你的服務器管理員安裝它。 – 2013-02-22 16:07:11

0

SSH -o NumberOfPasswordPrompts = 1 -o StrictHostKeyChecking =無-o UserKnownHostsFile =的/ dev/null的-l用戶[主機名或IP] -p 22

無需發送是