Enable FTP at the windows Firewall


netsh advfirewall firewall add rule name="FTP (non-SSL)" action=allow protocol=TCP dir=in localport=21

netsh advfirewall set global StatefulFtp enable

Extra enable ping:

netsh advFirewall Firewall add rule name="PING IPv4" protocol=icmpv4:8,any dir=in action=allow

scapy example under windows

I had to use windows at work. The version 2.4.2 doesn’t work well so I installed the 2.4.0
The interface names under windows are not as pretty as under Linux.

pip install scapy==2.4.0

import sys
from scapy.all import srp,Ether,ARP,conf
from scapy.arch.windows import show_interfaces

if len(sys.argv) != 2:
  print("Usage example: pingarp 192.168.1.0/24")
  show_interfaces()
sys.exit(1)

myinterface="Intel(R) Ethernet Connection (4) I219-V"
conf.verb=0
ans,unans=srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=sys.argv[1]),
iface=myinterface, timeout=2)

print( r"MAC,IP")
for snd,rcv in ans:
  print(rcv.sprintf(r"%Ether.src%,%ARP.psrc%"))

EWON to Excell vbscript and opentext

I made an script to convert TXT/CSV files generated by EWON to a excell xlsx file to avoid configuring the format each time. The function opentext does work but it doesn’t work according to the documentation and I was lucky I didn’t have to specify the decimal and thousand marker because I still don’t know how to get the rest of the parameters after FieldInfo accepted.

https://github.com/ideechaniz/ewon2excell

Documentation:
https://docs.microsoft.com/en-us/office/vba/api/excel.workbooks.opentext
https://docs.microsoft.com/en-us/office/vba/api/excel.xlcolumndatatype


expression. OpenText( _Filename_ , _Origin_ , _StartRow_ , _DataType_ ,
'_TextQualifier_ , _ConsecutiveDelimiter_ , _Tab_ , _Semicolon_ , _Comma_ , _Space_ ,
_Other_ , _OtherChar_ , _FieldInfo_ , _TextVisualLayout_ ,
_DecimalSeparator_ , _ThousandsSeparator_ , _TrailingMinusNumbers_ , _Local_ )

FTP upload using python

I did several scripts before to upload files to a server but they don’t give any errors if something has happened and I need to upload the files if there is a network connection and delete the local files once the upload is done so I made a python script.

 

#
#  Sube ficheros con un nombre determinado por FTP a un server y luego los borra localmente
#  Si no consigue subir el fichero, luego no lo borra.
#

import os
import sys
import ftplib

# Configuracion para subir ficheros a DEXMA
SERVER="192."
USER="iker"
PASSWORD=""
ORIGEN="C:\\Users\\Iker\\Desktop\\borrame" # Directorio origen, recuerda usar dos \\ para \
NOMBRE="MARTUTENE" # Palabra clave en el nombre de los ficheros

def uploadFTP(fichero, dirDestino, server, username, password):
  ftp = ftplib.FTP(server, username, password)
  ftp.cwd(dirDestino)
  print("%s Subiendo" % fichero)
  fh = open(fichero, 'rb')
  
  try:
    ftp.storbinary('STOR %s' % fichero, fh)
  except ftplib.all_errors:
    fh.close()
    ftp.quit()
    return "Error"    
  else:
    fh.close()
    ftp.quit()
    return "Ok"


def main():
  print("Cambiando a directorio %s" % ORIGEN)
  os.chdir(ORIGEN)

  dir = os.listdir()
  for fichero in dir:
    if NOMBRE in fichero:
      if os.path.isfile(fichero):
        print("%s Encontrado" % fichero)
        
        if uploadFTP(fichero,"/",SERVER, USER, PASSWORD) == "Ok":
          print("%s Subido correctamente" % fichero)
          os.remove(fichero)
          print("%s Borrado localmente" % fichero)
        else:
          print("Error, %s no se ha subido" % fichero)

 
if __name__== "__main__":
  main()

BUG python pandas to_csv escaping quotes

The function to_csv creates triple quotes “”” if it finds a single quote ”

I tried to use the option quoting=None but it doesn’t work so I have to use another character.

Example:

doscolumnas.csv

col1;col2
A;1
B;2
C;3

cols.py

import sys
import pandas as pd # pip install pandas

def main():

f=pd.read_csv("doscolumnas.csv",sep=';')
df=pd.DataFrame(f)
df.col2='\"'+df.col2.astype(str)+'\"'
print(df.col2.astype(str))

#WTF! quoting=None does nothing
f.to_csv("otro.csv",sep=';', index=False, quoting=None, quotechar="'")

if __name__== "__main__":
main()

PD: Just to remember, this is the way to use spaces and different encoding at the column names:

  f=pd.read_csv("doscolumnas.csv",sep=';',encoding="ISO-8859-1")
  df=pd.DataFrame(f)
  df['Dirección IP de pasarela']=':'+df['Dirección IP de pasarela'].astype(str)
  print(df['Dirección IP de pasarela'])
  

winscp scripting for ftp

Some servers ban some ftp clients but allow winscp and filezilla. Filezilla can not be scripted but winscp can.

winscp.exe /console

this way you can test the commands.

open ftp://user:pass@ftp.dexcell.com
lcd C:\Users\x230\Desktop\Origen
cd historic
put MARTUTENE*
exit

When you want to execute the script

WinSCP.com /script=”c:\Users\x230\Desktop\ftptarea\winscp.txt”