标签:int 文件名 shell ssh2 校验 ror blog 传输 参数
>>> import paramiko >>> a = paramiko.Transport((“127.0.0.1″,2222)) >>> a.connect(username=”root”, password=’123456′) >>> sftp = paramiko.SFTPClient.from_transport(a)
>>> localpath=’ftp-test.log’ >>> remotepath=’/data/ftp-test.log’ >>> sftp.put(localpath,remotepath)
#!/usr/bin/python import paramiko import os,sys ssh_host = sys.argv[1]
ssh_port = 22 user = ‘root‘ password = ‘xxxxxx‘ cmd = sys.argv[2] paramiko.util.log_to_file(‘/tmp/test‘) #使用paramiko记录日志 s = paramiko.SSHClient() #绑定一个实例 s.load_system_host_keys() #加载known_hosts文件 s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #远程连接如果提示yes/no时,默认为yes s.connect(ssh_host,ssh_port,user,password,timeout=5) #连接远程主机 stdin,stdout,stderr = s.exec_command(cmd) #执行指令,并将命令本身及命令的执行结果赋值到标准办入、标准输出或者标准错误 cmd_result = stdout.read(),stderr.read() #取得执行的输出 for line in cmd_result: print line s.close()
pkey_file = ‘/home/breeze/.ssh/id_rsa‘ key = paramiko.RSAKey.from_private_key_file(pkey_file) s.connect(host,port,username,pkey=key,timeout=5) stdin,stdout,stderr = s.exec_command(cmd)
#!/usr/bin/python import os,sys import paramiko host = sys.argv[1] rfilename = sys.argv[2] lfilename = os.path.basename(rfilename) user = ‘root‘ password = ‘xxxx‘ paramiko.util.log_to_file(‘/tmp/test‘) t = paramiko.Transport((host,22)) t.connect(username=user,password=password) sftp = paramiko.SFTPClient.from_transport(t) sftp.get(rfilename,lfilename) #sftp.put(‘paramiko1.py‘,‘/tmp/paramiko1.py‘) t.close()
#!/usr/bin/python import socket import sys # windows does not have termios... try: import termios import tty has_termios = True except ImportError: has_termios = False def interactive_shell(chan): if has_termios: posix_shell(chan) else: windows_shell(chan) def posix_shell(chan): import select oldtty = termios.tcgetattr(sys.stdin) try: tty.setraw(sys.stdin.fileno()) tty.setcbreak(sys.stdin.fileno()) chan.settimeout(0.0) while True: r, w, e = select.select([chan, sys.stdin], [], []) if chan in r: try: x = chan.recv(1024) if len(x) == 0: print ‘\r\n*** EOF\r\n‘, break sys.stdout.write(x) sys.stdout.flush() except socket.timeout: pass if sys.stdin in r: x = sys.stdin.read(1) if len(x) == 0: break chan.send(x) finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty) # thanks to Mike Looijmans for this code def windows_shell(chan): import threading sys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n") def writeall(sock): while True: data = sock.recv(256) if not data: sys.stdout.write(‘\r\n*** EOF ***\r\n\r\n‘) sys.stdout.flush() break sys.stdout.write(data) sys.stdout.flush() writer = threading.Thread(target=writeall, args=(chan,)) writer.start() try: while True: d = sys.stdin.read(1) if not d: break chan.send(d) except EOFError: # user hit ^Z or F6 pass
#!/usr/bin/python #_*_coding:utf8_*_ import paramiko import interactive #记录日志 paramiko.util.log_to_file(‘/tmp/test‘) #建立ssh连接 ssh=paramiko.SSHClient() ssh.load_system_host_keys() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(‘192.168.128.82‘,port=22,username=‘root‘,password=‘cheyian‘) #建立交互式shell连接 channel=ssh.invoke_shell() #建立交互式管道 interactive.interactive_shell(channel) #关闭连接 channel.close() ssh.close()
标签:int 文件名 shell ssh2 校验 ror blog 传输 参数
原文地址:http://www.cnblogs.com/breezey/p/6663546.html