def sshclient_execmd(self, execmd, ip, dev_type): com = '' cm = [] for command in execmd.splitlines(): cm += [command.strip()] com = ';'.join(cm) Log.success(com) stdin, stdout, stderr = self.ssh.exec_command(com.encode('ascii'), get_pty='$ ') print(stdout.read().decode(encoding='utf-8')) # add_device = '/home/caxxx/zhulin/create_dashboard/sean.sh {} "{}"'.format(ip, device_type) add_device = 'cd /home/caxxx/zhulin/create_dashboard/\npwd\n./create_dashboard.sh {} "{}"'.format( ip, dev_type) print(add_device) stdin, stdout, stderr = self.ssh.exec_command( add_device.encode('ascii'), get_pty=True) sleep(2) result = stdout.read().decode(encoding='utf-8') Log.info(result) if 'success' in result: Log.warning('device add successfully.........') elif 'exist' in result: Log.warning('device alread exists........') else: Log.warning('device add failed...........')
def sshclient_execmd(self, execmd): com = '' cm = [] for command in execmd.splitlines(): cm += [command.strip()] com = ';'.join(cm) Log.success(com) stdin, stdout, stderr = self.ssh.exec_command(com.encode('ascii'), get_pty=True) # stdin.write("Y") # Generally speaking, the first connection, need a simple interaction. print(stdout.read().decode(encoding='utf-8'))
def _login(self): try: self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh.connect(hostname=self.hostname, port=self.port, username=self.username, password=self.password, allow_agent=True) Log.info("self.ssh session login complete (sid=%s,host=%s)" % (self.hostname, self.port)) except Exception: raise Exception("sid=%s, host=%s" % (self.hostname, self.port)) finally: pass return self.ssh
def main(device_ip): hostname = '10.245.251.56' port = 22 sshtype = ['CLI', 'LINUX'] sshtype = sshtype[1] username = '' password = '' filename = '' if sshtype == 'CLI': username = '******' password = '******' filename = 'cmd.txt' elif sshtype == 'LINUX': username = '******' password = '******' filename = 'shell.txt' sh = Sshlib(hostname, port, username, password, sshtype='LINUX') execmd = sh.withopen(filename) sh.login(sshtype) Log.context('execmd: {}'.format(execmd) + '\r') sh.sshclient_execmd(execmd, device_ip, dev_type='E7')
def main(): hostname = '10.245.46.208' port = 22 sshtype = ['CLI', 'LINUX'] sshtype = sshtype[1] username = '' password = '' filename = '' if sshtype == 'CLI': username = '******' password = '******' filename = 'cmd.txt' elif sshtype == 'LINUX': username = '******' password = '******' filename = 'shell.txt' sh = Sshlib(hostname, port, username, password, sshtype='LINUX') execmd = sh.withopen(filename) sh.login(sshtype) Log.context('execmd: {}'.format(execmd) + '\r') sh.sshclient_execmd(execmd)
def _login(self): """cafe code good sample 设置连接的远程主机没有本地主机密钥或HostKeys对象时的策略,目前支持三种: AutoAddPolicy 自动添加主机名及主机密钥到本地HostKeys对象,不依赖load_system_host_key的配置。 即新建立ssh连接时不需要再输入yes或no进行确认 WarningPolicy 用于记录一个未知的主机密钥的python警告。并接受,功能上和AutoAddPolicy类似,但是会提示是新连接 RejectPolicy 自动拒绝未知的主机名和密钥,依赖load_system_host_key的配置。此为默认选项 """ try: self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.ssh.connect(hostname=self.hostname, port=self.port, username=self.username, password=self.password, allow_agent=True) Log.info("self.ssh session login complete (sid=%s,host=%s)" % (self.hostname, self.port)) except Exception: raise Exception("sid=%s, host=%s" % (self.hostname, self.port)) finally: pass return self.ssh
def wrapper(*args, **kw): Log.warning('%s %s():' % (text, func.__name__)) return func(*args, **kw)