Skip to content

Instantly share code, notes, and snippets.

@sergkondr
Created May 21, 2019 22:10
Show Gist options
  • Save sergkondr/5615f2d5cb57682010cd854bc2951fdb to your computer and use it in GitHub Desktop.
Save sergkondr/5615f2d5cb57682010cd854bc2951fdb to your computer and use it in GitHub Desktop.
import paramiko
import time
import warnings
warnings.filterwarnings(action='ignore', module='.*paramiko.*')
class ParamikoWraper:
def __init__(self, host, user, password, port=22):
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.client.connect(host, username=user, password=password, port=port)
self.channel = self.client.invoke_shell()
self.stdin = self.channel.makefile('wb')
self.stdout = self.channel.makefile('r')
def __del__(self):
self.client.close()
def run(self, command):
command = command.strip('\n')
self.stdin.write(command + '\n')
self.stdin.flush()
time.sleep(1)
return command, ""
suka = ParamikoWraper('192.168.33.10', "vagrant", "qwerty")
suka.run("sudo -s") # запускаем шелл
for cmd in ["echo $(whoami) >> /root/test", "echo 2 >> /root/test", "echo 3 >> /root/test"]:
print(suka.run(cmd))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment