How can i execute tcl command [using tclsh prompt] remotely using python 2.7 module? - python-2.7

Here is my set up.
Windows PC1 where Python 2.7 code is running ----> Windows PC2 where tcl is installed.
Windows PC1 and Windows PC2 are connected through LAN and able to access each other.
Now i want to open CMD line (As administrator) of PC2 from PC1 using python and execute windows command.
Yes this can be achieved through paramiko.
Now i want to run "tclsh" command in PC2 command prompt. After this the command line goes inside tcl prompt [%].Here i can execute tcl command like puts "Welcome". So if i try to execute "tclsh" command through paramiko its is in waiting state as the prompt has been changed from windows prompt to %.
My intention is to run tcl command in tclsh prompt [PC2 cmd line] through python [PC1].
Example: From PC1 i want to execute "puts "Hello" to PC2 tcl prompt through python2.7.
Please suggest me some idea what can be used [python module or any other approach] to access tclsh prompt through python 2.7.
Through paramiko normal windows commands are working as expected but tclsh enter into % prompt so paramiko is not working in this scenario.
I want to get access to tcl prompt through python 2.7 remotely.
I want to execute tcl command inside tclsh prompt in remote system through python.Here is the small program i have tried .
host = '192.168.1.4'
user = 'lenovo'
passw = 'XXX'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, username=user, password=passw)
stdin, stdout, stderr = ssh.exec_command('tclsh')
print (str('.'.join(stdout.readlines())))
print (str('.'.join(stdin.readlines())))
print (str('.'.join(stderr.readlines())))
But i do not see any output as tclsh goes into new % prompt. How do i solve this? After i have access to tclsh % prompt i want to execute "puts "Welcome""
.Instead of tclsh if give "hostname" windows command i get reply.

I got the solutions to access tclsh prompt for a remote windows pc using pramiko in python 2.7.
Step1: Use invoke shell method for tclsh command for the first time and store the return of invoke_shell to a globa variable.Suppose the return is x.
Step2: Now for any other command for tclsh prompt [like cmd = puts "stack"] use like x.send(cmd+"\n")
Step3: Now we can get output buffer like x.recv(9999)
Note [For windows only]: x.recv(9999) returns actual output with many escape sequences. We need to clean that to get the actual data.

Related

Run source command using python and keep the environment intact for next set of commands

I am trying to use subproces.Popen() to launch some command like below :
cmd = source /dev/stdin <<< "$(docker run --rm XX/YY-application:${APP_TAG} test)"
proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, executable="/bin/bash"
)
Now it doesn't return anything, and even using proc.stdout.readline() returns me new line character. While running the same cmd, on the shell gives me proper output.
So, I have two issues:
1. why printing the stdout not giving me anything?
2. how can I save the environment created by cmd, to be used in subsequent command?

How to run iostream system command in the same thread?

I'm trying to execute a command using std::system from Unreal Engine C++
FString command = FString("start /B cmd /k python \"D:/app.py\"");
std::string comm(TCHAR_TO_UTF8(*command));
std::system(comm.c_str());
The command itself is working as expected, however, I need it to execute on the current thread, or alternatively check if it's finished before continuing because the next operations depend on the completion of this command
Is there a way to do it? or maybe I should use another command?
Thanks, Eden
The std::system function will not return until the command you execute have finished running.
Also on Windows (which you seem to be running) then system will invoke the command interpreter (cmd) for execution of the command, which means the command you want to execute must be in the command interpreters PATH (or be an internal command of the command interpreter).
If python is in the PATH, then you could run the python command directly, without using start or cmd (especially since then you would have two instances of cmd running), and the system function would block and not return until the python command finished running:
FString command = FString("python \"D:/VRoscopy_repo/VRoscopy/conversion/invesalius3-master/app.py\" --no-gui -i \"D:\VRoscopy_repo\DICOM\Human\MedDream\Body\" -t 200,3033 -e \"D:\VRoscopy_repo\DICOM\Human\MedDream\Body/VRoscopy27777BED4B650CE6AFE884B365C56BCC.stl\"");

QProcess can launch programs but not python (command line)

The following code is failing to launch the python command line.
QProcess *myProcess = new QProcess(this);
myProcess->start("\"C:\\Program Files\\Python27\\python.exe\"");
If I replace python27 with (for example)
myProcess->start("\"C:\\Program Files\\Notepad++\\notepad++.exe\"")
notepad opens. Why is my program able to launch notepad but not Python Command Line?
I tried using startDetached() as suggested here but that didn't make a difference.
QProcess::Error() gives me error 5: unknown error.
If you just want to use the 'python console' you must use cmd.exe application from windows
You must have python in PATH so the windows console will know where to take it from.
So, you can try: QProcess::startDetached("cmd", "python")..see more specific syntax details here
It seems I've misunderstood what happens when you launch a command line. I was expecting the python command line or command prompt window to open.
It turns out that if I just pass my commands as arguments start() like so:
myProcess->start("cmd.exe /C python C:\\Users\\SP4\\Desktop\\helloworld.py");
Command prompt runs my python script and I get the output ("Hello World") using:
QString output = myProcess->readAllStandardOutput();
All this happens in the background and you can't actually see a command line window open and print out "Hello, World".
Please correct me if I've misunderstood something.

how to make terminal prompt messages?

I have a image processing project in C++ using opencv. The program runs correctly and I get the desired output. However, I have some messages that I print out using the cout command. When I run the program using the terminal (./myprogram) the messages are displayed correctly. When I double click the executable file I get only the output (in my case a new video is created) But I do get the messages. How do I get the program to automatically prompt the messages when it is not run from the terminal.
PS: I use ubuntu 14.04
Create a script like this, lets call it run.sh:
#!/bin/sh
cd work_dir
./myProgram
read -r -p "Press any key..." key
Then do:
xterm -e run.sh
and make your desktop shortcut run this command instead of the program directly.

Execute shell command in c++

I have a question regarding executing shell commands in c++. I'm building an application in winforms, vs 2008. My application has a button, when clicked should decode a binary file to a .csv file. I can decode files by first going to the right directory (cd Test_Copy2) and then execute a command in the command prompt (java -jar tool.jar -b x.fit x.csv). I tried a lot of different stuff but unfortunately got none to work!
I tried using:
system, _popen, ShellExecute(NULL, L"open", L"C:\\WINDOWS\\system32\\cmd.exe ", L"java -jar Tool.jar -b x.fit x.csv", L"C:\\Test_Copy2", SW_SHOWNORMAL)
Can anyone please provide me with an example on how to do that? I dont know where I'm going wrong, most of the time the command prompt opens but no command is executed!
If you really want to run the jar in a cmd.exe instance, you need to add one of the correct command line switches to cmd.exe for it to work the way you want it to:
/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains
For instance, your command string should be:
C:\\WINDOWS\\system32\\cmd.exe /c java -jar Tool.jar -b x.fit x.csv
You can use the system() function to execute shell commands.
For example:
system("DIR") executes the DIR command in the CMD shell. The default directory at the start is the directory you're .exe file is located.
'system("PAUSE")` executes the PAUSE command.
The command/s you wannt to execute should be passed as a constant string to the function.
Edit:
For you paritcular program the syntax (IMO) would be:
system("java -jar Tool.jar -b x.fit x.csv")