Python Forum

Full Version: convert v2 code to v3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all, please help am new to python 3 and attached is py2 code and getting bellow error. please help to solve.

Error .......
Error:
"a bytes-like object is required, not 'str'"
========================SCRIPT=======================
import os
import paramiko
import telnetlib
import time
import getpass
import Crypto.Cipher.AES
import subprocess
orig_new = Crypto.Cipher.AES.new
##################################################################################
def addressInNetwork(ip, net):
	import socket,struct
	ipaddr = int(''.join([ '%02x' % int(x) for x in ip.split('.') ]), 16)
	netstr, bits = net.split('/')
	netaddr = int(''.join([ '%02x' % int(x) for x in netstr.split('.') ]), 16)
	mask = (0xffffffff << (32 - int(bits))) & 0xffffffff
	return (ipaddr & mask) == (netaddr & mask)
##################################################################################
##################################################################################
def fixed_AES_new(key, *ls):
	if Crypto.Cipher.AES.MODE_CTR == ls[0]:
		ls = list(ls)
		ls[1] = ''
		return orig_new(key, *ls)
def telnetcall(host1,user1,pass1):
        try:
                host = host1
                user = user1
                password = pass1
                telnet = telnetlib.Telnet(host,23,10) 
                telnet.read_until(': ', 3)
                telnet.write(user.encode('ascii') + '\r')
                telnet.read_until('Password: ', 3)
                telnet.write(password.encode('ascii') + '\r')
                telnet.write('terminal length 0' + '\r\n')
                telnet.write('show version' + '\r\n')
                telnet.write('exit' + '\r\n')
                a = telnet.read_until("RELEASE", 30)
                time.sleep(5)
                if "IOS Software" in a and openportcheck(host1) == "OPEN":
                        print ("                                                                                                                      ")
                        print ("                                                 ---------- "+host+" TELNET and unwanted port OPEN-----------")
                        time.sleep(5)
                        host = host1
                        user = user1
                        password = pass1
                        telnet = telnetlib.Telnet(host,23,10) 
                        telnet.read_until(': ', 3)
                        telnet.write(user.encode('ascii') + '\r')
                        telnet.read_until('Password: ', 3)
                        telnet.write(password.encode('ascii') + '\r')
                        telnet.write('terminal length 0' + '\r\n')
                        telnet.write('show running-config | begin line vty 0 4' + '\r\n')
                        telnet.write('exit' + '\r\n')
                        b = telnet.read_all()
                        time.sleep(5)
                        if "access-class 10 in" in b:
                                time.sleep(5)
                                host = host1
                                user = user1
                                password = pass1
                                telnet = telnetlib.Telnet(host,23,10) 
                                telnet.read_until(': ', 3)
                                telnet.write(user.encode('ascii') + '\r')
                                telnet.read_until('Password: ', 3)
                                telnet.write(password.encode('ascii') + '\r')
                                telnet.write('terminal length 0' + '\r\n')
                                telnet.write('show runn | i access-list 10 ' + '\r\n')
                                telnet.write('exit' + '\r\n')
                                c = telnet.read_until("RELEASE", 30)
                                if "access-list 10 permit " in c:
                                        print ("                                                 ---------- "+host+" ACL 10 in place, need to close unwanted ports  ----------")
                                else:
                                        print ("                                                 ---------- "+host+" NO NO NO ACL 10, need to close unwanted ports ----------")
                        elif "access-class 15 in" in b:
                                time.sleep(5)
                                host = host1
                                user = user1
                                password = pass1
                                telnet = telnetlib.Telnet(host,23,10) 
                                telnet.read_until(': ', 3)
                                telnet.write(user.encode('ascii') + '\r')
                                telnet.read_until('Password: ', 3)
                                telnet.write(password.encode('ascii') + '\r')
                                telnet.write('terminal length 0' + '\r\n')
                                telnet.write('show runn | i access-list 2 ' + '\r\n')
                                telnet.write('exit' + '\r\n')
                                c = telnet.read_until("RELEASE", 30)
                                if "access-list 15 permit " in c:
                                        print ("                                                 ---------- "+host+" ACL 15 in place, need to close unwanted ports ----------")
                                else:
                                        print ("                                                 ---------- "+host+" NO NO NO ACL 15, need to close unwanted ports ----------")
                        elif "access-class 2 in" in b:
                                time.sleep(5)
                                host = host1
                                user = user1
                                password = pass1
                                telnet = telnetlib.Telnet(host,23,10) 
                                telnet.read_until(': ', 3)
                                telnet.write(user.encode('ascii') + '\r')
                                telnet.read_until('Password: ', 3)
                                telnet.write(password.encode('ascii') + '\r')
                                telnet.write('terminal length 0' + '\r\n')
                                telnet.write('show runn | i access-list 2 ' + '\r\n')
                                telnet.write('exit' + '\r\n')
                                c = telnet.read_until("RELEASE", 30)
                                if "access-list 2 permit " in c:
                                        print ("                                                 ---------- "+host+" ACL 2 in place, need to close unwanted ports ----------")
                                else:
                                        print ("                                                 ---------- "+host+" NO NO NO ACL 2, need to close unwanted ports ----------")
                        else:
                                print ("                                                 ---------- "+host+" NO NO NO access-class in line vty 0 4 ----------")
                elif "IOS Software," in a and openportcheck(host1) == "CLOSED":
                        print ("                                                                                                                      ")
                        print ("                                                 ---------- "+host+" TELNET and unwanted port CLOSED-----------")
                        time.sleep(5)
                        host = host1
                        user = user1
                        password = pass1
                        telnet = telnetlib.Telnet(host,23,10) 
                        telnet.read_until(': ', 3)
                        telnet.write(user.encode('ascii') + '\r')
                        telnet.read_until('Password: ', 3)
                        telnet.write(password.encode('ascii') + '\r')
                        telnet.write('terminal length 0' + '\r\n')
                        telnet.write('show running-config | begin line vty 0 4' + '\r\n')
                        telnet.write('exit' + '\r\n')
                        b = telnet.read_all()
                        time.sleep(5)
                        if "access-class 10 in" in b:
                                time.sleep(5)
                                host = host1
                                user = user1
                                password = pass1
                                telnet = telnetlib.Telnet(host,23,10) 
                                telnet.read_until(': ', 3)
                                telnet.write(user.encode('ascii') + '\r')
                                telnet.read_until('Password: ', 3)
                                telnet.write(password.encode('ascii') + '\r')
                                telnet.write('terminal length 0' + '\r\n')
                                telnet.write('show runn | i access-list 10 ' + '\r\n')
                                telnet.write('exit' + '\r\n')
                                c = telnet.read_until("RELEASE", 30)
                                if "access-list 10 permit " in c:
                                        print ("                                                 ---------- "+host+" ACL 10 in place, and unwanted port CLOSED ----------")
                                else:
                                        print ("                                                 ---------- "+host+" NO NO NO ACL 10, and unwanted port CLOSED ----------")
                        elif "access-class 15 in" in b:
                                time.sleep(5)
                                host = host1
                                user = user1
                                password = pass1
                                telnet = telnetlib.Telnet(host,23,10) 
                                telnet.read_until(': ', 3)
                                telnet.write(user.encode('ascii') + '\r')
                                telnet.read_until('Password: ', 3)
                                telnet.write(password.encode('ascii') + '\r')
                                telnet.write('terminal length 0' + '\r\n')
                                telnet.write('show runn | i access-list 2 ' + '\r\n')
                                telnet.write('exit' + '\r\n')
                                c = telnet.read_until("RELEASE", 30)
                                if "access-list 15 permit " in c:
                                        print ("                                                 ---------- "+host+" ACL 15 in place, and unwanted port CLOSED ----------")
                                else:
                                        print ("                                                 ---------- "+host+" NO NO NO ACL 15, and unwanted port CLOSED ----------")
                        elif "access-class 2 in" in b:
                                time.sleep(5)
                                host = host1
                                user = user1
                                password = pass1
                                telnet = telnetlib.Telnet(host,23,10) 
                                telnet.read_until(': ', 3)
                                telnet.write(user.encode('ascii') + '\r')
                                telnet.read_until('Password: ', 3)
                                telnet.write(password.encode('ascii') + '\r')
                                telnet.write('terminal length 0' + '\r\n')
                                telnet.write('show runn | i access-list 2 ' + '\r\n')
                                telnet.write('exit' + '\r\n')
                                c = telnet.read_until("RELEASE", 30)
                                if "access-list 2 permit " in c:
                                        print ("                                                 ---------- "+host+" ACL 2 in place, and unwanted port CLOSED ----------")
                                else:
                                        print ("                                                 ---------- "+host+" NO NO NO ACL 2, and unwanted port CLOSED ----------")
                        else:
                                print ("                                                 ---------- "+host+" NO NO NO access-class in line vty 0 4 ----------")
                else:
                        print ("                                                                                                                      ")
                        print ("                                                 ---------- "+host+" not looking like IOS device..!! -----------")
                        print ("                                                 ---------- "+host+" Unwanted Port Status : "+openportcheck(host)+"     -----------")
        except Exception as detail:
                print (detail)
