Package: scrippy-remote
scrippy_remoteSSH/SFTP/FTP client for theScrippyframework.PrerequisitesPython modulesList of necessary modulesThe modules listed below will be automatically installed.paramikoInstallationManualgitclonehttps://codeberg.org/scrippy/scrippy-remote.gitcdscrippy-remote
python-mpipinstall-rrequirements.txt
makeinstallWithpippipinstallscrippy-remoteUsagescrippy_remoteThis module offers all the objects, methods, and functions for operations on remote hosts accessible viaSSH/SFTPorFTPand a limited support ofCIFS:Execution of commands on a remote hostCopying directories/files to a remote hostDeleting directories/files on a remote hostCopying files between remote hosts (with the local machine acting as a buffer)...Thescrippy_remote.remotemodule provides several objects for transferring files via SFTP, FTP(s), or CIFS, and for remote command execution via SSH.The source code for thescrippy_remote.remotemodule and its sub-modules is also extensively commented and remains the best source of documentation.SSH/SFTPExecute a command on a remote host:importloggingfromscrippy_remoteimportremoteremote_host="srv.flying.circus"remote_port=22remote_user="luigi.vercotti"key_filename="/home/luigi.vercotti/.ssh/id_rsa"password="dead_parrot"withremote.Ssh(username=remote_user,hostname=remote_host,port=remote_port,key_filename=key_filename,password=password)ashost:stdout=host.exec_command("ls /home/luigi.vercotti",return_stdout=True)ifstdout["exit_code"]==0:forlineinstdout["stdout"]:logging.debug(line)Retrieve a remote file:importloggingfromscrippy_remote.remoteimportSshfromscrippy_remoteimportScrippyRemoteErrorremote_host="srv.flying.circus"remote_port=22remote_user="luigi.vercotti"remote_path="/home/luigi.vercotti/piranha_brothers_files"key_filename="/home/luigi.vercotti/.ssh/id_rsa"password="dead_parrot"local_path="/home/harry.fink"pattern=".*"recursive=Truedelete=Falseexit_on_error=Truewithremote.Ssh(username=remote_user,hostname=remote_host,port=remote_port,key_filename=key_filename,password=password)ashost:try:err=host.sftp_get(remote_path=remote_path,local_path=local_path,pattern=pattern,recursive=recursive,delete=delete,exit_on_error=exit_on_error)logging.debug("Errors:{}".format(err))exceptScrippyRemoteErrorase:logging.critical("{}".format(e))Transfer files to a remote host:fromscrippy_remote.remoteimportSshfromscrippy_remoteimportScrippyRemoteErrorremote_host="srv.flying.circus"remote_port=22remote_user="luigi.vercotti"remote_path="/home/luigi.vercotti"key_filename="/home/luigi.vercotti/.ssh/id_rsa"password="dead_parrot"local_path="/home/harry.fink"pattern=".*"recursive=Truedelete=Trueexit_on_error=TruewithSsh(username=remote_user,hostname=remote_host,port=remote_port,key_filename=key_filename)ashost:try:err=host.sftp_put(local_path=local_path,remote_path=remote_path,pattern=pattern,recursive=recursive,delete=delete,exit_on_error=exit_on_error)logging.debug("Errors:{}".format(err))exceptScrippyRemoteErrorase:logging.critical("{}".format(e))FTPSending a fileremote_host="srv.flying.circus"remote_port=21remote_user="luiggi.vercotti"local_file="/home/luiggi.vercotti/parrot.txt"remote_dir="dead/parrot"password="d34dp4rr0t"ftp_tls=Trueftp_explicit_tls=Trueftp_ssl_verify=False# If `ftp_create_dir` is set to `True`, the local tree will be recreated on the remote hostftp_create_dir=True# Copy the local file "/home/luiggi.vercotti/parrot.txt" to the remote directory "dead/parrot/home/luiggi.vercotti"withFtp(username=remote_user,hostname=remote_host,port=remote_port,password=password,tls=ftp_tls,explicit=ftp_explicit_tls,ssl_verify=ftp_ssl_verify)ashost:host.put_file(local_file=local_file,remote_dir=remote_dir,create_dir=ftp_create_dir)Listing files in a remote directoryremote_host="srv.flying.circus"remote_port=21remote_user="luiggi.vercotti"remote_dir="dead/parrot"password="d34dp4rr0t"# Pattern is a regular expressionpattern=".*\.txt"ftp_tls=Trueftp_explicit_tls=Trueftp_ssl_verify=False# List all *.txt files in the remote directory "dead/parrot"withFtp(username=remote_user,hostname=remote_host,port=remote_port,password=password,tls=ftp_tls,explicit=ftp_explicit_tls,ssl_verify=ftp_ssl_verify)ashost:files=host.list(remote_dir=remote_dir,pattern=pattern)Retrieving a remote fileremote_host="srv.flying.circus"remote_port=21remote_user="luiggi.vercotti"remote_dir="dead/parrot"password="d34dp4rr0t"remote_file="parrot.txt"local_dir="/home/luiggi.vercotti"# If `ftp_create_dir` is set to `True`, the remote tree will be recreated on the local hostftp_create_dir=Trueftp_tls=Trueftp_explicit_tls=Trueftp_ssl_verify=FalsewithFtp(username=remote_user,hostname=remote_host,port=remote_port,password=password,tls=ftp_tls,explicit=ftp_explicit_tls,ssl_verify=ftp_ssl_verify)ashost:remote_file=os.path.join(remote_dir,remote_file)host.get_file(remote_file=remote_file,local_dir=local_dir,create_dir=ftp_create_dir)Deleting a remote fileremote_host="srv.flying.circus"remote_port=21remote_user="luiggi.vercotti"remote_dir="dead/parrot"password="d34dp4rr0t"remote_file="parrot.txt"ftp_tls=Trueftp_explicit_tls=Trueftp_ssl_verify=FalsewithFtp(username=remote_user,hostname=remote_host,port=remote_port,password=password,tls=ftp_tls,explicit=ftp_explicit_tls,ssl_verify=ftp_ssl_verify)ashost:remote_file=os.path.join(remote_dir,remote_file)host.delete_remote_file(remote_file)Deleting a remote directoryThe directory will only be deleted if it is empty.remote_host="srv.flying.circus"remote_port=21remote_user="luiggi.vercotti"remote_dir="dead/parrot"password="d34dp4rr0t"ftp_tls=Trueftp_explicit_tls=Trueftp_ssl_verify=FalsewithFtp(username=remote_user,hostname=remote_host,port=remote_port,password=password,tls=ftp_tls,explicit=ftp_explicit_tls,ssl_verify=ftp_ssl_verify)ashost:host.delete_remote_dir(remote_dir)CIFSUsage example:withCifs(hostname='SRV2GNC.gnc.recif.nc',shared_folder='BackupConfluence',username='svc.conf-bkp',password='MonSuperMotDePasse')ascifs:cifs.create_directory('myfolder')cifs.put_file(local_filepath='./mlocal-file.txt',remote_filepath='myfolder/remote-file.txt')cifs.get_file(remote_filepath='myfolder/remote-file.txt',local_filepath='./copy.txt')withcifs.open_for_write('myfolder/new-remote-file.txt')asfile:file.write(b'Hello from cifs.open_for_write')withcifs.open_for_read('myfolder/new-remote-file.txt')asfile:print(file.readlines())cifs.delete_directory_content('myfolder')