-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
41 lines (36 loc) · 1.37 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import paramiko
class Bot:
def __init__(self, host, user, password, port):
self.host = host
self.user = user
self.password = password
self.port = port
self.session = self.connect()
def connect(self):
try:
bot = paramiko.SSHClient()
bot.set_missing_host_key_policy(paramiko.AutoAddPolicy())
bot.connect(hostname=self.host, port=self.port, username=self.user, password=self.password)
return bot
except paramiko.ssh_exception.AuthenticationException:
print(f'[*] Authentication Failed IP:{self.host} USER:{self.user} PASS={self.password}')
return
except paramiko.SSHException as e:
print(f'[*] Device Failed Executing the Command IP:{self.host} USER:{self.user} PASS={self.password}')
return e
except Exception as e:
return e
def send_command(self, cmd):
try:
stdin, stdout, stderr = self.session.exec_command(cmd)
if 'nohup' in cmd:
return ''
return stdout.read().decode()
except Exception as e:
return e
def upload_file(self, localpath, remotepath):
self.sftp = self.session.open_sftp()
try:
self.sftp.put(localpath, remotepath)
except Exception as e:
return e