Pass command to KiTTY UI - python

I wish to automate the below task
Launch KiTTY
Enter username and password
Login successfully
Enter this below command to see the logs
tail -2000f /apps/test/good.log
I am able to achieve up to point 3 using below code
from subprocess import Popen
Popen("powershell kitty-0.73.1.1.exe sakthi#x.y.w.z -pw YYYY")
(new KiTTY windows is opened and user is logged in successfully)
But I don't know how to pass the below command
tail -2000f /apps/test/good.log
Note:
I am using Python3
I WANT THIS AUTOMATION AT UI LEVEL. I have around 5 to 6 log files to go through while testing. I don't want to open all the logs manually. So I am looking for a way to automate it.
I am using KiTTY, because it can re-connect automatically when there is any network problem.

KiTTY, as well as PuTTY, has -m command-line switch to provide a command for SSH "exec" channel.
This is discussed in: Automating command/script execution using PuTTY
KiTTY additionally has -cmd command-line switch, which (contrary to -m) simulates key strokes on SSH "shell" channel. It is an equivalent of KiTTY "Automatic Command" feature.
See also Open command line in C# and send commands PuTTY or KiTTY
Though if you want to automate testing, you better use a native Python SSH module, like Paramiko.

Related

Open a PuTTY window via Python subprocess.Popen (or Paramiko session) and run number of shell commands (like "top") in the same window

I want to open a PuTTY window, login to some server and enter few commands right after that and at the end save the output. I want to see the PuTTY window open and all activity that I am doing via program (hence need PuTTY GUI).
Here is what I have tried:
I am able to open a new window and login in it. But using stdin.write I am not able to enter further commands in the same window. What am I doing wrong here? I am very new to python. Please help.
import subprocess
from subprocess import Popen, PIPE, STDOUT
p = subprocess.Popen("putty.exe demo#Test.Rebex.Net -pw password", stdout=PIPE, stdin=PIPE, stderr=STDOUT)
p.stdin.write("ls".encode("utf-8"))
print(p.stdout.readlines())
I have tried Paramiko, and don't want to use it, as it fails to display/print the output of commands like top.
PuTTY is a GUI application, it does not use a standard input. It is not intended for automation. For automation, you can use Plink, what is a console equivalent of PuTTY.
See also Pipe PuTTY console to Python script
Though you should use Paramiko. It does not fail to display/print the output of commands like "top". It' not what it is for. You have to attach it to a terminal to achieve what you want. See Running interactive commands in Paramiko.
Or for automation, you better run the top non-interactively. See your other question Collect output from top command using Paramiko in Python.

How to execute a python script on a remote machine after accessing via PuTTY (SSH)? [duplicate]

These are the things I need to do:
Open putty.exe
Enter username and password.
Run a shell script.
I am using UFT (VB Scripting). I am able to open PuTTY but not able to enter username and password or run any commands using UFT.
Is there any other way I can achieve this? I have searched it and found that we can use Plink. Then the problem would be that the whole team will have to install Plink for that purpose. And that is not possible.
Thanks in advance.
PuTTY has the -m switch, that you can use to provide a path to a file with a list of commands to execute:
putty.exe user#example.com -m c:\local\path\commands.txt
Where the commands.txt will, in your case, contain a path to your shell script, like:
/home/user/myscript.sh
Though for automation, your better use the Plink command-line connection tool, instead of the GUI PuTTY application, as you have already found out. The Plink is a part of PuTTY package, so everyone who has PuTTY should have Plink too.
The Plink (plink.exe) has the same command-line arguments as PuTTY. And in addition to those, you can specify your command directly on its command like:
plink.exe user#example.com /home/user/myscript.sh
or using its standard input
plink.exe user#example.com < c:\local\path\command.txt
(of course, you will use redirection mechanism of your language, instead of the <).
Note that providing a command using the -m switch or directly on command-line implies a non-interactive mode, while using the standard input uses an interactive mode by default. So the results or behavior may differ. Use the -t and -T switches to force the interactive and the non-interactive mode, respectively.
You can add cmd arguments when you launch putty directly;
start C:\Users\putty.exe -load "server" -l userID -pw Password -m commands.txt
Can you not request the user name and pass prior and pass this along to the executable?
To run a single remote command or short series of commands is even easier by using the plink -batch flag instead of needing a script file. For example to show the OS name and a directory listing, do this:
plink user#host -pw password -batch uname;ls

Minecraft Server Executing Scripts

I am working on a minecraft world space that can interact with a terminal shell and run commands on the computer directly. I intend to use not just the vanilla server but maybe craftbukkit or spigot.
Is it possible to create a listener on minecraft server.jar and wait for a certain command which executes a script on the computer itself?
Is there a plugin out there made for this purpose?
You can create a new plugin for Bukkit/Spigot (more information here).
In the onCommand-Method you can then call Runtime.getRuntime().exec("your shell command") to run commands in the linux shell (can also be used on Windows servers).
See also the Java documentation.

