Write ascii characters to putty from keyboard with Sikuli - putty

I'm trying to send some commands to terminal by putty using sikuliX. The problem is that some commands need to use characters like "#", ">" but when I'm trying to write it via keyboard (For example, alt + 64 for "#"), doesn't work. Any idea? Here is my code example:
openApp("notepad.exe")
sleep(2)
type("64",KeyModifier.ALT)
PS: I can't use paste(), because putty receive content from that command.

As far as I remember, the Java AWT Robot that is used internally by Sikuli's type(), is restricted to the use of Java defined key constants that are mapped to the standard layout of the US-EN qwerty keyboard. Hence the usage of type() is generally restricted to whatever you can produce with your keyboard.
Saying that I would still expect the Alt combination to work.
Try asking this question on Sikuli launchpad and when you get an answer, update it here. It's a good question.

I found a solution that works but I think that should exist more. I used the clipboard to paste the entire command that I want to send. Here is the explanation:
Using the clipboard
App.setClipboard("ssh user#hostname > /path/to/save/file.txt") # Here we save the command into clipboard to paste inside putty
if exists("puttyScreen.txt",10):
rightClick("puttyScreen.txt") # Here we paste the content of the clipboard

Related

Garbage/control characters observed in output from Paramiko when get_pty=True for Windows2019/2022 (CygwinOpenssh) [duplicate]

