此文章轉載於https://www.cnblogs.com/breezey/p/6663546.html,若有侵權,請聯繫。python
>>> 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()