How can i get password,ip ,enable password and port ssh from ip2.txt for my Cisco Backup script?
Hello to everyone. I want to write a script for Cisco backup. The script will try to connect by ssh, and if it fails by telnet. All data for the connection will be stored in the ip2.txt file. The lines are written as follows:
192.168.1.12:admin:password:enpassword:22
For the test, I use a telnet connection, but my dictionary doesn 't work. Such a mistake flies out -
ip, user, password, enable_password, port, = res[0] IndexError: list index out of range
import time
import os
import cmd
import datetime
import re
import pexpect
now = datetime.datetime.now()
with open('ip2.txt', 'r') as f:
for line in f:
line = line.strip()
res = re.findall('^(.+?)\:(.+)\:(.+)\:(.+)\:(.+)$', line)
ip, user, password, enable_password, port, = res[0]
device_params = {
'device_type': 'cisco_ios',
'ip': ip,
'username': user,
'password': password,
'secret': enable_password,
'port': port
}
child = pexpect.spawn('telnet ' + ip)
child.expect('Username: ')
child.sendline(user)
child.expect('Password: ')
child.sendline(password)
child.expect('*> ')
child.sendline('enable')
child.expect('Password: ')
child.sendline(enable_password)
child.expect('# ')
child.sendline('copy running-config tftp://192.168.1.22')
child.sendline('\n ')
child.sendline('\n ')
child.sendline('\n ')
child.expect('# ')
child.sendline('quit \n ')
child.sendline('\n')
pexpect.spawn.close()