Ci-dessous un petit script qui permet de lancer les commandes de sauvegarde TFTP via pexpect sur un composant Cisco:
import pexpect
IPa = raw_input('Entrer l adresse IP du composant:')
IP = 'ssh admin@'+IPa
child = pexpect.spawn (IP)
child.expect ('Password: ')
pwd = raw_input('Entrer le password du composant:')
print pwd
child.sendline (pwd)
child.expect ('#')
child.sendline ('copy running-config tftp:')
child.expect ('\? ')
child.sendline ('10.10.10.20')
child.expect ('\? ')
host = IPa+'.cfg'
child.sendline (host)
child.expect ('#')
child.sendline ('exit')
print child.before
Idem pour un Altéon:
import pexpect
child = pexpect.spawn ('ssh admin@10.10.10.10')
child.expect ('password: ')
child.sendline ('admin')
child.expect (['Main#','[y]'])
i = child.expect (['Main#','[y]'])
if i == 0:
print 'Connexion Ok'
elif i == 1:
child.sendline ('y')
else:
p.expect(pexpect.EOF)
print 'Timeout connexion'
child.expect ('Main#')
child.sendline ('/cfg/ptcfg')
child.expect ('server: ')
child.sendline ('10.10.10.20')
child.expect ('server: ')
child.sendline ('hostname.cfg')
child.expect ('server: ')
child.sendline ('')
print child.before
Idem pour un ISG1000:
import pexpect
child = pexpect.spawn ('ssh admin@10.10.10.10')
child.expect ('password: ')
child.sendline ('admin')
child.expect ('>')
child.sendline ('exec save config to tftp 10.10.10.20 hostname.cfg')
child.expect ('>')
print child.before
N'étant pas familié du scripting, je suis preneur de toute amélioration à apporter.