Monday 16 October 2017

linux - how to tail into the last file on a remote server via ssh



I always tail logs (both error and info).. which requires the following manual steps 1. ssh into a server 2. cd into the logs directory 3. identify the last file that is either error or info 4. tail into it


this is what a typical log directory looks like:


error-2017-12-11.log  error-2017-12-30.log  error-2018-01-05.log  error-2018-01-11.log  error-2018-01-17.log  error-2018-01-23.log  error-2018-01-29.log  info-2017-12-26.log  info-2018-01-01.log  info-2018-01-07.log  info-2018-01-13.log  info-2018-01-19.log  info-2018-01-25.log  info-2018-01-31.log
error-2017-12-13.log error-2017-12-31.log error-2018-01-06.log error-2018-01-12.log error-2018-01-18.log error-2018-01-24.log error-2018-01-30.log info-2017-12-27.log info-2018-01-02.log info-2018-01-08.log info-2018-01-14.log info-2018-01-20.log info-2018-01-26.log info-2018-02-01.log
error-2017-12-26.log error-2018-01-01.log error-2018-01-07.log error-2018-01-13.log error-2018-01-19.log error-2018-01-25.log error-2018-01-31.log info-2017-12-28.log info-2018-01-03.log info-2018-01-09.log info-2018-01-15.log info-2018-01-21.log info-2018-01-27.log info-2018-02-02.log
error-2017-12-27.log error-2018-01-02.log error-2018-01-08.log error-2018-01-14.log error-2018-01-20.log error-2018-01-26.log error-2018-02-01.log info-2017-12-29.log info-2018-01-04.log info-2018-01-10.log info-2018-01-16.log info-2018-01-22.log info-2018-01-28.log info-2018-02-03.log
error-2017-12-28.log error-2018-01-03.log error-2018-01-09.log error-2018-01-15.log error-2018-01-21.log error-2018-01-27.log error-2018-02-02.log info-2017-12-30.log info-2018-01-05.log info-2018-01-11.log info-2018-01-17.log info-2018-01-23.log info-2018-01-29.log outfile
error-2017-12-29.log error-2018-01-04.log error-2018-01-10.log error-2018-01-16.log error-2018-01-22.log error-2018-01-28.log error-2018-02-03.log info-2017-12-31.log info-2018-01-06.log info-2018-01-12.log info-2018-01-18.log info-2018-01-24.log info-2018-01-30.log

I want to create a command alias that lets me do this from a remote machine instantly



doing this as a single command on the remote server is easy (grep info for info, and error for error):


tail -f `ls -Art | grep info | tail -n 1`

but when I try to run this alias:


alias logger='ssh -i /file.pub user@host -t 
"cd /path/to/logs; tail -f `ls -Art | grep info | tail -n 1`; bash --login"'

I get this error:


tail: cannot open '.viminfo' for reading: No such file or directory
tail: no files remaining

ideas?



function option


function totprod1log() {
ssh -i file.pub user@host;
cd /path/to/logs;
tail -f $(ls -Art | grep info | tail -n 1);
bash --login;
}

this option simply made me login on aws, but nothign else



Answer



When your alias runs ssh ... "cd ...; commands using backquote that I can't easily show on Stack" that commands your shell to run the backquoted ls ... | ... pipeline locally, which finds the name of the newest file in your current directory on your system, and sends that filename as part of the command to the remote system, where of course trying to tail that file doesn't work.


Your options are:


 # ugly quoting to work with doublequotes
alias logger='ssh ... "cd ...; tail -f \`ls ... | ...\`; bash --login"'

# shell function or script, which let you use clearer singlequotes
logger(){
ssh ... 'cd ...; tail -f `ls ... | ...`; bash --login'
}
# or
cat <<"END" >logger # use some dir (early) in $PATH
ssh ... 'cd ...; tail -f `ls ... | ...`; bash --login'
END
chmod +x logger

In general you could also provide the command as input to the remote shell instead of a commandline (argument)


ssh ... <<"END" # shouldn't need -t in this case 
cd ...; tail -f `ls ... | ...`
END

but this doesn't combine with your apparent though unmentioned and unexplained desire to leave bash --login running after exiting the tail.


Note the two heredoc cases quote the delimiter string so local shell does NOT subsitute backquote, or certain other things, within the data.


And in all cases it would be better to use the newer $( ... ) syntax for command substitution instead of the old backquote syntax -- especially for questions on Stack where backquotes interfere with (much? most?) non-codeblock formatting.


No comments:

Post a Comment

Where does Skype save my contact&#39;s avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...