Code:Select all
# < SSHtransfer > function of forwarding the SSH client script to the SSH server # by Sertik 19/01/2023 # use SSH and ftp # to work, you need to configure the local variables parameters for ftp: fport 21, fuser and fpassword (see below) # the length of the portable script is limited to 4096 bytes # # $1 - address SSH-server # $2 - port SSH service # $3 - userName SSH-server # $4 - name of the source script # $5 - dst script name # $6 - "run", if you need to execute the transmitted script immediately # examples usage: # :put [$SSHtransfer 192.168.1.2 22 admin sourceScript DstScript] # :put [$SSHtransfer 192.168.1.2 22 admin sourceScript DstScript run] :global SSHtransfer do={ # fetch ftp transfer parametrs: # :local fport 21 # :local fuser admin # :local fpassword "user`s password" :if ([:len $1]=0) do={:return "Error IP SSH-Server"} do {:set $1 [:toip $1]} on-error={:return "Error syntax address IP SSH-Server"} :if (!any $2) do={:return "Error port"} do {:set $2 [:tonum $2]} on-error={:return "Error syntax port"} :if ([:len $3]=0) do={:return "Error username"} :if ([:len $4]=0) do={:return "Error Source Script name not set"} :if ([:len $5]=0) do={:return "Error Dst Script name on SSH-server not set"} :if (([:len $6]>0) && ($6!="run")) do={:return "Error parametr \$6 $6"} :if ([:len [/system script find name=$4]]=0) do={:return "Error Source Script $4 no find in repository router SSH-client"} :if ([:len [/system script get $4 source]]>4096) do={:return "Error long source script"} :global fileContent :if ([:len [/file find name="transferSSH.txt"]]>0) do={[/file remove transferSSH.txt]} :delay 1s /file print file=transferSSH.txt; do { :set fileContent [/system script get $4 source] :delay 3s :execute script=":put [/system script get \"$4\" source]" file="transferSSH.txt" } on-error={ [/file remove transferSSH.txt] :set fileContent :return "Error script to long"} :delay 3s do { /tool fetch address=$1 port=$fport src-path=transferSSH.txt user=$fuser mode=ftp password=$fpassword dst-path=tSSH.rsc upload=yes } on-error={:set fileContent; [/file remove transferSSH.txt]; :return "Error ftp transfer script on SSH-server"} :delay 3s :set fileContent [/file remove transferSSH.txt] :if ($6!="run") do={ :do { [/system ssh-exec address=$1 port=$2 user=$3 command=":if ([:len [/system script find name=\"$5\"]]>0) do={/system script remove \"$5\"}; \ /system script add name=\"$5\" source=[/file get tSSH.rsc contents]; :delay 2s; /file remove tSSH.rsc"]; \ } on-error={:return "Error add script $5 in SSH-server repository"} } else={\ :do { [/system ssh-exec address=$1 port=$2 user=$3 command=":if ([:len [/system script find name=\"$5\"]]>0) do={/system script remove \"$5\"}; \ /system script add name=\"$5\" source=[/file get tSSH.rsc contents]; :delay 3s; /file remove tSSH.rsc; \ :put RUN;\ /system script run \"$5\""] } on-error={:return "Error add script $5 in SSH-server repository or run"} :set $6 ("and $6") } :return "Done: Script $5 add to SSH-server repository $6" }
Code:Select all
:local Answer [$SSHtransfer 192.168.1.2 22 admin sourceScript DstScript] :if ([:len [:find $Answer "Error"]]!=0) do={:log error $Answer} else={:log warning $Answer} :local Answer [$SSHtransfer 192.168.1.2 22 admin sourceScript DstScript run] :if ([:len [:find $Answer "Error"]]!=0) do={:log error $Answer} else={:log warning $Answer}
I will be glad to comments and criticism. I hope someone will suggest a better and more rational option