Using file mask with time constraint in rm command of WinSCP - winscp

I am trying to connect to remote FTP server to delete the file older than 14 days
The script I am applying is
winscp.com /command "open ftp://username:password#xxx.xxx.xxx.xxx -explicit" "rm -filemask=<14D *.bak " "exit"
But I encounter the error :
Unknown switch 'filemask'.
I wonder what I did wrong here?
Thanks & regards,

The WinSCP rm command has no -filemask switch.
You can use the time constraint directly in the mask argument of the command.
rm *.bak<14D

Related

Copy file from remote server through lftp

I'm new to linux scripting. I want to copy file from remote server to current server(executing or client server),required cert & key files are already installed on my server(client server). below commands work when I execute it individually in sequence but, after Integrating into a .sh script it doesnt!
--My Script--
lftp -u username,xxx -p 2121 remoteServer.net;
set ssl:cert-file /abc/def/etc/User_T.p12;
set ssl:key-file abc/def/etc/User_T.p12.pwd;
lftp -e 'set net:timeout 10; get /app/home/atm/feed.txt -o /com/data/';
man lftp:
-f script_file
Execute commands in the file and exit. This option must be used
alone without other arguments (except --norc).
-c commands
Execute the given commands and exit. Commands can be separated
with a semicolon, `&&' or `||'. Remember to quote the commands
argument properly in the shell. This option must be used alone
without other arguments (except --norc).
Use "here document" feature of the shell:
lftp <<EOF
set...
open...
get...
EOF
Thanks lav for your suggestion, I found that my script was not executing second line so added continuation like
<< SCRIPT
& ended script with SCRIPT
removed all semi colon... Its working

Shell - LFTP - multiple extensions

