使用paramiko庫和scp庫結合
import paramiko
from scp import scpclient
class
linuxsshscp
(object):
def__init__
(self, ip, username, password, port=22)
: self.ip = ip
self.username = username
self.password = password
self.port = port
self.ssh_client =
none
self.scp_client =
none
self.sftp_client =
none
defssh_connect
(self)
:print
("ssh connect"
)if self.ssh_client is
none
: self.ssh_client = paramiko.sshclient(
)# set_missing_host_key_policy(paramiko.autoaddpolicy()) 允許連線不在know_hosts檔案中的主機
self.ssh_client.set_missing_host_key_policy(paramiko.autoaddpolicy())
self.ssh_client.connect(self.ip, self.port, self.username, self.password, timeout=10)
defssh_disconnect
(self)
:print
("ssh disconnect"
) self.ssh_client.close(
) self.ssh_client =
none
defssh_exec_command
(self, command)
:print
("exec command on remote"
) std_in, std_out, std_err = self.ssh_client.exec_command(command)
result = std_out.read(
).decode(
)return result
defscp_connect
(self)
:print
("scp connect"
)if self.ssh_client is
none
:print
("ssh not connect, start connect ssh"
) self.ssh_connect(
)if self.scp_client is
none
: self.scp_client = scpclient(self.ssh_client.get_transport(
), socket_timeout=
15.0
)def
scp_disconnect
(self)
:print
("scp disconnect"
) self.scp_client.close(
) self.scp_client =
none
defscp_copy_from_remote
(self, local_path, remote_path)
:print
("start copy remote to local"
) self.scp_client.get(remote_path, local_path,
true
)def
scp_copy_to_remote
(self, local_path, remote_path)
:print
("start copy local to remote"
) self.scp_client.put(local_path, remote_path,
true
)def
sftp_connect
(self)
:print
("sftp connect"
)if self.ssh_client is
none
: self.ssh_connect(
) self.sftp_client = self.ssh_client.open_sftp(
)def
sftp_copy_from_remote
(self, local_path, remote_path)
:print
("sftp copy from remote"
) self.sftp_client.get(remote_path, local_path)
defsftp_copy_to_remote
(self, local_path, remote_path)
:print
("sftp copy to remote"
) self.sftp_client.put(local_path, remote_path)
defsftp_disconnect
(self)
:print
("sftp connect"
) self.sftp_client.close(
) self.sftp_client =
none
if __name__ ==
"__main__"
:# 使用scp遠端拷貝 拷貝整個目錄只要提供路徑就可以
# s = linuxsshscp("ip", "tester", "password", 22)
# s.ssh_connect()
# s.scp_connect()
# print(s.ssh_exec_command("ls /home/tester/ |grep test"))
# print(s.ssh_exec_command("touch /home/tester/scp_test.text"))
# s.scp_copy_from_remote("/home/test/demo/ssh_demo/", "/home/tester/scp_test.text")
# print(s.ssh_exec_command("ls /home/tester/ |grep test"))
# s.scp_copy_to_remote("/home/test/demo/ssh_demo/tester_password", "/home/tester/")
# print(s.ssh_exec_command("ls /home/tester/ |grep test"))
# s.scp_disconnect()
# s.ssh_disconnect()
# 使用sftp遠端拷貝 拷貝整個目錄需要先獲取目錄下所有的檔案名字,然後依次拷貝
s = linuxsshscp(
"ip"
,"tester"
,"password",22
) s.ssh_connect(
) s.sftp_connect(
)print
(s.ssh_exec_command(
"ls /home/tester/ |grep test"))
print
(s.ssh_exec_command(
"touch /home/tester/scp_test.txt"))
s.sftp_copy_from_remote(
"/home/test/demo/ssh_demo/scp_test.txt"
,"/home/tester/scp_test.txt"
)print
(s.ssh_exec_command(
"ls /home/tester/ |grep test"))
s.sftp_copy_to_remote(
"/home/test/demo/ssh_demo/tester_password"
,"/home/tester/tester_password"
)print
(s.ssh_exec_command(
"ls /home/tester/ |grep test"))
s.sftp_disconnect(
) s.ssh_disconnect(
)
python 遠端執行命令
1.簡單版 coding utf 8 import paramiko import refrom time import sleep defssh ssh paramiko.sshclient ssh.set missing host key policy paramiko.autoaddpolic...
linux遠端拷貝和本地拷貝命令
歡迎持續關注閱讀,一起學習,共同交流 477819525君羊 linux遠端拷貝和本地拷貝命令 一 linux對linux 遠端拷貝 scp命令 scp 檔名 root 遠端ip 路徑 將本地home目錄下的test.tar的檔案拷貝到遠端主機192.168.1.23的 home adm 目錄下,則...
linux遠端拷貝和本地拷貝命令
linux遠端拷貝和本地拷貝命令 一 linux對linux 遠端拷貝 scp命令 scp 檔名 root 遠端ip 路徑 將本地home目錄下的test.tar的檔案拷貝到遠端主機192.168.1.23的 home adm 目錄下,則命令為 scp home test.tar root 192....