I am trying to run a python script on a remote computer via psexec. I am able to connect and run python.exe with the following:
C:\test>psexec \\192.168.X.X -u domain\administrator -p password -i C:\Anaconda\python.exe
The path to python.exe is the path on the remote machine. This opens a python window on the remote machine - all good.
I want to now pass a python script from the host machine to run on the remote. This script is on the host machine in C:\test\test.py. I tried
psexec \\192.168.X.X -u domain\administrator -p password -i "C:\Anaconda\python.exe" -c C:\test\test.py
and get:
C:\Anaconda\python.exe exited on 192.168.X.X with error code 1.
I also tried-c test.py without the full path, and got a similar error. My thought is the remote application cannot find C:\test\test.py. I want to be able to pass the script from the host machine.
Any help is much appreciated. Thanks.
If the .py extension has been associated with the Python installation on the remote machine, you may be able to run your Python script by simply removing the Python executable from the command line:
psexec \\192.168.X.X -u domain\administrator -p password -i -c C:\test\test.py
Please note that I have not tried this as I don't presently have access to a remote machine, so I can't guarantee that it will work.
The line
psexec \\192.168.X.X -u domain\administrator -p password -i "C:\Anaconda\python.exe" -c C:\test\test.py
may be trying to run the command "C:\Anaconda\python.exe" -c C:\test\test.py on the remote machine. In other words, Python may be interpreting the -c switch, rather than PsExec. The Python switch -c specifies some Python code to run, and of course a filename is not valid Python code:
C:\Users\Luke>python -c "print 2 + 2"
4
C:\Users\Luke>python -c C:\test\test.py
File "<string>", line 1
C:\test\test.py
^
SyntaxError: invalid syntax
C:\Users\Luke>echo %ERRORLEVEL%
1
Was able to access a python script on a shared drive from the remote computer and host, and so by copying to the share from the host and reading from the share on the remote machine i had a suitable workaround (the -i switch is not required).
psexec \\remote_machine_name -u domain\user -p pswrd -i C:/Anaconda/python.exe \\server\share\test\test.py
Related: if you are running on windows and writing to a UNC path from a python script i.e test.py above, helpful path formatting help:
python copy files to a network location on Windows without mapping a drive
Related
I have to run python code on remote python process.
Normally, what I would do is:
ssh admin#localhost -p 61234
which opens a interactive python console and I can execute python code directly.
>>> print('3')
3
>>>
But I want to automate this and pass python code as parameter to ssh.
I tried following options:
ssh admin#localhost -v -p 61234 python logs.py
ssh admin#localhost -v -p 61234 nohup python logs.py
ssh admin#localhost -p 61234 < logs.py
cat logs.py | ssh admin#localhost -p 61234 python -
But all options give following error:
shell request failed on channel 0
logs.py:
#!/usr/bin/env python
# tried with and without first line.
print('3')
netstat -anp | grep 61234
tcp 0 0 127.0.0.1:61234 0.0.0.0:* LISTEN 6/python2.7
Is there a way to do this?
Pretty sure this is overkill, but who knows, maybe you need more than just a simple command in the future.
Paramiko package is what you're looking for. The project is full of demos which demonstrate how and in many different ways.
The function you'll find the most useful is paramiko.client.SSHClient.exec_command
Execute a command on the SSH server. A new Channel is opened and the
requested command is executed. The command’s input and output streams
are returned as Python file-like objects representing stdin, stdout,
and stderr.
Demos Folder
In-depth Testing
Interactive.py is a fully interactive TTY (remote terminal control functions).
PyCharm Professional Edition (Python IDE from JetBrains) has tools for remote development, including SSH remoting, and the ability to run a remote interpreter:
I have a simple python script which I want to start a daemon-service in background in docker container
/sbin/start-stop-daemon --start --user root --make-pidfile --pidfile /var/lock/subsys/my-application.pid --exec 'python /opt/app/uc/monitor/bin/my-application.py'
when I execute this command in a shell I get
/sbin/start-stop-daemon: unable to stat //python /opt/app/uc/monitor/bin/my-application.py (No such file or directory)
However when execute just the below command in shell it works
python /opt/app/uc/monitor/bin/my-application.py
I'm sure the python is installed and all the links have been setup.
Thanks for the help
That error message implies that start-stop-daemon is looking for a file to open (the stat operation is a check before it opens the file) and treating your 'python ... ' argument as if it was a file.
See this example which confirms this. You may need to read the man page for start-stop-daemon, for your Ubuntu version, to check what a valid command would be for your setup.
Simplest solution is probably to create a shell script (say /opt/app/uc/monitor/bin/run-my-application.sh), and put this into it:
#!/bin/bash
python /opt/app/uc/monitor/bin/my-application.py
Be sure to do chmod +x on this file. If python is not found, use which python to find the path to python and use that in the script.
Now try:
/sbin/start-stop-daemon --start --user root --make-pidfile --pidfile /var/lock/subsys/my-application.pid --exec '/opt/app/uc/monitor/bin/run-my-application.sh'
Deployment by using Python throws error:
I used Python code ( its your deploy.py) to deploy our proxy (our company proxy) into apigee platform. i read http://apigee.com/docs/api-services/content/deploying-proxies-command-line
but it throws error when i run "python api-platform-samples-master/tools/deploy.py -n apikey -u "yusuf.karatoprak#mobgen.com:Welcome#2014" -o yusufkaratoprak123 -e test -p / -d sample-proxies"
i would like to solve this situation. i added to python code it is not working. it throws me Error: name 'ZipFile' is not defined
The -d flag value needs to point to the directory that contains the /apiproxy directory for the sample you want to deploy. (In your command above, it appears that you are pointing at /sample-proxies, rather than, for example, /sample-proxies/apikey
Try using the deploy scripts. There is one in each sample proxy directory. There's a also a script, /setup/deploy_all.sh if you want to deploy all sample proxies.
Make sure you update /setup/setenv.sh before running the deploy scripts.
The error is in how you are calling it from the command line. You have a space in one of the parameters you pass in, which needs to be put inside of quotes. Turn -u yusuf karatoprak:123 into -u "yusuf karatoprak:123"
Fixed command line call:
python api-platform-samples-master/tools/deploy.py -n weatherapi -u "yusuf karatoprak:123" -o yk123 -e test -p / -d simpleProxy
I have a very strange issue that I can't seem to figure out.
When I execute a python script containing the following lines while inside a SSH terminal (putty), it works fine. But the moment I run the script via crontab or even nohup python myscript >/dev/null 2>&1& it doesn't seem to execute these commands.
subprocess.call('rsync -avr /path/to/folder/. --include "delta.*" --exclude "*" -e "ssh -o StrictHostKeyChecking=no -i /path/to/key.pem" ec2-user#'+server+':/path/to/folder/', shell=True)
local('ssh -t -o StrictHostKeyChecking=no -i /path/to/key.pem ec2-user#'+server+' "sudo /usr/bin/indexer -c /path/to/sphinx.conf --merge main delta --rotate"')
Basically all the above is doing is syncing a folder with new sphinx search engine updates to a remote server, then the second line runs a remote ssh command to force the search engine to rotate updates into production.
I do have fabric installed (hence the local command) but to avoid having to fab a second file I was hoping a single line of code could allow me to execute sudo commands on a remote server.
Can someone help me out?
I found the answer, for ssh commands in a script run in the background, you need to to have -t -t to force a pseudo terminal.
Reference:
Pseudo-terminal will not be allocated because stdin is not a terminal
I am working on python, actually i am trying to connect to sftp and get some files after connecting.
The process which i followed on windows is below
import os
psftpCmd='psftp sftp.example.com -l user -pw pass -b client_configurations\lifebridge.scr -batch'
os.system(psftpCmd)
The code in lifebrige.scr is
cd lifebridge
lcd feeds\lifebridge
get jobs.xml
bye
So i am able to fetch the file successfully i want to do the same process on linux(fedora) machine and i tried the following
import os
psftpCmd='psftp sftp.example.com -l user -pw pass -b client_configurations\lifebridge.scr -batch'
os.system(psftpCmd)
Result:
sh: psftp: command not found
Here i can expect that psftp is putty command so need to do something else on linux fro the same, Can anyone let me now how to write the same command in linux
On Linux, the command is sftp.