问题
I have got csv a file conatainning ips aranged in columns like :
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
and so on
for each ip i need to login to the cisco switch and execute some commands for ip 10.38.227.233(VLAN-A,23|VLAN-XYZ,27|VLAN-XZ,27)
all the ips have the same password so i am trying to get the password prompt only once and loop over the ips from the file.
I am a novice at this and have started like this but does not seems to work. right now i started some testing by executing simple ls on a remote *nix machines but it not working
#!/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)
this is what i am getting :
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
Please help
moreover how do i loop over the ips in the file and execute some set of commands in ssh scope.
NOTE:each ip will have multiple VLAN but each VLAN will have only one id (like 11,12)
EDIT: i need to run some commands in ssh scope
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...............
"
回答1:
First thing you need to move the getpass line to before you start the loop that way it will only ask for the password once.
The error you are getting is a result of a timeout, basically ssh is taking too long to respond.
Since what you want to do is pretty simple. Checkout spur (https://pypi.python.org/pypi/spur). It has a really clean interface for sending commands.
回答2:
ssh -o NumberOfPasswordPrompts=1 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -l user [hostname or ip] -p 22
No need for sending a yes
来源:https://stackoverflow.com/questions/15026536/looping-over-a-some-ips-from-a-file-in-python