How send command or data after running an executable through pypsexec? - python

I used the following Python script to run an executable remotely and I can send input data through stdin.
from pypsexec.client import Client
c = Client("my_ip", username="myusername", password="mypassword")
c.connect()
try:
c.create_service()
stdin b'first-input\nsecond-input\n'
stdout, stderr, rc = c.run_executable("app.exe", stdin=stdin)
print(stdout)
finally:
c.remove_service()
c.disconnect()
I need to send my data after executing my application.

Related

Run interactive python class within paramiko [duplicate]

I'm having a problem with a ShoreTel voice switch, and I'm trying to use Paramiko to jump into it and run a couple commands. What I believe the problem might be, is that the ShoreTel CLI gives different prompts than the standard Linux $. It would look like this:
server1$:stcli
Mitel>gotoshell
CLI> (This is where I need to enter 'hapi_debug=1')
Is Python still expecting that $, or am I missing something else?
I thought it might be a time thing, so I put those time.sleep(1) between commands. Still doesn't seem to be taking.
import paramiko
import time
keyfile = "****"
User = "***"
ip = "****"
command1 = "stcli"
command2 = "gotoshell"
command4 = "hapi_debug=1"
ssh = paramiko.SSHClient()
print('paramikoing...')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname = ip, username = User, key_filename = keyfile)
print('giving er a go...')
ssh.invoke_shell()
stdin, stdout, stderr = ssh.exec_command(command1)
time.sleep(1)
stdin, stdout, stderr = ssh.exec_command(command2)
time.sleep(1)
stdin, stdout, stderr = ssh.exec_command(command4)
time.sleep(1)
print(stdout.read())
ssh.close()
print("complete")
What I would expect from the successful execution of this code, would be for the hapi_debug level to be 1. Which means that when I SSH into the thing, I would see those HAPI debugs populating. When I do, I do not see those debugs.
I assume that the gotoshell and hapi_debug=1 are not top-level commands, but subcommands of the stcli. In other words, the stcli is kind of a shell.
In that case, you need to write the commands that you want to execute in the subshell to its stdin:
stdin, stdout, stderr = ssh.exec_command('stcli')
stdin.write('gotoshell\n')
stdin.write('hapi_debug=1\n')
stdin.flush()
If you call stdout.read afterwards, it will wait until the command stcli finishes. What it never does. If you wanted to keep reading the output, you need to send a command that terminates the subshell (typically exit\n).
stdin.write('exit\n')
stdin.flush()
print(stdout.read())

I'm writing a paramiko common method to accept stdin and run command on other node [duplicate]

I'm having a problem with a ShoreTel voice switch, and I'm trying to use Paramiko to jump into it and run a couple commands. What I believe the problem might be, is that the ShoreTel CLI gives different prompts than the standard Linux $. It would look like this:
server1$:stcli
Mitel>gotoshell
CLI> (This is where I need to enter 'hapi_debug=1')
Is Python still expecting that $, or am I missing something else?
I thought it might be a time thing, so I put those time.sleep(1) between commands. Still doesn't seem to be taking.
import paramiko
import time
keyfile = "****"
User = "***"
ip = "****"
command1 = "stcli"
command2 = "gotoshell"
command4 = "hapi_debug=1"
ssh = paramiko.SSHClient()
print('paramikoing...')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname = ip, username = User, key_filename = keyfile)
print('giving er a go...')
ssh.invoke_shell()
stdin, stdout, stderr = ssh.exec_command(command1)
time.sleep(1)
stdin, stdout, stderr = ssh.exec_command(command2)
time.sleep(1)
stdin, stdout, stderr = ssh.exec_command(command4)
time.sleep(1)
print(stdout.read())
ssh.close()
print("complete")
What I would expect from the successful execution of this code, would be for the hapi_debug level to be 1. Which means that when I SSH into the thing, I would see those HAPI debugs populating. When I do, I do not see those debugs.
I assume that the gotoshell and hapi_debug=1 are not top-level commands, but subcommands of the stcli. In other words, the stcli is kind of a shell.
In that case, you need to write the commands that you want to execute in the subshell to its stdin:
stdin, stdout, stderr = ssh.exec_command('stcli')
stdin.write('gotoshell\n')
stdin.write('hapi_debug=1\n')
stdin.flush()
If you call stdout.read afterwards, it will wait until the command stcli finishes. What it never does. If you wanted to keep reading the output, you need to send a command that terminates the subshell (typically exit\n).
stdin.write('exit\n')
stdin.flush()
print(stdout.read())

