1

我有一个包含 ips 的 csv 文件,列在如下列中:

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 auto21@10.39.5.41

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', 'auto21@10.39.5.41']
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 username@hosname "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...............                        
                      "
4

2 回答 2

0

在开始循环之前,首先需要将 getpass 行移到,这样它只会要求输入一次密码。

您得到的错误是超时的结果,基本上 ssh 响应时间太长。

因为你想做的很简单。结帐支线(https://pypi.python.org/pypi/spur)。它有一个非常干净的界面来发送命令。

于 2013-02-22T15:13:43.563 回答
0

ssh -o NumberOfPasswordPrompts=1 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -l user [hostname or ip] -p 22

无需发送是

于 2017-04-04T01:55:02.433 回答