Not able to erase from input buffer when the line wraps (pexpect) - python

I am using a script to connect to another server. The skeleton looks something like this:
import pexpect
...
child = pexpect.spawn(f"""ssh {username}#{host}""")
child.setwinsize(136,1000)
child.expect("Password:")
child.sendline(pwd)
However, when I enter a very long input it wraps around to the next line. The problem I am facing is that when I try to erase it (backspace-it), it doesn't take me to the previous line. So the input stays as is on the previous line. But this is how it is shown to me in the terminal, behind the scene the input gets backspaced (if that's a word).

Related

PwnTools recv() on output that expects input directly after

Hi I have a problem that I cannot seem to find any solution for.
(Maybe i'm just horrible at phrasing searches correctly in english)
I'm trying to execute a binary from python using pwntools and reading its output completely before sending some input myself.
The output from my binary is as follows:
Testmessage1
Testmessage2
Enter input: <binary expects me to input stuff here>
Where I would like to read the first line, the second line and the output part of the third line (with ':' being the last character).
The third line of the output does not contain a newline at the end and expects the user to make an input directly. However, I'm not able to read the output contents that the third line starts with, no matter what I try.
My current way of trying to achieve this:
from pwn import *
io = process("./testbin")
print io.recvline()
print io.recvline()
print io.recvuntil(":", timeout=1) # this get's stuck if I dont use a timeout
...
# maybe sending data here
# io.send(....)
io.close()
Do I missunderstand something about stdin and stdout? Is "Enter input:" of the third line not part of the output that I should be able to receive before making an input?
Thanks in advance
I finally figured it out.
I got the hint I needed from
https://github.com/zachriggle/pwntools-glibc-buffering/blob/master/demo.py
It seems that Ubuntu is doing lots of buffering on its own.
When manually making sure that pwnTools uses a pseudoterminal for stdin and stdout it works!
import * from pwn
pty = process.PTY
p = process(stdin=pty, stdout=pty)
You can use the clean function which is more reliable and which can be used for remote connections: https://docs.pwntools.com/en/dev/tubes.html#pwnlib.tubes.tube.tube.clean
For example:
def start():
p = remote("0.0.0.0", 4000)
return p
io = start()
io.send(b"YYYY")
io.clean()
io.send(b"ZZZ")

Using Python3.x , in nano, the first line cannot be edited

In Python3, using nano, the very first line I create somehow gets "tabbed" over about several characters. This does not happen when the source code is initially created. E.g. the program can contain only one line, a simple:
print ("Hello World")
Once you save and you re-enter the program, it looks like this (moved to the right several spaces)on the first line:
print ("Hello World")
A 'cat' on the program does not show the first line "tabbing". The line is left justified (i.e., it looks normal). The program runs fine and without errors. It is important to note that re-entering the source in nano OR vi subsequently makes that first line totally disabled and it cannot be edited. What is possibly causing this?

Creating "Scrollable" Output in Command Line Programs

I have a program that outputs anywhere from 300-1000 lines of data. Rather than have it all output at once, I'd like it to have a manpages-like interface where it will display the first 50 or so lines of input and then the user can press 'f' or 'b' to navigate through the pages. Is there a way to do this in Python?
Note: I want to distribute the program, and I don't want to force users to pipe the output to less/more. Moreover, the output occurs in the middle of the program and is not the only output of the program, so I'm not sure if that would work any way.
You could do something very rudimentary like:
# pseudocode
def display_text(text):
lines = text.splitlines()
while lines remaining:
display next N lines
wait for key press
To "wait for key press", you could do something like this: http://www.daniweb.com/software-development/python/threads/123777/press-any-key-to-continue
Note: I would never do this, and I think it is very bad UIX, but ...
pager = subprocess.Popen(['less'], stdin=subprocess.PIPE)
Then write all of your command's output to the file-like object: pager.stdin

Multiple lines user input in command-line Python application

Is there any easy way to handle multiple lines user input in command-line Python application?
I was looking for an answer without any result, because I don't want to:
read data from a file (I know, it's the easiest way);
create any GUI (let's stay with just a command line, OK?);
load text line by line (it should pasted at once, not typed and not pasted line by line);
work with each of lines separately (I'd like to have whole text as a string).
What I would like to achieve is to allow user pasting whole text (containing multiple lines) and capture the input as one string in entirely command-line tool. Is it possible in Python?
It would be great, if the solution worked both in Linux and Windows environments (I've heard that e.g. some solutions may cause problems due to the way cmd.exe works).
import sys
text = sys.stdin.read()
After pasting, you have to tell python that there is no more input by sending an end-of-file control character (ctrl+D in Linux, ctrl+Z followed by enter in Windows).
This method also works with pipes. If the above script is called paste.py, you can do
$ echo "hello" | python paste.py
and text will be equal to "hello\n". It's the same in windows:
C:\Python27>dir | python paste.py
The above command will save the output of dir to the text variable. There is no need to manually type an end-of-file character when the input is provided using pipes -- python will be notified automatically when the program creating the input has completed.
You could get the text from clipboard without any additional actions which raw_input() requires from a user to paste the multiline text:
import Tkinter
root = Tkinter.Tk()
root.withdraw()
text = root.clipboard_get()
root.destroy()
See also How do I copy a string to the clipboard on Windows using Python?
Use :
input = raw_input("Enter text")
These gets in input as a string all the input. So if you paste a whole text, all of it will be in the input variable.
EDIT: Apparently, this works only with Python Shell on Windows.

Python: How to ensure the last line of code has completed?

I'm doing some kind of complex operation, it needs the last line(s) of code has completed, then proceed to the next step, for example. I need to ensure a file has written on the disk then read it. Always, the next line of code fires while the file haven't written on disk , and thus error came up. How resolve this?
Okay..
picture.save(path, format='png')
time.sleep(1) #How to ensure the last step has finished
im = Image.open(path)
You do not need to do anything unless the previous operation is asynchronous. In your case, you should check picture.save's documentation to see if it specifically define as asynchronous. By default everything is synchronize. Each line will complete before it continues to next operation.
Seems like you just want to check is that there's a file at path before trying to open it.
You could check for the path's existence before trying to open it and sleep for a bit if it doesn't exist, but that sleep might not be long enough, and you still might have a problem opening the file.
You really should just enclose the open operation in a try-except block:
try:
im = Image.open(path)
except [whatever exception you are getting]:
# Handle the fact that you couldn't open the file
im = None # Maybe like this..

Categories