9
votes
def execute(self,command):
            to_exec = self.transport.open_session()
            to_exec.exec_command(command)
            print 'Command executed'
connection.execute("install.sh")

When I check the remote system, I found the script didn't run. Any clue?

3
More code, please. Not enough context. - Lior Cohen
question is too vague, which ssh wrapper are you using, what is the error, and how did you know that "the script didn't run" instead of "the script runs but with errors". - Lie Ryan
I don't know why you want to do that, but if it's for sys administration, you might find Fabric useful. - charlax
@charlax Fabric is a good suggestion. The author of Fabric, Jeff Forcier, is also the person who forked Paramiko into the ssh module. My answer addresses the specific question of how to use ssh with python, but fabric is an even better way to handle the more general problem that @kevin is likely trying solve. - aculich

3 Answers

21
votes

The code below will do what you want and you can adapt it to your execute function:

from paramiko import SSHClient
host="hostname"
user="username"
client = SSHClient()
client.load_system_host_keys()
client.connect(host, username=user)
stdin, stdout, stderr = client.exec_command('./install.sh')
print "stderr: ", stderr.readlines()
print "pwd: ", stdout.readlines()

Note, though, that commands will default to your $HOME directory, so you'll either need to have install.sh in your $PATH or (most likely) you'll need to cd to the directory that contains the install.sh script.

You can check your default path with:

stdin, stdout, stderr = client.exec_command('getconf PATH')
print "PATH: ", stdout.readlines()

However, if it is not in your path you can cd and execute the script like this:

stdin, stdout, stderr = client.exec_command('(cd /path/to/files; ./install.sh)')
print "stderr: ", stderr.readlines()
print "pwd: ", stdout.readlines()

If the script is not in your$PATH you'll need to use ./install.sh instead of install.sh, just like you would if you were on the command line.

If you are still having problems after everything above it might also be good to check the permissions of the install.sh file, too:

stdin, stdout, stderr = client.exec_command('ls -la install.sh')
print "permissions: ", stdout.readlines()
0
votes
ssh = paramiko.client.SSHClient()
ssh.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
ssh.connect(hostname=host, username=username, password = password)
chan = ssh.invoke_shell()

def run_cmd(cmd):    
    print('='*30)
    print('[CMD]', cmd)
    chan.send(cmd + '\n')
    time.sleep(2)
    buff = ''
    while chan.recv_ready():
        print('Reading buffer')
        resp = chan.recv(9999)
        buff = resp.decode()
        print(resp.decode())

        if 'password' in buff:
            time.sleep(1)
            chan.send(password + '\n')        
        time.sleep(2)

    print('Command was successful: ' + cmd)
-3
votes
subprocess.Popen('ssh thehost install.sh')

See the subprocess module.