I need to copy few files from a remote directory which has subdirectories within it. I am using lftp to do that but shell commands inside it aren't working. Is there a workaround for this? Please see code below. Any help is much appreciated guys!
lftp -u $USER,$PASS sftp://$HOST <<EOF 2>&1
#find file file_name with absolutepath from REMOTE_DIR which lies in any of its subdirectories
filefound=`find "${REMOTE_DIR}"`-name "{$file_name}"`
#Get the absolutepath for subdirectory where the file resides
dir_loc=`dirname "${filefound}"`
lcd ${LOCAL_DIR}
cd ${dir_loc}
get ${file_name}
bye
EOF
The error I am getting is:
filefound: command not found
dir_loc:command not found
That's a conceptual misunderstanding.
You cannot run shell commands using SFTP client. To run shell commands use an SSH client.
Related
I am using lftp to connect to SFTP server using the below in a shell script.
host=testurl.url.com
user=username
pass=pass
lftp<<EOF
open sftp://${host}
user ${user} ${pass}
cd test/myfolder/
bye
EOF
when executing the above using a shell script, the script exits but I am not sure if a connection is established and I don't see the output of my cd command which I executed within lftp.
Is there a way to output to a log file to see if connection is successful and the output of cd command.
Thank you.
I added a ls to the list of commands and I was able to list the directories
host=testurl.url.com
user=username
pass=pass
lftp<<EOF
open sftp://${host}
user ${user} ${pass}
cd test/myfolder/
ls
bye
EOF
Hi i have created a batch file (run.bat) that after execution connects me to UNIX server with help of plink. But issue starts from this point i have to execute a script after connection to my server the script contains a command sudo -l. After the execution i get the error as mentioned in subject can anyone help me on this issue ??
Batch File-:
"C:\Program Files\PuTTY" plink -ssh -pw Tos#12Ts w44dvftyw#caa1607UX009.wvd.abcd.net /opt/sieb/w44dvftyw/run.sh
Script file(run.sh) -:
#!/bin/bash
sudo -l
It says
sudo: command not found
But when i run my script normally on UNIX server it runs with no issues. What am i missing here to make it work this way please help.
Scripts such as ~/.profile or ~/.bash_profile responsible for setting up the current user's PATH are run only on login shells.
Running sh -c 'somescript' (as performed by ssh host 'somescript') is neither a login shell, nor an interactive shell; thus, it does not gain the benefit of such scripts.
This means that additions to the PATH (in your case, /usr/local/bin) may not be present with commands run in this way.
Among your options:
Pass the PATH you want as part of the command to remotely run. This might look like:
plink -ssh user#host "PATH=/bin:/usr/bin:/usr/local/bin /opt/sieb/w44dvftyw/run.sh"
Embed a working value in the script you're running:
#!/bin/bash
PATH=/bin:/usr/bin:/usr/local/bin
# ...put the rest of your script here.
I know that there are some related questions already about this, but can't make it work for me!
So I can run grep in the command line and it works fine, but if I do that on a bash script, I have the following error:
grep: secondword:No such file or directory
I am connecting via ssh to the server, then I run some commands. The path to grep in the server is /bin/grep, but still it does not work. Here is the sample code:
#!/bin/bash
$host="user#host";
ssh $host "
myinfo=\$(grep "word secondword" path/to/file);
"
I also verified that it does not have the CR that is created in Windows with Notepad++. Any ideas on how to fix this?
EDIT:
As suggested, I made the following change with the quotes:
#!/bin/bash
$host="user#host";
ssh $host "
myinfo=\$(grep \"word secondword\" path/to/file);
"
but now I have a very weird behavior: it looks like is listing all the files that are on the home server path.Doing echo to the variable:
file1 file2 file 3
file4 file5 etc.
Why it as this behavior? Did I miss something?
Please Put Script working Dir. While run crontab it will take as user home as default path.
#!/bin/bash
cd Your_Path
$host="user#host"
ssh $host
myinfo=\$(grep "word secondword" path/to/file)
I was trying the below program,
This is a simple script, to cd into a folder
#! /bin/bash
cd /root/
But this below command , doesnt get into the folder
EDITED
#!/bin/bash
alias ex="cd /fs/fm"
alias ex1="source setenv"
alias ex2="cd /fs/fm/tests"
alias ex3="runtest"
To get into /root/ you should make sure that you have permissions. It's accessible if you're running as root itself but if you're running as a normal user you should consider becoming root first. One way is to use sudo:
sudo bash script.sh
And again, make sure your script is in UNIX format. Certainly you can't change to /root/\r.
sed -i 's|\r||' script.sh
dos2unix script.sh
This will never work The script you're running is a separate process, when it finishes you get back to the original environment (cwd, enviroment variables, etc...).
Create an alias:
alias r="cd /root"
or execute the script within your shell:
. myscript
Note: . is a synonym for source.
Ok so I kinda know how to do this locally with a find then cp command, but don't know how to do the same remotely with scp.
So know this:
scp -vp me#server:/target/location/ /destination/dir/.
That target directory is going to be full of database backups, how can I tell it to find the latest backup, and scp that locally?
remote_dir=/what/ever
dst=remote-system.host.name.com
scp $dst:`ssh $dst ls -1td $remote_dir/\* | head -1` /tmp/lastmod
Write a script on the remote side that uses find to find it and then cat to send it to stdout, then run:
ssh me#server runscript.sh > localcopy