| # Copyright 2012 Citrix Systems, Inc. Licensed under the |
| # Apache License, Version 2.0 (the "License"); you may not use this |
| # file except in compliance with the License. Citrix Systems, Inc. |
| # reserves all rights not expressly granted by the License. |
| # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| # |
| # Automatically generated by addcopyright.py at 04/02/2012 |
| import paramiko |
| import cloudstackException |
| class remoteSSHClient(object): |
| def __init__(self, host, port, user, passwd): |
| self.host = host |
| self.port = port |
| self.user = user |
| self.passwd = passwd |
| self.ssh = paramiko.SSHClient() |
| self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| try: |
| self.ssh.connect(str(host),int(port), user, passwd) |
| except paramiko.SSHException, sshex: |
| raise cloudstackException.InvalidParameterException(repr(sshex)) |
| |
| def execute(self, command): |
| stdin, stdout, stderr = self.ssh.exec_command(command) |
| output = stdout.readlines() |
| errors = stderr.readlines() |
| results = [] |
| if output is not None and len(output) == 0: |
| if errors is not None and len(errors) > 0: |
| for error in errors: |
| results.append(error.rstrip()) |
| |
| else: |
| for strOut in output: |
| results.append(strOut.rstrip()) |
| |
| return results |
| |
| |
| if __name__ == "__main__": |
| ssh = remoteSSHClient("192.168.137.2", 22, "root", "password") |
| print ssh.execute("ls -l") |
| print ssh.execute("rm x") |