我正试图从横幅中获取序列号。我已经成功地将横幅内容存储在文件中,但现在我想尝试不将其存储在文件中。以下是代码片段:
import argparse
import logging
import paramiko
def grab_banner(ip_address, port):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(ip_address, port=port, username='username', password='bad-password-on-purpose')
except:
return client._transport.get_banner()
def GetSerialNo(ip_address,Banner):
fp1=open("Baner","w")
fp1.write(Banner)
fp1.close()
fp2=open("Baner","r")
for line in fp2:
if re.search("System S/N", line):
Serial = line.split()
return Serial[2]
fp2.close()
if __name__ == '__main__':
logger = logging.getLogger(__name__)
parser = argparse.ArgumentParser(description='This is a demo script')
parser.add_argument('-s','--ipsetups', help='IP Address')
args = parser.parse_args()
Setup_File=args.ipsetups
fp=open(Setup_File,"r")
for line in fp.readlines():
IP = line.strip()
logger.info("================================ WORKING on %s ===================================",IP)
Banner = grab_banner(IP, 22)
serial = GetSerialNo(IP, Banner)
logger.info("Serial Number is -> %s",serial)
fp.close()
上面的代码工作正常,但现在我试图通过将其存储在某个变量中然后获取序列号来实现。但我无法这样做。以下是我想要做的事情:
def get_ip(Setup_File):
IPS = []
with open(Setup_File, 'r') as f:
for line in f:
IPS = line.split()
return IPS
def grab_banner(ip_address, port):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(ip_address, port=port, username='username', password='bad-password-on-purpose')
except:
return client._transport.get_banner()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='This is a demo script by Mangesh Pardhi.')
parser.add_argument('-s','--ipsetups', help='PD-Setup IP Address')
args = parser.parse_args()
Setup_File=args.ipsetups
print Setup_File
IPS = get_ip(Setup_File)
for IP in IPS:
logger.info("================================ WORKING on %s ===================================",IP)
Banner = grab_banner(IP, 22)
if "System S/N" in Banner:
XXXXXXXXXXHow To procees XXXXXXXXXXx
serial = Serial[2]
logger.info("Serial Number is -> %s",serial)
答案 0 :(得分:0)
您可以简化原始代码中的support_case.instance_variables.each { |ivar| puts "#{ivar}: #{support_case.instance_variable_get(ivar)}" }
。
GetSerialNo
(当然你已经知道你不需要参数 def GetSerialNo(ip_address, Banner):
for line in Banner.split('\n'):
if re.search("System S/N", line):
Serial = line.split()
return Serial[2]
。)