def getStorage(url): parseUrl = urlparse(url) protocolName = parseUrl.scheme # Remove the / parseUrl.path always starts with backupPath = parseUrl.path[1:] login = parseUrl.username password = parseUrl.password server = parseUrl.hostname if protocolName == 'file': from file import File storage = File(backupPath) elif protocolName == 'ssh': from ssh import Ssh print('Sauvegarde ssh dans : ' + backupPath) (login, password) = getIds(login, password, server) storage = Ssh(backupPath, login, password, server) elif protocolName == 'imap' or protocolName == 'imaps': from imaps import Imaps (login, password) = getIds(login, password, server) if backupPath == '': storage = Imaps(login, password, server) else: storage = Imaps(login, password, server, box=backupPath) elif protocolName == 'http' or protocolName == 'https': from webdav import Webdav (login, password) = getIds(login, password, server) storage = Webdav('', urljoin(protocolName+'://'+server, backupPath), login, password) return storage
def main(): """ Runs the program """ config = get_config() scanners = [] if config['enabled']['openports']: scanners.append(OpenPorts(config)) if config['enabled']['root']: scanners.append(Root(config)) if config['enabled']['ssh']: scanners.append(Ssh(config)) if config['enabled']['umask']: scanners.append(Umask(config)) if config['enabled']['update']: scanners.append(Update(config)) if config['enabled']['worldwritable']: scanners.append(WorldWritable(config)) for scanner in scanners: print('-' * 79) print('Running:', scanner.__class__.__name__) result = scanner.scan() print('Status:', result[0]) print('Message:', result[1]) print()
def check_boxes(box_ids, boxes_tobe=[1] * 8): boxes_are_online = [] for box_id in box_ids: ip = '192.168.' + box_ips[box_id][0] ssh = Ssh() try: ssh.login(ip) time.sleep(1) print("{}: current box: {} is online.".format( nowtimefmt(), box_ips[box_id][1])) boxes_are_online.append(1) except Exception as e: if "Unable to connect" or "连接尝试失败" in e: print("{}: current box: {} is offline.".format( nowtimefmt(), box_ips[box_id][1])) else: print("{}: 未知错误 {}".format(nowtimefmt(), e)) boxes_are_online.append(0) finally: try: ssh.ssh.close() except Exception: pass if len(boxes_are_online): return boxes_are_online == boxes_tobe
def __init__(self, remote_address: str, remote_username: str, remote_port: int, remote_path: str, file_name: str): super().__init__(name=self.__class__.__name__) self.__remote_path = remote_path self.__file_name = file_name self.__ssh = Ssh(host=remote_address, port=remote_port, user=remote_username)
def __init__(self, args): self.command = args.command self.server_name = args.server_name self.git = Git() self.error = Error() if self.server_name != '': self.conf = self._load_conf() self.ssh = Ssh(self.conf.ssh_infos)
def create_protocol(): protocol = os.environ.get("protocol") logger.info("Using protocol: %s" % protocol) if protocol.lower() == "ftp": return Ftp() elif protocol.lower() == "ssh": return Ssh() elif protocol.lower() == "json": return Json() else: raise Exception("Unknown protocol: '%s'" % protocol)
def test_scan_when_ok(self): # Prepare data and mocks test_subject = Ssh(None) test_subject.check_protocol = MagicMock(return_value=True) # Run test scenario result = test_subject.scan() # Assertions test_subject.check_protocol.assert_called_once_with( '/etc/ssh/sshd_config') self.assertEqual(result[0], ScanStatus.success)
def test_check_protocol_when_ok_no_protocol(self): # Prepare data and mocks with patch( 'builtins.open', mock_open(read_data='# $OpenBSD: s\n\n# Host *\nSomeConfig') ) as mock_file: test_subject = Ssh(None) # Run test scenario result = test_subject.check_protocol('/etc/ssh/sshd_config') # Assertions mock_file.assert_called_once_with('/etc/ssh/sshd_config') self.assertTrue(result)
def __init__(self): self.config = self.read_config() self.args = self.parse_args() cmd = self.make_cmd() print(cmd) for host in self.config.get('ssh', 'hosts').split(','): ssh = Ssh(host, int(self.config.get('ssh', 'port')), self.config.get('ssh', 'login'), self.config.get('ssh', 'password')) try: grep_out = ssh.remote_cmd(cmd) for row in grep_out: print(row) except ValueError as e: print(e)
def __init__(self, remote_address: str, remote_username: str, remote_port: int, remote_path_to_scan: str, local_path_to_scan_script: str, remote_path_to_scan_script: str): self.logger = logging.getLogger("RemoteScanner") self.__remote_path_to_scan = remote_path_to_scan self.__local_path_to_scan_script = local_path_to_scan_script self.__remote_path_to_scan_script = remote_path_to_scan_script self.__ssh = Ssh(host=remote_address, port=remote_port, user=remote_username) self.__scp = Scp(host=remote_address, port=remote_port, user=remote_username) self.__first_run = True
def __init__(self): self.config = Configuration() self.launch = Init() date_from = self.launch.dates[0] date_to = self.launch.dates[1] self.db = Db() for host in self.config.hosts: self.ssh = Ssh(host, 22, self.config.login, self.config.password) logs = self.find_logs(date_from, date_to) for log in logs: strings = self.find_strings(log) batch_counter = 0 batch = [] for string in strings: parsed = self.parse_row(string) if re.match(r'(\d{4})-(\d{2})-(\d{2})', parsed[0]): batch.append(parsed) batch_counter += 1 if batch_counter >= 100: self.db.insert(batch) batch = [] batch_counter = 0
def check_box_single(box_id, box_tobe): box_is_online = None ip = '192.168.' + box_ips[box_id][0] ssh = Ssh() try: ssh.login(ip) time.sleep(1) print("{}: current box: {} is online.".format(nowtimefmt(), box_id)) box_is_online = 1 except Exception as e: if "Unable to connect" or "连接尝试失败" in e: print("{}: current box: {} is offline.".format( nowtimefmt(), box_id)) else: print("{}: {}发生未知错误 {}".format(nowtimefmt(), box_id, e)) box_is_online = 0 finally: try: ssh.ssh.close() except Exception: pass return box_is_online == box_tobe
from ssh import Ssh # Sample code to test paramiko on a real ssh device using the 'shell' channel myssh = Ssh(ip='10.205.10.120', user='******', password='******', port=10106, debug=False) myssh.connect() myssh.shell_send(['show configuration commands | grep network-emulator']) myssh.shell_read() print(myssh.output) myssh.close()
from ssh import Ssh import time ssh = Ssh(ip='127.0.0.1', user='******', password='', private_key_file='/home/cgustave/.ssh/id_rsa', port=22, debug=True) ssh.trace_open(filename="myTraceFileChannel.log") #ssh.connect() #ssh.invoke_channel() ssh.channel_send('ls -la\n') data = ssh.channel_read() print("received {}".format(data)) ssh.close()
def setUp(self): self.ssh = Ssh(ip='127.0.0.1', user='******', password='', debug=True)
from ssh import Ssh # Sample code to test paramiko on a real ssh device using the 'shell' channel myssh = Ssh(ip='10.5.0.31', user='******', password='******', port=22, debug=True) myssh.connect() myssh.shell_send(["ps -xww | grep qemu-system-x86\n"]) print(myssh.output) for line in myssh.output.splitlines(): print("\nline={}".format(line)) myssh.close()
def deploy_to_stage(self): if self.is_config_valid(self.config_json, 'stage'): print('===== Preparing deploy to STAGE Server =====') ssh = Ssh(self.get_ssh_opts('stage')) ssh.deploy()
import env from ssh import Ssh ssh = Ssh() ssh_cylc = Ssh()
def deploy_to_dev(self): if self.is_config_valid(self.config_json, 'dev'): print('===== Preparing deploy to DEV Server =====') ssh = Ssh(self.get_ssh_opts('dev')) ssh.deploy()
def __init__(self): self.__connect = Ssh(SSH_IP, SSH_USERNAME, SSH_PASSWORD) raw_response = self.__connect.sendCommand("config paging disable\n")
def deploy_to_prod(self): if self.is_config_valid(self.config_json, 'prod'): print('===== Preparing deploy to PROD Server =====') ssh = Ssh(self.get_ssh_opts('prod')) ssh.deploy()
from ssh import Ssh myssh = Ssh(ip='127.0.0.1', user='******', password='', debug=True) myssh.trace_open(filename="test4File.log") myssh.mock(context='default') myssh.connect() myssh.execute(["ps -ef"]) myssh.shell_read() myssh.close()