I want to write script on python which could execute shell commands on a remote server.
I find out that I could use something like:
# set environment, start new shell
p = Popen("/path/to/env.sh", stdin=PIPE)
# pass commands to the opened shell
p.communicate("python something.py\nexit")
But I do not understand how can I login to remote Linux server and execute shell commands there?
Look into using Paramiko or Pyro4 or fabric. All of these should do what you would like.
Related
I have a Python script on my local Jupiter notebook. Right now, I run a command on a Putty session to generate some data that I write to an output text file and the download to my local to be able to be read into the Python script. Is there any way to be able to write a command and do this from the script itself?
You can run the commands over ssh from python via paramiko library : https://www.paramiko.org
Example from the official documentation:
client = SSHClient()
client.load_system_host_keys()
client.connect('ssh.example.com')
stdin, stdout, stderr = client.exec_command('ls -l')
Use case
On a unix server , when login manually ,opens a command shell of its own to run the command.
I am trying to automate this by using paramiko , however , somehow i am not able to execute the command on command shell using paramiko
What i have done ?
I created a simple script which is able to make connection, but its not executing command on Vshell as the ouput is always coming empty.
import paramiko
import sys
ssh_client=paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=sys.argv[1],port=sys.argv[2],username=sys.argv[3],password=sys.argv[4])
command="show hwid"
stdin,stdout,stderr=ssh_client.exec_command(command)
out=stdout.read()
print out
err=stderr.read()
print err
ssh_client.close()
The same script runs perfectly fine , when its used on server where vshell is not being used
Anyhelp or suggestion on this?
stdin,stdout,stderr=ssh_client.exec_command(command)
Regarding this line of code, I suspect that the SSH server is not properly configured to allow commands to be executed in this way (this is the equivalent of ssh myserver show hwid, rather than typing it into the terminal after login).
You might want to imitate the behaviour of typing the command in after logging into the server, and for that I think this is appropriate:
shell = ssh_client.invoke_shell()
stdin, stdout, stderr = shell.exec_command(command)
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?
This solution requires me to run Python on the same machine as the process I am trying to terminate.
However, I'm running Python locally and have the process running over SSH in Terminal. How do I send the terminate command in this situation?
SSH using pexpect after setting up ssh-keygen with the server so that it doesn't require a password from your machine:
import pexpect
ssh_command = 'ssh user#your.server.com'
child = pexpect.spawn(ssh_command)
default_prompt = 'user#server:~/# '
child.expect(default_prompt)
kill_command = 'killall process_name'
child.sendline(kill_command)
If you don't use ssh-keygen, you will need to work your password login into the pexpect script before the default_prompt line.
Just attach this script to a hotkey (e.g. ctrl+alt+c) using Alfred.
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.