##################################################################################
##################################################################################
def openportcheck(host1):
                        ip_device=host1
                        nmapstr="nmap -PN -p 2002,4002,6002,9002 "+ip_device+" | grep 002"
                        proc = subprocess.Popen(nmapstr,stdout=subprocess.PIPE,shell=True)
                        (out,err) = proc.communicate()
                        outwithoutreturn = out.rstrip
                        if "open" in out:
                                return "OPEN"
                        elif "filtered" in out:
                                return "FILTERED"
                        elif "closed" in out:
                                return "CLOSED"
##################################################################################
##################################################################################
def sshcall(host1,user1,pass1):
        try:
                t14 = paramiko.Transport((host1,22))
                t14.connect(username = user1,password= pass1)
                channel_four = t14.open_session()
                channel_four.exec_command("terminal length 0")
                response4 = channel_four.recv("5000")
                print (response4)
                t14.close()
                t15 = paramiko.Transport((host1,22))
                t15.connect(username = user1,password= pass1)
                channel_four = t15.open_session()
                channel_four.exec_command("show version")
                response5 = channel_four.recv("5000")
                t15.close()
                if "IOS Software," in response5 and openportcheck(host1) == "OPEN":
                        print ("                                                                                                                      ")
                        print ("                                                 ---------- "+host1+" SSH and unwanted port OPEN   -----------")
                        t9 = paramiko.Transport((host1,22))
                        t9.connect(username = user1,password= pass1)
                        channel_four = t9.open_session()
                        channel_four.exec_command("terminal length 0")
                        response4 = channel_four.recv("5000")
                        t9.close()
                        t10 = paramiko.Transport((host1,22))
                        t10.connect(username = user1,password= pass1)
                        channel_zer0 = t10.open_session()
                        channel_zer0.exec_command("show running-config | begin line con")
                        response0 = channel_zer0.recv("5000")
                        t10.close()
                        if "access-class 15 in" in response0:
                                ssh_dev=host1
                                usr = user1
                                pwd = pass1
                                t14 = paramiko.Transport((host1,22))
                                t14.connect(username = user1,password= pass1)
                                channel_four = t14.open_session()
                                channel_four.exec_command("terminal length 0")
                                response4 = channel_four.recv("5000")
                                t14.close()
                                t15 = paramiko.Transport((host1,22))
                                t15.connect(username = user1,password= pass1)
                                channel_four = t15.open_session()
                                channel_four.exec_command("show runn | i access-list 15")
                                response5 = channel_four.recv("5000")
                                if "access-list 15 permit " in response5:
                                        print ("                                                 ---------- "+host1+" ACL 15 is there, access-class is there in vty 0 4 ----------")
                                else:
                                        print (response5) 
                                t15.close()
                                time.sleep(5)
                        elif "access-class 10 in" in response0:
                                ssh_dev=host1
                                usr = user1
                                pwd = pass1
                                t14 = paramiko.Transport((host1,22))
                                t14.connect(username = user1,password= pass1)
                                channel_four = t14.open_session()
                                channel_four.exec_command("terminal length 0")
                                response4 = channel_four.recv("5000")
                                t14.close()
                                t15 = paramiko.Transport((host1,22))
                                t15.connect(username = user1,password= pass1)
                                channel_four = t15.open_session()
                                channel_four.exec_command("show runn | i access-list 10")
                                response5 = channel_four.recv("5000")
                                if "access-list 10 permit " in response5:
                                        print ("                                                 ---------- "+host1+" ACL 10 is there, access-class is there in vty 0 4 ----------")
                                else:
                                        print (response5) 
                                t15.close()
                                time.sleep(5)
                        elif "access-class 2 in" in response0:
                                ssh_dev=host1
                                usr = user1
                                pwd = pass1
                                t14 = paramiko.Transport((host1,22))
                                t14.connect(username = user1,password= pass1)
                                channel_four = t14.open_session()
                                channel_four.exec_command("terminal length 0")
                                response4 = channel_four.recv("5000")
                                t14.close()
                                t15 = paramiko.Transport((host1,22))
                                t15.connect(username = user1,password= pass1)
                                channel_four = t15.open_session()
                                channel_four.exec_command("show runn | i access-list 2")
                                response5 = channel_four.recv("5000")
                                if "access-list 2 permit " in response5:
                                        print ("                                                 ---------- "+host1+" ACL 2 is there, access-class is there in vty 0 4 ----------")
                                else:
                                        print (response5) 
                                t15.close()
                                time.sleep(5)
                        else:
                                print ("                                                 ---------- "+host+" NO NO NO access-class in line vty 0 4 ----------")
                elif "IOS Software," in response5 and openportcheck(host1) == "CLOSED":
                        print ("                                                 ---------- "+host1+" SSH and unwanted port CLOSED   -----------")
                        t9 = paramiko.Transport((host1,22))
                        t9.connect(username = user1,password= pass1)
                        channel_four = t9.open_session()
                        channel_four.exec_command("terminal length 0")
                        response4 = channel_four.recv("5000")
                        t9.close()
                        t10 = paramiko.Transport((host1,22))
                        t10.connect(username = user1,password= pass1)
                        channel_zer0 = t10.open_session()
                        channel_zer0.exec_command("show running-config | begin line con")
                        response0 = channel_zer0.recv("5000")
                        t10.close()
                        if "access-class 15 in" in response0:
                                ssh_dev=host1
                                usr = user1
                                pwd = pass1
                                t14 = paramiko.Transport((host1,22))
                                t14.connect(username = user1,password= pass1)
                                channel_four = t14.open_session()
                                channel_four.exec_command("terminal length 0")
                                response4 = channel_four.recv("5000")
                                t14.close()
                                t15 = paramiko.Transport((host1,22))
                                t15.connect(username = user1,password= pass1)
                                channel_four = t15.open_session()
                                channel_four.exec_command("show runn | i access-list 15")
                                response5 = channel_four.recv("5000")
                                if "access-list 15 permit " in response5:
                                        print ("                                                 ---------- "+host1+" ACL 15 is there, access-class is there in vty 0 4 ----------")
                                else:
                                        print (response5) 
                                t15.close()
                                time.sleep(5)
                        elif "access-class 10 in" in response0:
                                ssh_dev=host1
                                usr = user1
                                pwd = pass1
                                t14 = paramiko.Transport((host1,22))
                                t14.connect(username = user1,password= pass1)
                                channel_four = t14.open_session()
                                channel_four.exec_command("terminal length 0")
                                response4 = channel_four.recv("5000")
                                t14.close()
                                t15 = paramiko.Transport((host1,22))
                                t15.connect(username = user1,password= pass1)
                                channel_four = t15.open_session()
                                channel_four.exec_command("show runn | i access-list 10")
                                response5 = channel_four.recv("5000")
                                if "access-list 10 permit " in response5:
                                        print ("                                                 ---------- "+host1+" ACL 10 is there, access-class is there in vty 0 4 ----------")
                                else:
                                        print (response5) 
                                t15.close()
                                time.sleep(5)
                        elif "access-class 2 in" in response0:
                                ssh_dev=host1
                                usr = user1
                                pwd = pass1
                                t14 = paramiko.Transport((host1,22))
                                t14.connect(username = user1,password= pass1)
                                channel_four = t14.open_session()
                                channel_four.exec_command("terminal length 0")
                                response4 = channel_four.recv("5000")
                                t14.close()
                                t15 = paramiko.Transport((host1,22))
                                t15.connect(username = user1,password= pass1)
                                channel_four = t15.open_session()
                                channel_four.exec_command("show runn | i access-list 2")
                                response5 = channel_four.recv("5000")
                                if "access-list 2 permit " in response5:
                                        print ("                                                 ---------- "+host1+" ACL 2 is there, access-class is there in vty 0 4 ----------")
                                else:
                                        print (response5) 
                                t15.close()
                                time.sleep(5)
                        else:
                                print ("                                                 ---------- "+host+" NO NO NO access-class in line vty 0 4 ----------")
                else:
                        print ("                                                                                                                         ")
                        print ("                                                 ---------- "+host1+" not an IOS device....!! -----------")
                        print ("                                                 ---------- "+host1+" Unwanted Port Status : "+openportcheck(host1)+"     -----------")
        except Exception as detail:
                print (detail)