Redirecting output from remote server to Python script

I am trying to create a python script that will gather some basic server's information and print it to the user who invoked the script.
def checks(argv):
host = argv[1]
sshclient = paramiko.SSHClient()
sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy)
if argv[2] == "22":
sshclient.connect(hostname=host, username='root', password=argv[3], port=argv[2])
stdin, stdout, stderr = sshclient.exec_command('top -n1')
data = stdout.read().decode("utf-8") + stderr.read().decode("utf-8")
print(data)
elif argv[2] == "22211":
sshclient.connect(hostname=host, username='root', key_filename=key_path, port=22211)
stdin, stdout, stderr = sshclient.exec_command('top -n1')
data = stdout.read() + stderr.read()
data.decode("utf-8")
print(data)
print(type(data))
#here I use my function
checks(sys.argv)
I am using SSH library to connect to server, then perform needed commands and save the data streams to my variable. Is there a possibility to, for example, invoke 'top' on remote machine and constantly redirect output from top to my Python variable so I can show it so it would like I am simply running 'top' on my machine and see the updating result?
The final thing I want to achieve is a simple program with GUI which will have several tabs, one of them will show running top output, another will show whole log file of some program (like Apache), etc etc.
Is that possible? Any recommendations on how I can do so?

Suprocess process.stdout.readline() freezes the program

I am trying to make a python program communicate with a minecraft server I am hosting, but the problem I am finding is when the console output from the server, it freezes the program. I am using Popen and PIPE from subprocess, and whenever I use process.stdout.readline() it prints all the lines and when it is done, it freezes the program, and then I am not able to execute any more commands because of that.
def start(cmd):
try:
process = Popen(cmd, stdin=PIPE, stdout=PIPE)
except Exception as e:
print(e)
return process
start('java -Xmx1024M -Xms1024M -jar server.jar nogui') # runs the minecraft server with 8GB of RAM
while True:
print(process.stdout.readline()) # prints out every line in the server console
readmail() # the program checks email for commands, and enters the command into the console using stdin
I have tried this:
def start(cmd):
try:
process = Popen(['python', '-u', cmd], stdin=PIPE, stdout=PIPE)
except Exception as e:
print(e)
return process
It fixes the issue with process.stdout.readline(), but it gives me an error because the command is meant to be called in cmd and not with python. Does anyone know how to fix the issue with readline()?

why paramiko ssh can't output the print info in realtime

I use python3 ssh, now I can login remote device, and execute my remote C program, but the program print info can't display on local pc in realtime. If my C program use setbuf(stdout, NULL); to set non-buffering, my PC can get realtime info; I want to know if the C program don't set non-buffering, how can I get the remote realtime info on local PC.
Here is my code:
def get_ssh_log(hostip, login_name, pw, privaete_key, cmd):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='%s' % hostip, port=22, username='%s' % login_name, password='%s' % pw,key_filename='%s' % privaete_key)
stdin, stdout, stderr = ssh.exec_command("%s" % cmd, bufsize=1)
for line in iter(stdout.readline, ""):
print(line)
ssh.close()
You can run any program under the wrapper unbuffer from the expect package. In your case:
ssh.exec_command("unbuffer %s" % cmd)
Of course this assumes unbuffer is installed on the target machine.
Ref: https://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe

Categories