SSH to a server and exit Python script (in the same shell) - python

I'm running a script that's determining login information for me, and in the end is outputting the login information that I need to use.
I am running the script in a terminal, and now I want it to SSH me with the credentials it has, exit the Python script on my computer and connect my current terminal to the new server.
Say I already have my sshHost, sshUser and sshPass as variables in the script. How do I run an SSH command in the current terminal and connect to that server?
I tried subprocess and spur, however I didn't really manage to get that going.
I would really appreciate your help and thanks in advance.

assuming the python prints the settings to stdout;
#!/bin/sh
export $(credentials.py)
exec ssh hostname

Related

SSH to jump server, check if service is running, if running execute command

I need to make a python script which goes into a jump server, then pings a set of ip's from a list. If the ping is successful, i will then need to ssh into that ip, check if a particular service is running, and then run a command if service is running. If not running, no need to execute command. Can you please give me an idea of how to go through this?
Fabric is maybe what you're looking for, it is a python library for executing shell commands remotely over ssh.

PHP - Execute python script on remote IP address and get result

here is what I am trying to do:
I have a PHP application running on a web server. There is also an other virtual machine (in the same network with the appropriate permissions) where there is a python script. Now, what I want to do is from the PHP to run the python script on the remote machine and get back the result.
The following code runs correctly on the cmd on the server.
cd C:\pstools
psexec.exe \\192.168.0.ip cmd
cd "path of the python script on the remote machine"
python main.py
This works great and returns the result.
Now... how do I run this from PHP? I already tried with exec commands and I also tried with .bat file. I mean, to right the code on a batch file and execute it with the exec command through PHP.
The batch files looks like this:
cd /d C:\pstools
psexec.exe -h -accepteula \\192.168.0.ip cmd
cd "path of the python script on the remote machine"
python main.py
This finishes without an error, but doesn't seem to actually run the cmd on the remote machine. I've also tried some variations of the psexec command, like adding the path of the file on the same command, but still no luck.
Any idea on how to address this would be appreciated!
Thank you all in advance :)

How do I use PuTTY to execute commands on my server from a script?

With the ssh command one can pass it arguments to tell it to run commands on the remote server. I am trying to achieve the same thing, but with PuTTY.
I have PuTTY on my Windows machine, installed to C:. I am trying to invoke it from a local Python script, and have it invoke a command show system info on the server.
This is the sort of pseudo-Python that I am thinking of:
import ssh
server=ssh.Connection(host='10.201.20.240')
result=server.execute('show system info')
and more specifically using PuTTY from the Python script, something like this (which is of course not right, otherwise I wouldn't be asking this)
command = '"c:\Putty\putty.exe" -ssh user#10.201.20.240 -pw admin 10.201.20.240 '
result=command.execute('show system info')
subprocess.Popen(command)
If this were the ssh command I would be using ssh … user#10.201.20.240 show system info and suchlike.
What is the command-line syntax for the Windows PuTTY program for doing this?

Open Python shell through SSH

I'm using this tool to set up a ssh server on Windows. I'm trying to open the standard Python shell through a remote ssh connection but I simply can't get it to work. If I type 'python' in my ssh command line nothing happens, it just seems to wait for more input. My server machine however, shows a new python process running after I do this.
Running scripts works fine, though.
Do I need to use another Python shell, some other ssh server, some different configs?
Thanks
My guess is that Python is not recognising the stdin on the SSH shell as a terminal. I don't know why that would be.
However, try running "python -i" to overcome it.
The problem is probably that you're running the Windows Python executable, which expects a Windows console environment to run in, over a channel which doesn't support the features of Windows console. You might find Andy Koppe's conin to be useful.

Cannot write a script to "svn export" in Python

I would like to write a script that will tell another server to SVN export a SVN repository.
This is my python script:
import os
# svn export to crawlers
for s in ['work1.main','work2.main']:
cmd = 'ssh %s "cd /home/zes/ ; svn --force export svn+ssh://174.113.224.177/home/svn/dragon-repos"' % s
print cmd
os.system(cmd)
Very simple. It will ssh into work1.main, then cd to a correct directory. Then call SVN export command.
However, when I run this script...
$ python export_to_crawlers.py
ssh work1.main "cd /home/zes/ ; svn --force export svn+ssh://174.113.224.177/home/svn/dragon-repos"
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-with-mic,password).
svn: Connection closed unexpectedly
ssh work2.main "cd /home/zes/ ; svn --force export svn+ssh://174.113.224.177/home/svn/dragon-repos"
Host key verification failed.
svn: Connection closed unexpectedly
Why do I get this error and cannot export the directory? I can manually type the commands in the command line and it will work. Why can't it work in the script?
If I change to this...it will not work. and instead, nothing will happen.
cmd = 'ssh %s "cd /home/zes/ ;"' % s
This is a problem with SSH.
Permission denied, please try again.
This means that ssh can't login. Either your ssh agent doesn't have the correct key loaded, you're running the script as a different user or the environment isn't passed on correctly. Check that the variables SSH_AUTH_SOCK and SSH_AGENT_PID are passed to the subprocess of your python script.
Host key verification failed.
This error means that the remote host isn't known to ssh. This means that the host key is not found in the file $HOME/.ssh/known_hosts. Again, make sure that you're checking the home directory of the effective user of the script.
[EDIT] When you run the script, then python will become the "input" of ssh: ssh is no longer connected to a console and will ask python for the password to login. Since python has no idea what ssh wants, it ignores the request. ssh tries three times and dies.
To solve it, run these commands before you run the Python script:
eval $(ssh-agent)
ssh-add path-to-your-private-key
Replace path-to-your-private-key with the path to your private key (the one which you use to login). ssh-add will ask for your password and the ssh-agent will save it in a secure place. It will also modify your environment. So when SSH runs the next time, it will notice that an ssh agent is running and ask it first. Since the ssh-agent knows the password, ssh will login without bothering Python.
To solve the second issue, run the second ssh command manually once. ssh will then add the second host to its files and won't ask again.
[EDIT2] See this howto for a detailed explanation how to login on a remote server via ssh with your private key.
I guess that it is related to ssh. Are you using a public key to automatically connect. I think that your shell knows this key but it is not the case of python.
I am not sure but it's just an idea. I hope it helps
Check out the pxssh module that is part of the pyexpect project:
https://pexpect.readthedocs.org/en/latest/api/pxssh.html
It simplifies dealing with automating ssh-ing into machines.

Categories

Resources