I have been trying to find a way to use mget with only certain file extensions.
I have used following command (which works just fine if I leave *.csv)
lftp -e "set xfer:clobber true;mget $SOURCE_DIR*.{csv,txt,xls,xlsx,zip,rar};exit" -u $SOURCE_USERNAME,$SOURCE_PASSWORD $SOURCE_SERVER || exit 0
But no luck, I get message dir/*.{csv,txt,xls,xlsx,zip,rar} no files found
Tried to add parenthesis
lftp -e "set xfer:clobber true;mget $SOURCE_DIR(*.{csv,txt,xls,xlsx,zip,rar});exit" -u $SOURCE_USERNAME,$SOURCE_PASSWORD $SOURCE_SERVER || exit 0
Also no luck
$SOURCE_DIR already has a slash / at the end
I tried to test lftp locally but I have problem with opening ports on my Vagrant box, hence the question
I managed to connect to one FTP without the need to forward ports.
Turns out (I know it seems obvious) you have to specify full path with the wildcard for each extension
mget $SOURCE_DIR*.csv $SOURCE_DIR*.txt
separated by space
Also if one (or more) extensions are not found the message "*.txt no files found" will land in standard error, which led me to not be able to proceed with the full script

executing parameter file in a unix remote server

I am trying to run a script remotely using ssh and the need use some parameters from remote server. Kept all parameters in remote server location temp/test/test.prm file. Getting an error saying " invoke.sh: line 20: . /temp/test/test.prm: No such file or directory "
See below for sample script. Have very basic knowledge in scripting so plz direct me
#!/bin/sh
Param1=$1
Param2=$2
ssh usr#Server1
. ${Param1}/Client/scripts/Sample1.prm
cd $prmHome/$prmSetPath
ls | sed '/\.log$/d' > $prmHome/$prmScript/Filelist.txt
cd $prmHome/$prmScript
while read LINE
do
ExportFilName=$LINE
./conversion.sh $prmHome/tmp_export/convert_$Param2.csv $prmHome/$prmSetPath/'$ExportFilName'
done < Filelist.txt
rm -rf $prmHome/$prmScript/Filelist.txt
exit 0
Content of Sample1.prm
prmHome=/iis/home
prmSetPath=/export/set
PrmScript=/Client/scripts
I have tried the same trough command line after connecting to remote server using ssh and it is working, but when I am trying to do the same through a script (invoke.sh) its throwing no such file or directory error
UPDATE
This is unclear and will not work !
ssh usr#Server1
. ${Param1}/Client/scripts/Sample1.prm
As mentioned you should use
ssh usr#Server1 ". ${Param1}/Client/scripts/Sample1.prm"
format first.
And secondly what you expect following command to do ?
. ${Param1}/Client/scripts/Sample1.prm
Notice that there is a space in between . and the path, which is a synonym for source. So also check if the Sample1.prm have valid commands.
Looks like you are not running any ssh shell command on remote host but only on your local host.
How exactly you are running the ssh shell command ?
The code snippet in your example is poorly formatted.
The general structure should look like this e.g.
ssh user1#server1 date
or
ssh user1#server1 'df -H'
Please revise you invocation script, or make the formatting in the question appropriate.
Update:
If you want to execute your code on remote server via ssh, you can do following:
Create separate file my_script.sh for your code you want to run remotely, and paste following code:
#!/bin/bash
function my_function() {
prmHome=$1
prmSetPath=$2
PrmScript=$3
cd $prmHome/$prmSetPath
ls | sed '/\.log$/d' > $prmHome/$prmScript/Filelist.txt
cd $prmHome/$prmScript
while read LINE
do
ExportFilName=$LINE
./conversion.sh $prmHome/tmp_export/convert_$Param2.csv $prmHome/$prmSetPath/'$ExportFilName'
done < Filelist.txt
rm -rf $prmHome/$prmScript/Filelist.txt
exit 0
}
Then you can call you function remotely, by sourcing your file on remote server:
ssh usr#Server1 '. my_script.sh; my_function "/iis/home" "/export/set" "/Client/scripts"'
That's it :)
This code will not work:
ssh usr#Server1
. ${Param1}/Client/scripts/Sample1.prm
Change it like that:
ssh usr#Server1 ". ${Param1}/Client/scripts/Sample1.prm"

scp multiple remote files while entering password once

I'm trying to write a script to copy multiple files (in multiple directories) from a remote host to my local machine.
My script is (more or less) as follows:
path1="/home/db/primary/*.xml"
path2="/tmp/log_*"
copyto="/home/pathtodesktop/Desktop/temp"
mkdir $copyto
scpcommand="scp -o StrictHostKeyChecking=no root#$address:\"$path1 $path2\" $copyto"
echo $scpcommand
$scpcommand
When I run the script, I get the following output:
scp -o StrictHostKeyChecking=no root#SERVER:"/home/db/primary/*.xml /tmp/log_*" /home/pathtodesktop/Desktop/temp
sh: syntax error: unterminated quoted string
cp: cannot stat '/tmp/log_*"': No such file or directory
The output of the echo is as expected. But when I copy the output above and run the command manually in the terminal, it works as expected with no errors.
So the ultimate question is, what am I doing wrong? The command seems to work fine when run manually in the terminal. Where is my syntax error?
Thanks for your help!
Adding set -f will prevent the wildcards in your paths from being expanded locally (although you may run in to other issues with spaces/special characters).
(You can re-enable wildcards afterwards with set +f)

Why this Bash script runs on a machine but fails on another?

I have written a program which needs to access the I/O ports of my motherboard so it needs root permission. I wanted to run this program at system start up but since the Ubuntu system start up cannot run applications with root permission I wrote these two simple files:
askpass.sh
export SUDO_ASKPASS="/var/www/Bash/mits/getpass.sh";
sudo -A -E /home/mits/QtProjects/HandST/HandST // this is the address of my application
getpass.sh
echo 'P54_99**' //this is my password
as it is appear in the askpass.sh file, I used SUDO_ASKPASS as stated in sudo manual to run my application with sudo password located in anther file.
when I call askpass.sh in my laptop it works fine and the applications will start, but when I run this script in my PC server located in my office it gives the following error.(I use remote access to reach to my server )
./askpass.sh
**./askpass.sh: line 5: $'\r': command not found**
sudo: unable to run /var/www/Bash/mits/getpass.sh: Exec format error
Sorry, try again.
sudo: unable to run /var/www/Bash/mits/getpass.sh: Exec format error
Sorry, try again.
sudo: unable to run /var/www/Bash/mits/getpass.sh: Exec format error
Sorry, try again.
sudo: 3 incorrect password attempts
what is this $'\r' variable (I don't have anything like that in my code ! it seems like \n at the end of the files or something like that !!!) !! these two files are exactly the same with my laptop files only the path and the password is changed for the server but why it fails to run on my server ? I am sure the password is modified to be the server password and I tested that many times. but I don't know why it gives error on the password too :(
I also tried to put my application path in rc.local which is run with root permission but it also failed.
I only want to try this one as a solution so I would be happy to propose your solution for this type of start up handling.
thanks in advance.
You appear to have embedded windows line endings (\r\n) in your shell script, you can run the command dos2unix on the file and that should fix it. If you don't have dos2unix you can use tr and something like,
tr -d '\r' < askpass.sh > out.sh
Then
mv out.sh askpass.sh
Try add this as the first line of Your script
#!/bin/bash
Maybe you have on the other machine set different shell. Try check it by running
env | grep SHELL

Resources