zedsec390/tn3270lib

This Utility is not working for me.

nivrutha opened this issue · 31 comments

I am getting this exception

Traceback (most recent call last):
  File "C:/Users/a595080/Desktop/MAUI-PlaceOrder/MAUI-PlaceOrder/codebase/scripts/fidelity_libs/3270.py", line 13, in <module>
    test()
  File "C:/Users/a595080/Desktop/MAUI-PlaceOrder/MAUI-PlaceOrder/codebase/scripts/fidelity_libs/3270.py", line 9, in test
    tn3270.initiate(host, 23)
  File "C:\Users\a595080\Desktop\MAUI-PlaceOrder\MAUI-PlaceOrder\codebase\scripts\fidelity_libs\tn3270lib.py", line 474, in initiate
    self.telnet_data = self.recv_data()
  File "C:\Users\a595080\Desktop\MAUI-PlaceOrder\MAUI-PlaceOrder\codebase\scripts\fidelity_libs\tn3270lib.py", line 383, in recv_data
    buf = self.sock.recv(256)
socket.timeout: timed out

Is this over an SSL connection?

Please enable debugging and post the full log here. You can enable debugging with: tn3270.set_debuglevel(1) just make sure you post past it between three ticks like so: ```

I think it is getting connected in the SSL Block only ,in this piece of code

 except ssl.SSLError as e:
            non_ssl.close()
            try:
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.settimeout(timeout)
                sock.connect((host, port))
                self.sock = sock
            except Exception as e:
                self.msg('Non-SSL Error: %r', e)
                return False
        except Exception as e:
            self.msg('SSL Error: %r', e)
            return False
        return True

This is the code which invokes the library

import tn3270lib
def test():
 tn3270 = tn3270lib.TN3270()
 host = "10.175.1.42"
tn3270.set_debuglevel(1)
 tn3270.initiate(host, 23)

The error is same

  File "C:\Users\a595080\AppData\Roaming\JetBrains\PyCharm Community Edition 2017.1.3\helpers\pycharm\_jb_unittest_runner.py", line 35, in <module>
    main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)
  File "C:\Users\a595080\AppData\Local\Programs\Python\Python36-32\lib\unittest\main.py", line 93, in __init__
    self.parseArgs(argv)
  File "C:\Users\a595080\AppData\Local\Programs\Python\Python36-32\lib\unittest\main.py", line 140, in parseArgs
    self.createTests()
  File "C:\Users\a595080\AppData\Local\Programs\Python\Python36-32\lib\unittest\main.py", line 147, in createTests
    self.module)
  File "C:\Users\a595080\AppData\Local\Programs\Python\Python36-32\lib\unittest\loader.py", line 219, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Users\a595080\AppData\Local\Programs\Python\Python36-32\lib\unittest\loader.py", line 219, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Users\a595080\AppData\Local\Programs\Python\Python36-32\lib\unittest\loader.py", line 153, in loadTestsFromName
    module = __import__(module_name)
  File "C:\Users\a595080\Desktop\MAUI-PlaceOrder\MAUI-PlaceOrder\codebase\scripts\fidelity_libs\3270.py", line 14, in <module>
    test()
  File "C:\Users\a595080\Desktop\MAUI-PlaceOrder\MAUI-PlaceOrder\codebase\scripts\fidelity_libs\3270.py", line 9, in test
    tn3270.initiate(host, 23)
  File "C:\Users\a595080\Desktop\MAUI-PlaceOrder\MAUI-PlaceOrder\codebase\scripts\fidelity_libs\tn3270lib.py", line 474, in initiate
    self.telnet_data = self.recv_data()
  File "C:\Users\a595080\Desktop\MAUI-PlaceOrder\MAUI-PlaceOrder\codebase\scripts\fidelity_libs\tn3270lib.py", line 383, in recv_data
    buf = self.sock.recv(256)
socket.timeout: timed out

Thank you for submitting the error, could you please submit the debug log as well?

The error comes in the very beginning and there is no debug_level getting used.

The code flow goes upto

  def initiate(self, host, port=0, timeout=25):
        """ Initiates a TN3270 connection until it gets the first 'screen' """
        # if not self.check_tn3270(host, port):
        #	return False

        if not self.connect(host, port, timeout):
            return False
        print(self.sock)
        self.client_options = {}
        self.server_options = {}
        self.state = NEGOTIATING
        self.first_screen = False

        while not self.first_screen:
            print("here")
            self.telnet_data = self.recv_data()

Can you increase verbosity to 2 with: tn3270.set_debuglevel(2) and send me the results?

if you could also send me the exact output from this command:

python tn3270lib.py -d 10.175.1.42

It should look like:

TN3270(xxx,23): Tryin SSL/TSL
TN3270(xxx,23): << IAC DO TN3270
TN3270(xxx,23): >> IAC WILL TN3270
TN3270(xxx,23): Entering TN3270 Mode:
TN3270(xxx,23): 	Creating Empty IBM-3278-2 Buffer
TN3270(xxx,23): 	Created buffers of length: 1920
TN3270(xxx,23): Current State: 'TN3270E mode'
TN3270(xxx,23): << IAC SB TN3270 TN3270E_SEND TN3270E_DEVICE_TYPE SE
TN3270(xxx,23): >> IAC SB TN3270 TN3270E_DEVICE_TYPE TN3270E_REQUEST IBM-3278-2-E IAC SE
TN3270(xxx,23): >> IAC DONT '('

While setting debug level 2 also I am not getting any modified output.
Also in command prompt I am getting the same as below.I forgot to mention that I am using python 3.6 and so I modified certain syntax accordingly where it was showing errors.Kindly help me on the same

ty_libs>python tn3270lib.py -d 10.175.1.42
Traceback (most recent call last):
  File "tn3270lib.py", line 1735, in <module>
    test()
  File "tn3270lib.py", line 1729, in test
    tn.initiate(host, port)
  File "tn3270lib.py", line 628, in initiate
    self.telnet_data = self.recv_data()
  File "tn3270lib.py", line 540, in recv_data
    buf = self.sock.recv(256)
socket.timeout: timed out

C:\Users\a595080\Desktop\MAUI-PlaceOrder\MAUI-PlaceOrder\codebase\scripts\fideli
ty_libs>

Ok, well unfortunately I cannot fix this for python 3.6 without re-writting the entire library.

Unfortunately it looks like there's a problem with the timeout and you're unable to connect to the mainframe you're trying to connect to but since you've tried to port it to python 3.6 i can't help you. Sorry.

which python version you are using

My python version is 2.7.3. Here's what you should see on a failed connection to a non-existent host/timeout:

$ python --version
Python 2.7.13
$ python tn3270lib.py -d 1.1.1.1
TN3270(1.1.1.1,23): Tryin SSL/TSL
TN3270(1.1.1.1,23): SSL/TLS Failed. Trying Plaintext
TN3270(1.1.1.1,23): Error: timeout('timed out',)
TN3270(1.1.1.1,23): Printing the current TN3270 buffer:
TN3270(1.1.1.1,23): Generating the current TN3270 buffer in ASCII

Also I notice you're running on Windows which i've never tested on and is likely unsupported, have you tried in a linux vm?

oh no I am working on Windows only :( .

IS there any other way to connect to mainframe via python..?The inbuilt X3270 library is also not working for me .. :(

At best try with python 2.7 on windows and see if the unmodified code works, if not I can help you debug, but unfortunately I can't debug 3.6.

I got the below output

C:\datarefresh>python2 tn3270lib.py -d 10.175.1.42
TN3270(10.175.1.42,23): Tryin SSL/TSL
TN3270(10.175.1.42,23): SSL/TLS Failed. Trying Plaintext
TN3270(10.175.1.42,23): << IAC DO TN3270
TN3270(10.175.1.42,23): >> IAC WILL TN3270
TN3270(10.175.1.42,23): Entering TN3270 Mode:
TN3270(10.175.1.42,23):         Creating Empty IBM-3278-2 Buffer
TN3270(10.175.1.42,23):         Created buffers of length: 1920
TN3270(10.175.1.42,23): Current State: 'TN3270E mode'
TN3270(10.175.1.42,23): << IAC SB TN3270 TN3270E_SEND TN3270E_DEVICE_TYPE SE
TN3270(10.175.1.42,23): >> IAC SB TN3270 TN3270E_DEVICE_TYPE TN3270E_REQUEST IBM -3278-2-E IAC SE
TN3270(10.175.1.42,23): << IAC SB TN3270 TN3270E_DEVICE_TYPE TN3270E_IS I B M - 3 2 7 8 - 2 - E TN3270E_CONNECT R 5 0 8 E I F 2 SE
TN3270(10.175.1.42,23): Confirmed Terminal Type: IBM-3278-2-E
TN3270(10.175.1.42,23): LU Name: R508EIF2
TN3270(10.175.1.42,23): >> IAC SB TN3270 TN3270E_FUNCTIONS TN3270E_REQUEST IAC SE
TN3270(10.175.1.42,23): << IAC SB TN3270 TN3270E_FUNCTIONS TN3270E_REQUEST BIND_IMAGE TN3270E_IS SE
TN3270(10.175.1.42,23): >> IAC SB TN3270 TN3270E_FUNCTIONS TN3270E_REQUEST IAC SE
TN3270(10.175.1.42,23): << IAC SB TN3270 TN3270E_FUNCTIONS TN3270E_IS SE
TN3270(10.175.1.42,23): >> IAC SB TN3270 TN3270E_FUNCTIONS TN3270E_REQUEST IAC SE
TN3270(10.175.1.42,23): Processing TN3270 Data
TN3270(10.175.1.42,23): Parsing TN3270E Header
TN3270(10.175.1.42,23): Value Received: '\x05'
TN3270(10.175.1.42,23): TN3270 Command: Erase Write (Alternate)
TN3270(10.175.1.42,23): Processing TN3270 Write Command
TN3270(10.175.1.42,23): Processing TN3270 Data
TN3270(10.175.1.42,23): Parsing TN3270E Header
TN3270(10.175.1.42,23): Value Received: '\x05'
TN3270(10.175.1.42,23): TN3270 Command: Erase Write (Alternate)
TN3270(10.175.1.42,23): Processing TN3270 Write Command
TN3270(10.175.1.42,23): Insert Cursor (IC) 0x13
TN3270(10.175.1.42,23): Printing the current TN3270 buffer:
TN3270(10.175.1.42,23): Generating the current TN3270 buffer in ASCII
                                                                               
NETID R508EIF2 - NETWORK IS ACTIVE. ENTER YOUR APPLICATION NAME.

So is it now connected or not connected ?

Yes, it works in 2.7 on your system, you could now interact with the mainframe using the library.

Thanks .But still I have few more doubts .

import tn3270lib

tn3270 = tn3270lib.TN3270()
host = "10.175.1.42"
port = 23
tn3270.initiate(host, port)
data = tn3270.get_screen()
print data
tn3270.send_cursor("menu")
tn3270.send_enter()
data = tn3270.get_screen()
print data

It is still printing the data of first screen only :(.It is not navigating to the second screen

Check the example in the readme:

>>> tn3270.send_cursor("TSO")
>>> tn3270.get_all_data()
>>> tn3270.send_cursor("margo")
>>> tn3270.get_all_data()
>>> tn3270.send_cursor("secret")
>>> tn3270.get_all_data()
>>> data = tn3270.get_screen()

I tried the same with send_enter and without that also but it is not navigating to second screen and it is going on running

import tn3270lib

tn3270 = tn3270lib.TN3270()
host = "10.175.1.42"
port = 23
tn3270.initiate(host, port)
data = tn3270.get_screen()
print data
tn3270.send_cursor("menu")
tn3270.send_enter()
tn3270.get_all_data()
data = tn3270.get_screen()
print data

Output

C:\datarefresh>python2 3270.py
                                                                               N

ETID R510CG7A - NETWORK IS ACTIVE. ENTER YOUR APPLICATION NAME.



Please turn on debugging and include that information in your posts.

This is running in loop for debuglevel(1)

TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
TN3270(10.175.1.42,23): Recv'd 0 bytes
Traceback (most recent call last):
  File "3270.py", line 11, in <module>
    tn3270.get_all_data()
  File "C:\datarefresh\tn3270lib.py", line 673, in get_all_data
    self.msg(1,"Recv'd %i bytes", len(self.telnet_data))
  File "C:\datarefresh\tn3270lib.py", line 503, in msg
    print msg % args
KeyboardInterrupt

This usually means the connection was closed by the other side. What happens when you connect with a normal tn3270 client and send 'menu'?

It navigates me to the next screen :(

sorry, it looks like when the script sends 'menu' its closing the connection on you, I've pushed a fix so it won't loop forever like this but its still looks like it's closing the connection on you.

I would recommend debugging with wireshark to see what you're doing.

Sorry for troubling you again.How should the data be sent.What does this part of line actually do in

send_cursor . self.msg(1,"Generating Output Buffer for send_cursor")
		self.output_buffer.insert(output_addr, ENTER)
		output_addr += 1
		self.msg(1,"Output Address: %r", output_addr)
		self.msg(1,"Cursor Location ("+ str(self.cursor_addr) +"): Row: %r, Column: %r ",
					self.BA_TO_ROW(self.cursor_addr),
					self.BA_TO_COL(self.cursor_addr) )
		self.output_buffer.insert(output_addr, self.ENCODE_BADDR(self.cursor_addr))
		output_addr += 1
		self.output_buffer.append(SBA)
		self.output_buffer.append(self.ENCODE_BADDR(self.cursor_addr))

The screen takes me to the next screen by typing 'menu' followed by ENTER KEY .Also I looked at the wireshark information .PFB .
capture

Ok, try disabling TN320E with: tn.disable_enhanced()

Again the same output
capture1

Can you help me understand this part of code :

                self.output_buffer.insert(output_addr, ENTER)
		self.output_buffer.insert(output_addr, self.ENCODE_BADDR(self.cursor_addr))
		output_addr += 1
		self.output_buffer.append(SBA)
		self.output_buffer.append(self.ENCODE_BADDR(self.cursor_addr))

I was looking Wireshark when I am actually entering the command via RUMBA session. Then the output will be like below .'menu' is getting attached to the header .Kindly guide me on the same .
mainframe

And for another command it is getting appended to Body .
bodycase

Couple of questions.

  • Looking at your question I can't find the code you're asking about. I think you're referring to this code: https://github.com/zedsec390/tn3270lib/blob/master/tn3270lib.py#L1629 but it looks different than what you've asked about. If you have specific questions about the code Please link directly to the line you'd like to know about.

  • Since I don't have access to your network or machine you'll have to decode this yourself. First by disabling TN3270E in rumba and then comparing the sessions to one another. Unfortunately I can't decode your wireshark sessions unless you want to send me the full pcap for the session. For example, when you say 'Then the output will be like below .'menu' is getting attached to the header' I have no idea where that is in the screenshot.

Hi I have underlined the place where menu is getting attached in the header in PICTURE1 and this happens when I connect to RUMBA normally and type menu.But when I pass parameter to send_cursor function as 'menu' ,it is getting appended to body in PICTURE2.How to disable TN3270E option in RUMBA .Can I select 'Do only TN3270' checkbox in PICTURE3 .

PICTURE1

menuheader

PICTURE2

menubody

PICTURE3

tn3270edisbale

Reattaching PICTURE1 again with 'MENU' underlined
mainframe