Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert v2 code to v3
#1
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=======================
Reply
#2
next time post your code by clicking 'insert python'

look at 2to3
Reply
#3
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Review my code: convert a HTTP date header to a datetime object stevendaprano 1 2,010 Dec-17-2022, 12:24 AM
Last Post: snippsat
  convert c code python satyanarayana 5 5,620 Sep-21-2021, 07:45 PM
Last Post: deanhystad
  How to convert a python code to binary? rsurathu 0 1,810 Aug-02-2020, 08:09 AM
Last Post: rsurathu
  Error in the code ->ValueError: could not convert string to float: ' ' eagleboom 1 3,227 Nov-29-2019, 06:19 AM
Last Post: ThomasL
  Can someone please help me convert this simple C ROT cipher code to Python code? boohoo9 5 3,460 Jun-14-2019, 03:02 PM
Last Post: DeaD_EyE
  code help to convert log file to csv format bmunagala 3 12,164 Jan-21-2019, 06:08 PM
Last Post: snippsat
  Convert arduino code to python JDutch 3 22,741 Nov-07-2018, 09:16 PM
Last Post: Larz60+
  Convert C code to Python code lostMarbles 6 32,723 Sep-19-2018, 06:23 AM
Last Post: pcsailor
  How to convert matlab code to python? Ricop522 0 5,228 Jan-13-2018, 01:55 AM
Last Post: Ricop522
  Looking code to convert my strings of line in Dict santosh 1 2,650 Nov-10-2017, 04:49 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020