How to use ssh with python on windows without paramiko? - python

On machine A I want to have a small python script to execute a command on machine B. Machine A is Windows with freesshd running on it, and machine B can be either Windows or Linux. I only have a username and a password to log in to machine B, no other way of authentication.
How can I create a python script to run a command on machine B? ssh would be fine, but where to find that command on Windows? And it has to be done without paramiko, since this module does not wok for unknown reasons.

If you can't get paramiko working, just do it manually with Popen: http://python-for-system-administrators.readthedocs.org/en/latest/ssh.html

I haven't tried this on Windows but to handle input and output of a command pexpect is a good candidate. It also has a ssh part http://pexpect.readthedocs.org/en/latest/api/pxssh.html.
Did some more digging. There is a windows port of pexpect: https://gist.github.com/anthonyeden/8488763
Use that along with putty on the command line in windows.
Example of Putty on command line: http://etherealmind.com/putty-command-line/

Related

Batch SFTP commands with Python and Paramiko

I am porting some batch jobs from Window to Linux. These are .bat scripts on Windows and I am rewriting them in Python to run on Linux
On Windows, we currently use putty to SFTP files and run commands like this:
psftp user#host -i privatekey.ppk -batch -b script.txt
This executes the putty SFTP commands found in script.txt in batch. Example below:
SCRIPT.TXT example
cd mydir
lcd outbox
get myremotefile.txt mylocalfile.txt
get myotherremotefile.txt myotherlocalfile.txt
bye
I am trying to find a way to use the same batch script (script.txt) in my python script to minimise the amount of changes overall. I've been looking into paramiko but so far I have not been able to find a way to execute the sftp commands in bulk via a script file.
An alternative would be to spawn a sub-process to execute the sftp command with the -b option but would prefer a python native solution if possible.
What do you think? Are there other options out there to solve this?
The commands in you script are psftp commands. Not "SFTP commands" (there are no SFTP commands). In general, no other application nor library understands them. Except for OpenSSH sftp, as PuTTY psftp was made to be somewhat compatible.
If you want to use a native Python SFTP library, like Paramiko or pysftp, you will need to use its API. And that means rewriting your code. It's not difficult.

How to use fabric with ssh keys?

Using fabric 2.4 and trying to set up ssh keys to remotely connect to various linux servers?
Python is new to me, I've followed example on this site and Read python doc but still unclear to me.
Currently running python on windows and my script is able to connect to remote linux servers because i have connection string defined as follows:
ssh_connect = Connection(host='servername', user='user1', connect_kwargs={'password': 'blahblah'})
I am running python script from my window server and instead of defining the connection string, I would like it to use ssh key. I have the id_rsa.pub file from the linux server. I would like to setup up on my windows box and have the script use that for connections?
You can always just pass a key via Fabric through it's command line arguments. This is definitely kind of a vague question, especially since it's running fabric on Windows, which to my knowledge is not technically supported by them, though it does work.
fab -i /Path/to/key
Source: fab --help and the official documentation here

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?

Cannot automate a remote program using python and dogtail, no graphical interface in the remote server…

I wrote a code using python and dogtail (automation program from redhat) to automate a program. This program is running in a virtual machine without desktop graphical environment.
I ssh the virtual machine and run the program and the graphical interface start on my machine.
I then run my python program on my machine to automate this program. And here is the problem... the python code does not work because it did not recognize the remote program that is run via ssh. And if I move the python code to the virtual machine it asks me to enable the “assistive technologies” and I can’t because I do not have gnome or any graphical environment in the virtual machine?
All used operating systems are Ubuntu…
I try to find solution everywhere without success. I wish I explain the problem clearly, if not please let me know...
Please help… thank you
There is a dogtail-run-headless-next binary in dogtail and it should be able to start X (or more precisely gdm service) and log in (with autologin of test user). Then it's parameter is executed in desktop environment.
we use (under root)
sudo -u test dogtail-run-headless-next "./your_script"
I hope this will enable assistive technology, too. If not you can always use:
if not isA11yEnabled():
enableA11y(True)
You can automate ssh into a machine via pexpect. See new: http://pexpect.readthedocs.org/en/latest/api/pxssh.html
or older:
http://pexpect.sourceforge.net/pexpect.html

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.

Categories

Resources