使用paramiko

时间:2016-10-24 17:09:25

标签: python ssh paramiko

我编写的代码将登录到远程服务器并执行低于2的命令

  

cd /文件夹路径/
  rm -rf / myfolder / view1

class ssh:
client = None

def __init__(self, address, username, password):
    print("Connecting to server.")
    self.client = client.SSHClient()
    self.client.set_missing_host_key_policy(client.AutoAddPolicy())
    self.client.connect(address, username=username, password=password, look_for_keys=False)

def sendCommand(self, command):
    if(self.client):
        stdin, stdout, stderr = self.client.exec_command(command)
        while not stdout.channel.exit_status_ready():
            # Print data when available
            if stdout.channel.recv_ready():
                alldata = stdout.channel.recv(1024)
                prevdata = b"1"
                while prevdata:
                    prevdata = stdout.channel.recv(1024)
                    alldata += prevdata

                print(str(alldata, "utf8"))
    else:
        print("Connection not opened.")
  

connection = ssh(ipaddress,username,password)
  connection.sendCommand(" cd / path to folder /")
  connection.sendCommand(" rm -rf / myfolder / view1")

执行此操作后,不会删除文件夹。请帮忙

0 个答案:

没有答案