I am using Python's Paramiko library to SSH a remote machine and fetch some output from command-line. I see a lot of junk printing along with the actual output. How to get rid of this?
chan1.send("ls\n")
output = chan1.recv(1024).decode("utf-8")
print(output)
[u'Last login: Wed Oct 21 18:08:53 2015 from 172.16.200.77\r', u'\x1b[2J\x1b[1;1H[local]cli#BENU>enable', u'[local]cli#BENU#Configure',
I want to eliminate, [2J\x1b[1;1H and u from the output. They are junk.
It's not a junk. These are ANSI escape codes that are normally interpreted by a terminal client to pretty print the output.
If the server is correctly configured, you get these only, when you use an interactive terminal, in other words, if you requested a pseudo terminal for the session (what you should not, if you are automating the session).
The Paramiko automatically requests the pseudo terminal, if you used the SSHClient.invoke_shell, as that is supposed to be used for implementing an interactive terminal. See also How do I start a shell without terminal emulation in Python Paramiko?
If you automate an execution of remote commands, you better use the SSHClient.exec_command, which does not allocate the pseudo terminal by default (unless you override by the get_pty=True argument).
stdin, stdout, stderr = client.exec_command('ls')
See also What is the difference between exec_command and send with invoke_shell() on Paramiko?
Or as a workaround, see How can I remove the ANSI escape sequences from a string in python.
Though that's rather a hack and might not be sufficient. You might have other problems with the interactive terminal, not only the escape sequences.
You particularly are probably not interested in the "Last login" message and command-prompt (cli#BENU>) either. You do not get these with the exec_command.
If you need to use the "shell" channel due to some specific requirements or limitations of the server, note that it is technically possible to use the "shell" channel without the pseudo terminal. But Paramiko SSHClient.invoke_shell does not allow that. Instead, you can create the "shell" channel manually. See Can I call Channel.invoke_shell() without calling Channel.get_pty() beforehand, when NOT using Channel.exec_command().
And finally the u is not a part of the actual string value (note that it's outside the quotes). It's an indication that the string value is in the Unicode encoding. You want that!
This is actually not junk. The u before the string indicates that this is a unicode string. The \x1b[2J\x1b[1;1H is an escape sequence. I don't know exactly what it is supposed to do, but it appears to clear the screen when I print it out.
To see what I mean, try this code:
for string in output:
print string

How to find out keystroke bash associated textual representation?

As described in this answer is possible to map keystrokes to commands in a terminal. And to do this, there is a specific bash syntax for describes each key, as \e[11~ for F1 or Control-o for ControlO
How not everyone is deductible, I would like to find a way to discover each key associated string. If I just press it in terminal nothing happens for most of non-alphanumeric keys
I think you can alternatively install expect, start autoexpect and see what are the codes for your key strokes in generated file.

How to create custom escape sequence in mac terminal

I don't understand how to create custom escape sequence for Ctrl+Shift+S like in this question
My problem is that I want to use Ctrl+Shift+S in emacs no-window mode under MacOS terminal. Currently I'm trying to bind this escape sequence to Ctrl+Shift+F12 but I have troubles creating that escape sequence. Maybe there is a batter way to use complex (Ctrl-Shift+anykey) bindings in MacOS terminal with emacs in no-window mode?
You're probably assuming that F12 is a character. It is not. Conventionally, function keys send a sequence of characters.
In Terminal's preferences screen, you can review (most of) the key bindings:
You can also add a key definition (after deciding what characters it ought to send), and associate the Shift and Control modifiers with that key:
For the sake of example, I used the sequence that xterm would send. However, Terminal is not the same as xterm, and you may find other sequences more interesting.
Regarding the related question "What is the correct escape code for control/shift/s": there is no correct escape code because terminals (unless specially configured as illustrated above) do not distinguish between shifted- and unshifted-control characters. To determine what is "correct", you will have to examine your emacs bindings to see what you should send to make it recognize the function you are asking about.

How Can We Clear the Screen in Iex on Windows

Please how can we clear the screen in Iex on Windows
Documented method in Iex help does not work:
clear/0 — clears the screen
This StackOverflow Answer also does not work in Windows.
I've discovered it's possible, because native terminal in Windows 10 supports ANSI colors and escape sequences. The only thing is to enabled this in iex shell.
According to https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/io/ansi.ex#L41 this option is configurable. As a quick solution just type in your iex session following code:
Application.put_env(:elixir, :ansi_enabled, true)
In order to make it permanent, you can configure iex shell in ~/.iex.exs file (https://hexdocs.pm/iex/IEx.html#module-configuring-the-shell). Just paste following into the file:
IEx.configure [colors: [enabled: true]]
You can use ANSI codes directly in iex on Windows with consoles that support them (like ConEmu or Windows 10 console.)
This will clear the screen in iex:
iex> IO.write "\e[H\e[J"; IEx.dont_display_result
Explanation:
IO.write outputs to the console without a newline
\e[ is the prefix for ANSI CSI codes
H is the CSI CUP – Cursor Position code with no arguments, by default moves cursor to row 1, column 1
J is the CSI ED – Erase Display code with no arguments, by default clears the screen from the current cursor position
IEx.dont_display_result prevents the :ok result of IO.write from displaying after the screen is cleared
You can also clear the screen using IO.ANSI rather than raw escape codes:
iex> IO.write [IO.ANSI.home, IO.ANSI.clear]; IEx.dont_display_result
This is basically how clear/1 is implemented.
Yes, we can't clear it on Windows as far as I know. If there is one escape that we can output to the IO device to clear screens on Windows, I would love to know and add this functionality to Windows too. :)
Your best option (if this is a real problem for you rather than an annoyance) is to use an alternate Windows shell that supports ANSI Escape Sequences. See this S O question for why you can't simply use ANSI Escape sequences in a Windows Cmd Shell. One command shell alternative that does support ANSI is ConEmu. Configuring ConEmu on your machine is left as an exercise for the reader.
Add the following to ~/.iex.exs in your home directory -- If the file doesn't exist, create it and add the following.
Application.put_env(:elixir, :ansi_enabled, true)
Edit: Note that you'll need a term that supports this, ConEmu, Cmder, etc..

How to quickly scroll to the latest / end of command history in bash?

Lots of times I'll use Ctrl-R for reverse search and mistype some letter. Bash jumps up hundreds of lines and I'm in the middle of commands I was using a week ago.
Is there a shortcut for jumping back down to the lastest commands I had typed?
Edit: after testing it out on a CentOS server and Mac OS X, it looks like this only happening on OS X.
I've struggled with this same issue.
You can solve this by aborting with ctrl-c. Whether you're in the middle of a reverse search or scrolling through history with the arrows, aborting returns you to a prompt with the history scroll just after the last command.
UPDATE
Here's a nice trick I just learned. Bash and many other programs use Readline under the hood for command-line interpretation. Key bindings for Readline can be configured in a .inputrc file or with the bind command. The bindings can make use of a few functions provided by Readline. For example, I use Bash in vi mode but I still like to use Emacs-style ctrl-A so I have this line in my .bashrc file:
bind '\C-a:beginning-of-line'
To list all the available Readline functions:
bind -l
Among the functions is end-of-history. The function does like its name suggests. The difference between this approach and just using the abort command is that this keeps you on the same prompt.
If using libreadline, Alt-> (or Meta->). More info on Readline shortcuts or search for Commands for Manipulating the History in the man page.
On Mac, try command + .
It works for me.
I was trying alt+. and alt+shift+. , neither works for me. And then found command + . actually works
Maybe not exactly what you want, but you can fix your mistyped character(s) by using backspace when you're in the CTRL-r (reverse-i-search) mode.
You may wan to try "suggest box"-like history HSTR. It reads the bash history and allows quick navigation and filtering - you can see the context of similar history entries. Once you select a history entry it can be edited on the command line.
In Zsh with emacs binding set the actual default key sequence is ^[> binded to end-of-buffer-or-history command rather than command-. suggested above (or end-of-history depending on effect you want to achieve)
Cmd-. produces in Apple Terminal the similar or the same key sequence as Ctrl-C, which can be confirmed by running something useless and long, e.g. find . >/dev/null 2>&1 and pressing one and then other keys on keyboard.
Ctrl-C forces input to be ended and reset. and history scroll is just a side effect for it.

Resources