##################################################################################
##################################################################################
ippermnet01='212.74.0.0/16'
ippermnet02='10.0.0.0/8'
Crypto.Cipher.AES.new = fixed_AES_new
txtopen = open("cpeweekacl.txt")
iplist = txtopen.readlines()
txtopen.close()
j=len(iplist)
username = str("zzzzzzz")
password = str("xxxxxx")
for i in range(0,j):
	ippermlist=iplist[i].strip()
	ip_device=ippermlist
	nmapstr="nmap -PN -p 23,22 "+ip_device
	proc = subprocess.Popen(nmapstr,stdout=subprocess.PIPE,shell=True)
	(out,err) = proc.communicate()
	outwithoutreturn = out.rstrip
	#if (addressInNetwork(ippermlist, ippermnet01) or addressInNetwork(ippermlist, ippermnet02)) == True:
	if (addressInNetwork(ippermlist, ippermnet01)) == True:
		print ("                                                                                                                         ")
		print ("                                                 ---------- "+ippermlist+" Host not in scope infra device     -----------")
	#elif (addressInNetwork(ippermlist, ippermnet01) and addressInNetwork(ippermlist, ippermnet02)) == False:
	elif (addressInNetwork(ippermlist, ippermnet01)) == False:
	#perm=addressInNetwork(ippermlist, ippermnet01) and addressInNetwork(ippermlist, ippermnet02)
		n=str("23/tcp open ")
		n1=str("22/tcp open ")
		a=str(ippermlist)
		if n in str(out):
			telnetcall(a,username,password)
		elif n1 in str(out):
			sshcall(a,username,password)
	else:
		print ("                                                                                                                      ")
		print ("                                                 ---------- "+ippermlist+" Host not in scope               -----------")
========================SCRIPT=======================
next time post your code by clicking 'insert python'

look at 2to3
Since Python 3, it differs between bytes (raw bytes, no known encoding, binary data) and strings (encoded default with utf8).
Using encryption in Python 3 requires always bytes.

To reduce the pain, you could always use strings as long as you can and when a function requires bytes, then encode them at this place.
If you want to encrypt binary files, you have to open them in binary mode.

str.encode() -> bytes
bytes.decode() -> str