A Telegram bot to control a Raspberry Pi through an interactive shell

I am trying to write the program for a Telegram bot to control my Raspberry Pi, so that every message I send the bot shall be interpreted as a shell command [1][2].
The Raspberry Pi is a version 2 model B and runs Arch Linux ARM. The program is written in Python 3.6.0 with the module Telepot, and is executed from the Pi.
So far, I have been using the module subprocess in order to execute the commands, like this:
# Execute a shell command (assuming that the message received is «text»)
P = subprocess.Popen(text, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
# Store the output and the error
(output, error) = P.communicate()
# Send a message with the output
if str(output) != "b''":
bot.sendMessage(my_id, output)
# And another message with the error
if str(error) != "b''":
bot.sendMessage(my_id, error)
The bot works just fine, but the problem is that I cannot execute every command like I would in a real shell. I am talking especially about those interactive commands that bypass the STDOUT and send their output directly to the tty (ssh, ftp... which typically prompt for a password and wait until the user types it). subprocess does not allow to read from or write to the tty. Infact, if I send my bot the command tty, it replies: not a tty.
Additionally, some shell commands update their output dinamically or keep running until some event happens (for example ping 8.8.8.8, or grep pattern), and I have not succeeded in reproduce them neither, because I cannot send a Ctrl+C or Ctrl+D. Note that this second one is a different problem, because it is possible to redirect ping´s output to a file (while it is not possible to redirect [sudo] Password for user:); but in this case I am unable to send input to the shell.
Also, as an optional, it would be fun if the bot could understand pipes, redirections and globbing.
What accomunates these problems is, to my mind, the fact that I cannot interact with the shell. I believe the solution to all of them is just one.
So the question is, broadly, how can I implement such a bot program that allows me to run interactive shell commands through it [3]?
An example of the final result I would like to achieve is (in the form of a conversation between me and the bot):
Me: pwd
Bot: /home/user/some/directory
Me: sudo chmod 777 file.txt
Bot: [sudo] password for user:
Me: qwerty
Me: ssh user#host
Bot: user#host´s password:
Me: qwerty2
Bot: Welcome to host...
or
Me: cat
Me: hello
Bot: hello
Me: test
Bot: test
Me: Ctrl+D
P.S.
I have tried the pexpect module for Python, but I think it is not so good for me because I do not expect any particular output; I just want to get anything the shell gives me. (Perhaps there is a way to do what I want that I do not know?)
I have also tried using fifo files, but they do not work since they are connected to STDIN and STDOUT, not to the tty.
[1] Example: I send the message pwd and the bot replies /working/directory.
[2] True, I could use ssh or similar; however, what intrigues me is that a bot would work independently of the operating system of the machine that performs the request, be it Linux, Windows, Android or anything, without installing additional software (without even installing Telegram, since there exists Telegram web).
[3] The question could also have been: how can I connect directly to a tty through a script? The script can also be written in bash or another language, if it is easier. But I do not want to bias you, I am open to every solution. Once I have the input/output in a variable, sending the messages is not a problem.
Forgive me if I answer my own question, but I have found precisely what I was looking for, and I'd like to share it with you.
https://jmendeth.com/blog/telegram-shell-bot/
At the link above you can find the instruction to install and use 'shell bot'; from there you can also reach the source code on github.
Though not written in Python, but in node.js, it seems perfect to me. It updates the messages on the fly and can also execute graphical commands.
P.S. You can even run vim through the bot!

Fabric - how to use an interactive shell on remote computer?

I am trying to use Fabric to run commands on a remote machine.
This works fine, until the command on the remote machine is interactive. In that case, Fabric return the interactive shell, but force me to type the info needed, while I am trying to send a command that does everything in remote, so I can automate the procedure.
Example:
from fabric.api import *
env.hosts=['myhost.mydomain']
env.user='root'
run(test1/myapp; exectask; exit)
I run a cli application on a remote machine, that uses interactive shell, so it is waiting for me to type the command (exectask); then once done, to exit, I call the exit command.
What happens now is that the app launch, Fabric show me the interactive UI and I still need to type exectask and after, exit.
How can I tell fabric to run that app, then pass the command to the interactive shell and then the exit to quit?
I see that Fabric has the prompt feature, but that's to ask the user to input data, while I want to just pass the commands and get the result back.
You might want to look into pexpect, a pure-Python module that works like expect. Essentially, it allows your program to spawn an external program or process, then control it just like a human was interacting with it. You program in what the program should expect to see (hence the name), then what action(s) to take.

Categories