Skip to content

Instantly share code, notes, and snippets.

@RPHAELLA
Last active January 7, 2021 09:08
Show Gist options
  • Save RPHAELLA/938ecdf93e10e79231af0ba1702acf99 to your computer and use it in GitHub Desktop.
Save RPHAELLA/938ecdf93e10e79231af0ba1702acf99 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# Multiple Sequential Upload of a Single File for
# SSH File Transfer Protocol
# Copyright (C) 2017
#
# This program can be redistributed and/or modified under the terms of the
# GNU General Public License, either version 3 of the License, or (at your
# option) any later version.
#
from tqdm import tqdm
import paramiko, sys, os, socket, time
global line, host, username, key, input_file
line = "\n-------------------------------------------------------------------------------\n"
def progress_bar(*args, **kwargs):
pbar = tqdm(*args, **kwargs) #transferred, toBeTransferred)
last = [0]
def bar(transferred, toBeTransferred):
pbar.total = int(toBeTransferred)
pbar.update(int(transferred - last[0]))
last[0] = transferred
return bar, pbar
def sftp_connect(code=0):
sftp_key = paramiko.RSAKey.from_private_key_file(key, password='P@ssw0rd')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print "[*] Connecting to " + host
try:
ssh.connect(host, username=username, pkey=sftp_key)
sftp = ssh.open_sftp()
sftp.sshclient = ssh
return sftp
except paramiko.AuthenticationException:
print("[!] Unable to Login - User: %s" % (username))
ssh.clsoe()
except socket.error, e:
print("[*] Connection Could Not Be Established To Address: %s " % (host))
ssh.close()
return
try:
host = raw_input("[*] Enter Target Host Address: ")
username = raw_input("[*] Enter SFTP Username: ")
key = raw_input("[*] Enter Private Key: ")
if os.path.exists(key) == False:
print "\n[*] File Path Does Not Exist"
sys.exit(4)
file = raw_input("[*] Enter File to Upload: ")
if os.path.exists(file) == False:
print "\n[*] File Path Does Not Exist"
sys.exit(4)
number_of_upload = int(raw_input("[*] Enter Number of Upload to Send: "))
try:
response = sftp_connect()
response.chdir('/ES')
directory_list = ", ".join(response.listdir('.'))
print("[*] Connection Established - User: %s\n[*] Directory: %s" % (username, directory_list))
for i in range(0, number_of_upload):
print("[*] Uploading: %s" % (file+str(i)))
bar, pbar = progress_bar(unit='B', unit_scale=True)
response.put(file, file+str(i), callback=bar)
pbar.close()
print("[*] File Uploaded: %s" % (file+str(i)))
directory_list = ", ".join(response.listdir('.'))
print("[*] Directory: %s" % (directory_list))
except Exception, e:
print e
remove = False
while(remove == False):
user_input = raw_input("[*] Do You Want To Remove All Files? [Y/N]:")
remove = True if user_input == "Y" else False
if remove:
for i in range(0, number_of_upload):
try:
response.remove(file+str(i))
print("[*] Removed %s" % (file+str(i)))
except Exception, e:
print e
pass
directory_list = ", ".join(response.listdir('.'))
print("[*] Directory: %s" % (directory_list))
print("[*] Connection Closed for User: %s" % (username))
except KeyboardInterrupt:
print "\n\n[*] User Requested An Interrupt"
sys.exit(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment