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.
Related
I'm trying to find a way to execute commands with python over ssh. I did find this answer, however I'm working on an embedded platform and can't install new packages. In other words I can't install paramiko. Is there a way to do what paramiko does but with standard packages?
Is it a Unix based system with an ssh client? If so, could you use subprocess, https://docs.python.org/3/library/subprocess.html, to spawn another process and run ssh commands?
If not, have you tried to see if you can package paramiko itself with your code and deploy everything together to the target system? I'm not sure if this works, but it may be worth a shot.
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/
I would like to achieve the following things:
Given file contains a job list which I need to execute one by one in a remote server using SSH APIs and store results.
When I try to call the following command directly on remote server using putty it executes successfully but when I try to execute it through python SSH programming it says cant find autosys.ksh.
autosys.ksh autorep -J JOB_NAME
Any ideas? Please help. Thanks in advance.
Fabric is a good bet. From the home page,
Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
A quick example,
>>> from fabric.api import run, env, cd, settings, hide, show
>>> env.host_string='xxx.xxx.com'
>>> env.user='user'
>>> env.password='password'
>>> run('ls -lart')
After reading your comment on the first answer, you might want to create a bash script with bash path as the interpreter line and then the autosys commands.
This will create a bash shell and run the commands from the script in the shell.
Again, if you are using autosys commands in the shell you better set autosys environment up for the user before running any autosys commands.
I've been using Paramiko today to work with a Python SSH connection, and it is useful.
However one thing I'd really like to be able to do over the SSH is to utilise some Pythonic sugar. As far as I can tell I can only use the inbuilt Paramiko functions, and if I want to anything using Python on the remote side I would need to use a script which I have placed on there, and call it.
Is there a way I can send Python commands over the SSH connection rather than having to make do only with the limitations of the Paramiko SSH connection? Since I am running the SSH connection through Paramiko within a Python script, it would only seem right that I could, but I can't see a way to do so.
RPyC could be what you're looking for. It gives you access to another machine's Python environment directly from the local script.
>>> import rpyc
>>> conn = rpyc.classic.connect("someremotehost.com")
>>> conn.modules.sys.path
['D:\\projects\\rpyc\\servers', 'd:\\projects', .....]
To establish a connection over SSL or SSH, see:
http://rpyc.sourceforge.net/docs/secure-connection.html#ssl
Well, that is what SSH created for - to be a secure shell, and the commands are executed on the remote machine (you can think of it as if you were sitting at a remote computer itself, and that either doesn't mean you can execute Python commands in a shell, though you're physically interact with a machine).
You can't send Python commands simply because Python do not have commands, it executes Python scripts.
So everything you can do is a "thing" that will make next steps:
Wrap a piece of Python code into file.
scp it to the remote machine.
Execute it there.
Remove the script (or cache it for further execution).
Basically shell commands are remote machine's programs themselves, so you can think of those scripts like shell extensions (python programs with command-line parameters, e.g.).
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.