目录
远程桌面工具:MobaXterm_Installer_v20.3.zip
百度网盘有,优点:左边是xftp,右边是命令行窗口
xshell软件中,xftp和ssh命令窗口是分开的。

- import os
-
- import paramiko
-
- trans = paramiko.Transport(sock=("192.168.10.11", 220))
-
- trans.connect(username="root", password="root")
- sftp = paramiko.SFTPClient.from_transport(trans)
-
-
- def down_from_txt(txt_path,out_dir):
- with open(txt_path,'r') as f_r:
- lines=f_r.readlines()
- for data in lines:
- path=data.split(" ")[0]
- cls_index=data.split(" ")[1].replace("\n","")
- out_to=out_dir+cls_index
- file_name=os.path.basename(path)
- os.makedirs(out_to,exist_ok=True)
- sftp.get(path, out_to+'/'+ file_name)
-
-
- def get_ssh():
- ssh = paramiko.SSHClient()
- ssh._transport = trans
- return ssh
- def down_product():
- ssh=get_ssh()
- stdin, stdout, stderr = ssh.exec_command('cd data/demo && python3 demo.py')
- print(stdout.read().decode('utf-8'))
- ssh.close()
-
- def list_dir(dir_path='/data/demo/out'):
- ssh = get_ssh()
- stdin, stdout, stderr = ssh.exec_command(f'cd {dir_path} && ls')
- print(stdout.read().decode('utf-8'))
- ssh.close()
- if __name__ == '__main__':
- list_dir()
-
- #down_from_txt(r'D:\project\data\val_clf_sampler_demo.txt',r'D:\data\good_size/')
- # 上传
- # 把本地的文件settings.py,上传到远端为/root/Desktop/settings.py
- # sftp.put("settings.py" ,"/root/Desktop/settings.py")
-
-
- sftp.close()
原文链接:https://blog.csdn.net/qq_39112101/article/details/94175083
原文还有多线程应用
原文链接:https://blog.csdn.net/weixin_45294285/article/details/122846537
- # encoding: utf-8
- import numpy as np
- import paramiko
- import time
- def creatSShConnectOb(hostname, username, password, port=22):
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- try:
- ssh.connect(hostname, port, username=username, password=password, timeout=60) # timeout protection
- return ssh
- except:
- print('Warning:\nFist connect the {} failed, now will retry!'.format(hostname))
- ssh.connect(hostname, port, username=username, password=password, timeout=60) # timeout re-try
- print('Error:\nAttempt to connect ABC failed!!! Please check the IP / port/ account / password.')
-
-
- def chanel_exe_cmd(ChanelSSHOb, cmd, t=0.1):
- ChanelSSHOb.send(cmd)
- ChanelSSHOb.send("\n")
- time.sleep(t)
- resp = ChanelSSHOb.recv(9999).decode("utf8")
- print("#"*40)
- return resp
-
-
-
- if __name__ == '__main__':
- ssh = creatSShConnectOb(hostname="192.168.110.16", username="root", password="xxx")
- chanelSSHOb = ssh.invoke_shell()
- result = chanel_exe_cmd(chanelSSHOb, "pwd")
- print(result)
- result = chanel_exe_cmd(chanelSSHOb, "cd /data")
- print(result)
- result = chanel_exe_cmd(chanelSSHOb, "pwd")
- print(result)
https://github.com/c0hb1rd/SSHTool

