Skip to content

Instantly share code, notes, and snippets.

@jmahmood
Created December 1, 2011 08:02
Show Gist options
  • Save jmahmood/1414828 to your computer and use it in GitHub Desktop.
Save jmahmood/1414828 to your computer and use it in GitHub Desktop.
How to use Paramiko to get rid of files from your rssh /chrootjail/lib path that don't have to be there.
#!/usr/bin/env python
# Moves files in directory, then checks to see if SCP works.
# If SCP fails, it moves the files back.
# This assumes you have a functional chroot jail configured with rssh, but you want to get rid of all files that are unnecessary.
# This works as follows
# 1. Script moves file from /chrootroot/lib/ to a temporary directory
# 2. Script attempts to connect to with a sftp connection using a restricted user
# 3. If user can connect and upload the file he wants to, the file can be discarded
# ELSE, the file is moved back.
# Finally; you have the absolute minimum number of files you need for sftp.
# Trying to find a similar scp test as well.
import os
import paramiko
path = "/chrootjail/lib/"
temp_path = "/home/jawaad/temp2/"
temp_file = "/home/jawaad/temp/lol.txt" # Used to test transfer.
def sftp_upload(local_directory, remote_directory):
try:
host = "www.example.com"
port = 22
transport = paramiko.Transport((host, port))
username = "rsshuser" #hard-coded, feel free to do something else.
password = "genericpasswordlol" #hard-coded
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put(local_directory, remote_directory)
sftp.close()
transport.close()
return True
except:
return False
def move_file(f):
print "Moving file from: %s%s" % (path,f)
os.rename("%s%s"%(path,f),"%s%s"%(temp_path,f))
def required_for_scp():
if sftp_upload(temp_file, "/rsshuser/lol.txt"):
return False
else:
return True
def move_back(f):
print "Moving back file from: %s%s" % (temp_path,f)
os.rename("%s%s"%(temp_path,f),"%s%s"%(path,f))
filelist = os.listdir(path)
filelist = filter(lambda x: not os.path.isdir(x), filelist)
for f in filelist:
move_file(f)
if(required_for_scp()):
move_back(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment