Getting data generated in putty Application using COM objects in python - python

I am trying to write application which uses WSH scripting under Python for automation purpose. I used win32com.client module to get shell and run the putty application to access routers. Iam able to access the devices and send the key strokes and commands to putty GUI window. But the problem is,I want to track the ongoing command status which was sent using COM object.How can I read/get the buffer data of application initiated by COM object.Is there any easy way?Can I can get the data in python variable which is returned by device in putty ! Please help..`
import time
import win32api
import win32com.client
shell = win32com.client.gencache.EnsureDispatch("WScript.Shell")
shell.Run("putty")
time.sleep(1)
shell.SendKeys("192.168.1.x")
shell.SendKeys(r"show version | no-more") #command to run on putty
console
time.sleep(2)
shell.SendKeys("~")
Thanks,
Sat

Related

How to automate the TeraTerm application

I'd like to automate TeraTerm. I know how to open it, but don't know how to code the rest of it because I'm not familiar with Python syntax, particularly the Subprocesses module. How to select the serial and change the port?
code to open TeraTerm:
import subprocess
subprocess.call('"C:\\Program Files (x86)\\teraterm\\ttermpro.exe"')
after execution of the above code tera term application will open but how to select "serial" and choose different port.

How can I automate a Kali Terminal in Python?

In Kali, I'm trying to build a python library for automating some pen testing tasks by opening shells so that users can interact with things like created tunnels, ssh sessions, etc.
I started trying something like:
from subprocess import Popen,PIPE
p1 = Popen(['x-terminal-emulator'], stdin=PIPE)
p1.communicate('echo "hello world"')
and also trying sending stdin input, this creates the terminal but doesn't send the hello world command.
How can I do basic tasks with this newly created terminal window in Kali?
Send input to the terminal
Read output
Position the terminal window
I'm not asking for the code, just a pointer on where to get started would be great.

accessing Putty outputstream with pywinauto

I am trying to automate a ssh connection with pywinauto. I am connecting via Putty to my server and can execute commands. However, I need to get the output of the Putty to verify what exactly is going on the server. Below you find my example code so far which I find on stackoverflow:
import time
import sys
app = Application ().Start (cmd_line=u'putty -ssh root#host')
putty = app.PuTTY
putty.Wait ('ready')
time.sleep (1)
putty.TypeKeys ("cd{SPACE}/")
putty.TypeKeys ("{ENTER}")
time.sleep (1)
putty.TypeKeys ("ls")
putty.TypeKeys ("{ENTER}")
I need a function that gives me the outputstream of the putty window. Something like:
stream = putty.ReadConsoleOutputStream()
lines = stream.readLines()
I tried it with:
print(putty.window_text())
but it only gives me the title of the window back. I have heard about paramiko but since the connection is passwordless, I couldn't access it via paramiko because i couldn't find a way to connect it without a password.
Thank you in advance for your help.

Paramiko does not allow to see Motion Streaming

I'm using motion to run a rudimentary livestream. It works perfectly when i start it in server side with:
sudo motion -c livestream.conf
This starts a video server in 8081 port and i can access perfectly from wherever i want inside my network.
The issue comes when i want to write a little script which will ssh using paramiko to server, start motion with the same command and open default browser directly in video stream url. Here the sample code:
import paramiko
import subprocess
import time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.1.111', username = 'pi', password = 'raspberry')
ssh.exec_command('sudo motion -c livestream.conf')
time.sleep(4)
subprocess.call('xdg-open "http://192.168.1.111:8081"', shell= True)
ssh.close()
a pidof motion in server shows that the service is running,but i can't access it!!! Because motion is running, i think is not the common problem with sudo/paramiko, but i don't have any idea why this does not work.
WORKAROUND
Motion has a daemon mode. Enabling it from
/etc/default/motion
it starts on boot and i can call it perfectly with:
subprocess.call('xdg-open "http://192.168.1.111:8081"', shell= True)
But is not exactly what i'm looking for, because i'd like to launch(and close, but this will be another thread sure!!) the daemon, not just access the stream.
This workaround executes
/etc/motion/motion.conf
as daemon.I copied my motion script in there and everything good.
But when i try to start the script as daemon (not on boot, with the code above), it tells me that it can't create PID file. Everything done as root. I'm getting close to the answer by myself, just a little more.

Automating a process in windows

We have a web application which alerts when a server refresh is needed. When the alert comes we have to open a windows application,give the server name & refresh the server. Can this process be automated by using any scripting languages?
You alert system could launch a python script. For example take a look here for a simple way to launch a python script when outlook gets an email.
Then you can use pywinauto, it has a very simple API:
from pywinauto import application
app = application.Application.start("notepad.exe")
app.Notepad.MenuSelect("Help->About Notepad")
app.AboutNotepad.OK.Click()
app.Notepad.Edit.TypeKeys ("pywinauto Works!", with_spaces = True)
Also see: Why write yet another automation tool if there are so many out there? for a comparison with other existing tools.

Categories

Resources