paramiko,无法从远程文件循环配置ip列表

时间:2017-10-23 18:50:10

标签: python paramiko

我只是编写一个python脚本来自动化思科防火墙固件升级。

我可以为单个IP运行脚本。

但是,每当我想从文件中读取IP时,我都无法完成任务。

我明白我错过了循环。但我无法完成。

需要宝贵的时间并帮助完成。

提前致谢。

    from __future__ import unicode_literals
import sys
import os
import traceback
import paramiko
import socket
import random
import subprocess
import ipaddress
import time
import string



hostip = ['firewallIP.txt']
username = "cisco"
password = "password"
port = 22

array =[ ]
for line in hostip:
hostip = string.split(line.rstrip('\n'),)

def copyImage(hostip):
try:
client_pre = paramiko.SSHClient()
client_pre.load_system_host_keys()
client_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client_pre.connect(hostip, port, username, password, look_for_keys=False, allow_agent=False)
client = client_pre.invoke_shell()
client.send('enable\n')
time.sleep(1)
client.send(password + '\n')
time.sleep(1)
output=client.recv(65535)
if 'Access denied' in output:
print "UserName not working %s" %hostip
return
output = 0
client.send('copy ftp://cisco@1.1.1.1/asa944-10-smp-k8.bin disk0:/asa944-10-smp-k8.bin' + '\n')
time.sleep(1)
output=client.recv(65535)        
client.send('\n')
time.sleep(1)
client.send('\n')
time.sleep(1)
client.send('Password' + '\n')
time.sleep(1)
client.send('\n')
time.sleep(1)
client.send('\n')
output=client.recv(65535)
output=0
time.sleep(1)
print "Copying Image to %s...Please Wait" %hostip
output=client.recv(65535)
print(output)
while 'bytes copied in' not in output:
time.sleep(1)
output=0
output=client.recv(65535)
print(output)
continue
output=0
time.sleep(1)
client.send('dir disk0:' + '\n')
time.sleep(2)
output=client.recv(65535)
if 'asa944-10-smp-k8.bin' and '66834432' in output:
print "Copy Successful. %s" %hostip
return 1
else:
print "Copy Failed. %s" %hostip
eturn 0
except:
print traceback.format_exc()
finally:
client.close ()

def changeBootVariable(hostip):
try:
client_pre = paramiko.SSHClient()
client_pre.load_system_host_keys()
client_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client_pre.connect(hostip, port, username, password, look_for_keys=False, allow_agent=False)
client = client_pre.invoke_shell()
client.send('enable\n')
Time.sleep(1)
client.send(password + '\n')
time.sleep(1)
output=client.recv(65535)
if 'Access denied' in output:
print "UserName not working %s" %hostip
return
output = 0
client.send('conf terminal' + '\n')
time.sleep(1)
client.send('boot system disk0:/asa944-10-smp-k8.bin' + '\n')
time.sleep(1)
client.send('write memory' + '\n')
output=client.recv(65535)
output=0
client.send('show run | i boot' + '\n')
time.sleep(1)
output=client.recv(65535)
if 'boot system disk0:/asa944-10-smp-k8.bin' in output:
print "Boot Variable Set %s" %hostip
else:
print "Boot Variable Failed %s" %hostip                        
except:
print traceback.format_exc()

def pingTest(hostip):
#Configure Subprocess to hide the console window
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE
output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(hostip)],
stdout=subprocess.PIPE, startupinfo=info).communicate()[0]

if 'Destination host unreachable' in output.decode('utf-8'):
print(hostip, 'is Offline')
elif 'Request timed out' in output.decode('utf-8'):
print(hostip, 'is Offline')
else:
print(hostip, 'is Online')

if __name__ == '__main__':
for i, elem in enumerate(hostip):
r = copyImage(elem)
if r == 1:
changeBootVariable(elem)
pingTest(elem)

0 个答案:

没有答案