user3682248
user3682248

Reputation:

how to run custom commands using sftp or ssh or paramiko

t I have the following code which uses ssh and sftp to ssh to a buildserver on the network and run the native mkdir and chdir commands,how do I run any custom commands with some executables i have installed locally on the build server?

import paramiko

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect('buildservername', username='yadomi', password='password')

sftp = ssh.open_sftp()
sftp.chdir('/local/mnt/workspace/newdir')
commandstring = 'repo init -u git://git.company.com/platform/manifest -b branchname --repo-url=git://git.company.com/tools/repo.git --repo-branch=caf/caf-stable'
si,so,se = ssh.exec_command(commandstring) 
readList = so.readlines()
print readList
print se

Error:-

<paramiko.ChannelFile from <paramiko.Channel 1 (closed) -> <paramiko.Transport at 0x2730310L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>>

Upvotes: 0

Views: 5074

Answers (1)

Robᵩ
Robᵩ

Reputation: 168626

1) You open an SFTP channel and set its working directory, but you never do anything else with that channel. Perhaps you believe that sets the working directory for a subsequent exec_command? It doesn't.

2) You do print se, but don't recognize the output. Perhaps you believe that prints the error stream from the invoked command? It doesn't.

Try this:

import paramiko

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect('buildservername', username='yadomi', password='password')

commandstring = 'cd /local/mnt/workspace/newdir ; repo init -u git://git.company.com/platform/manifest -b branchname --repo-url=git://git.company.com/tools/repo.git --repo-branch=caf/caf-stable'
si,so,se = ssh.exec_command(commandstring) 
readList = so.readlines()
errList = se.readlines()
print readList
print errList

The answer to your literal question, "how do I run any custom commands with some executables i have installed locally on the build server?", is "by passing the appropriate command line to ssh.exec_command()".

Upvotes: 3

Related Questions