?
Solved

Having problems with IFS Variable using SSH

Posted on 2010-03-24
8
Medium Priority
?
440 Views
Last Modified: 2013-12-26
I have a perl script that needs to pass a string of code to a remote system via ssh.  The code works perfectly while ssh'd into the command shell of the remote system, but when it is passed to the remote system via ssh, I am unable to manipulate the IFS Variable to and the code fails.  I have tried sourcing ~./bashrc at the beginning of the command string, but to no avail.  Any help would be greatly appreciated. Probably something simple with the enviornment, but it's driving me crazy.

Linux localsystem 2.6.18-53.el5 #1 SMP Wed Oct 10 16:34:19 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux
Linux remotesystem 2.6.18-92.el5 #1 SMP Tue Apr 29 13:16:15 EDT 2008 x86_64 x86_64 x86_64 GNU/Linux
(keys have properly installed between systems)
(This Works)
ssh remotesystem
 echo -e "begin\n\n\n";  cd /home/program/jboss-eap-4.2.CP07/jboss-as/server/customer/log_sec; pwd; IFS=$'\n'; echo $IFS | od -c; FILES=( $(find ./ -maxdepth 1 -type f -printf "%f\n"  ) ); echo -e "NUMBR:${#FILES[*]}\nFILES\n${FILES[*]}\n1.)${FILES[0]}\n2.)${FILES[1]}\n-------------"; if [[ -n $FILES ]]; then for i in ${FILES[*]}; do echo -e "i:$i";  MD5=$(/usr/bin/md5sum $i); MD5VAL=${MD5:0:32 }; FNAME=${MD5:34}; echo "$MD5VAL,\"$FNAME\""; done; else echo -e "False\n${#FILES[*]}\n"; fi


--------------------------------------Desired Output--------------------------------------------------------
/home/program/jboss-eap-4.2.CP07/jboss-as/server/customer/log_sec
0000000  \n
0000001
NUMBR:2
FILES
this
this is also a file
1.)this
2.)this is also a file
-------------
i:this
d41d8cd98f00b204e9800998ecf8427e,"this"
i:this is also a file
6607e4132c9a84526d2a11b27979ec0c,"this is also a file"

(This Fails)
 ssh remotesystem 'source ~./bashrc; echo -e "begin\n\n\n";  cd /home/program/jboss-eap-4.2.CP07/jboss-as/server/customer/log_sec; pwd; IFS=$'\n'; echo $IFS | od -c; FILES=( $(find ./ -maxdepth 1 -type f -printf "%f\n"  ) ); echo -e "NUMBR:${#FILES[*]}\nFILES\n${FILES[*]}\n1.)${FILES[0]}\n2.)${FILES[1]}\n-------------"; if [[ -n $FILES ]]; then for i in ${FILES[*]}; do echo -e "i:$i";  MD5=$(/usr/bin/md5sum $i); MD5VAL=${MD5:0:32 }; FNAME=${MD5:34}; echo "$MD5VAL,\"$FNAME\""; done; else echo -e "False\n${#FILES[*]}\n"; fi'

----------------------------------FAILED OUTPUT------------------------------------------------------------
begin



/home/cscott/jboss-eap-4.2.CP07/jboss-as/server/firstrade/log_sec
0000000  \n
0000001
NUMBR:1
FILES
this
this is also a file
1.)this
this is also a file
2.)
-------------
i:this
this is also a file
,""
bash: ~./bashrc: No such file or directory
/usr/bin/md5sum: this
this is also a file: No such file or directory

0
Comment
Question by:cp5c0tt
  • 5
  • 3
8 Comments
 
LVL 26

Expert Comment

by:arober11
ID: 28545148
Hi

Your:

source ~./bashrc;  

should probably read:

source ~/.bashrc;  

Anyway I don't believe that's your problem, as I dought your file loop:

for i in ${FILES[*]}; do echo -e "i:$i";

Will return the individual files, stick an extra colon on the end or you echo to prove this e.g.

echo -e "i:$i:";
0
 

Author Comment

by:cp5c0tt
ID: 28566146
Yes, I fixed the ~/.bashrc, hazards of cut and paste ;-)
Here is the applicable output from your recommeded change -

then for i in ${FILES[*]}; do echo -e "i:$i:";

---- OUTPUT -----

i:this
this is also a file:

---- END ----

As  I mentioned earlier, this code works perfectly when I ssh into a shell on the remote machine, it is only when I pass the code to the remote machine via an ssh command.  For some reason (I suspect Security) the IFS Variable can not be manipulated.  

Thank you for your input arobert11!
0
 
LVL 26

Expert Comment

by:arober11
ID: 28571482
Nope, you can play happily play around with the IFS variable in a remote shell e.g.

ssh localhost 'echo -e "begin\n\n\n";  cd $HOME/boo; pwd; IFS=$'\n'; echo $IFS | od -c; FILES=( $(find ./ -maxdepth 1 -type f -printf "%f\n"  ) ); echo -e "NUMBR:${#FILES[*]}\nFILES\n${FILES[*]}\n1.)${FILES[0]}\n2.)${FILES[1]}\n-------------"; if [[ -n $FILES ]]; then IFS=':';for i in `echo "${FILES[*]}"|/usr/bin/tr "\n" ":" `; do echo -e "i:$i:";  MD5=$(/usr/bin/md5sum $i); MD5VAL=${MD5:0:32 }; FNAME=${MD5:34}; echo "$MD5VAL,\"$FNAME\""; done; else echo -e "False\n${#FILES[*]}\n"; fi'

It's the escaping in your example that's the issue.
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
LVL 26

Expert Comment

by:arober11
ID: 28571641
With your host / file names:

ssh remotesystem 'echo -e "begin\n\n\n";  cd /home/program/jboss-eap-4.2.CP07/jboss-as/server/customer/log_sec; pwd; IFS=$'\n'; echo $IFS | od -c; FILES=( $(find ./ -maxdepth 1 -type f -printf "%f\n"  ) ); echo -e "NUMBR:${#FILES[*]}\nFILES\n${FILES[*]}\n1.)${FILES[0]}\n2.)${FILES[1]}\n-------------"; if [[ -n $FILES ]]; then IFS=':';for i in `echo "${FILES[*]}"|/usr/bin/tr "\n" ":" `; do echo -e "i:$i";  MD5=$(/usr/bin/md5sum $i); MD5VAL=${MD5:0:32 }; FNAME=${MD5:34}; echo "$MD5VAL,\"$FNAME\""; done; else echo -e "False\n${#FILES[*]}\n"; fi'
0
 
LVL 26

Expert Comment

by:arober11
ID: 28571886
Appears I have the same cut-n-paste issues, as your good self:

ssh remotesystem 'echo -e "begin\n\n\n";  cd /home/program/jboss-eap-4.2.CP07/jboss-as/server/customer/log_sec; pwd; IFS=$'\n'; echo $IFS | od -c; FILES=( $(find ./ -maxdepth 1 -type f -printf "%f\n"  ) ); echo -e "NUMBR:${#FILES[*]}\nFILES\n${FILES[*]}\n1.)${FILES[0]}\n2.)${FILES[1]}\n-------------"; if [[ -n $FILES ]]; then IFS=:;for i in `echo "${FILES[*]}"|/usr/bin/tr "\n" ":" `; do echo -e "i:$i";  MD5=$(/usr/bin/md5sum $i); MD5VAL=${MD5:0:32 }; FNAME=${MD5:34}; echo "$MD5VAL,\"$FNAME\""; done; else echo -e "False\n${#FILES[*]}\n"; fi'
0
 
LVL 26

Accepted Solution

by:
arober11 earned 2000 total points
ID: 28573978
The following should give the desired results:

ssh remotesystem 'echo -e "begin\n\n\n";cd /home/program/jboss-eap-4.2.CP07/jboss-as/server/customer/log_sec;pwd;FILES=`find ./ -maxdepth 1 -type f|wc -l`;echo -e "NUMBR:$FILES";for aFile in `find ./ -maxdepth 1 -type f`; do aFile=`basename $aFile`;echo ":$aFile";MD5=$(/usr/bin/md5sum $aFile); MD5VAL=${MD5:0:32 }; FNAME=${MD5:34}; echo "$MD5VAL,\"$FNAME\""; done'
0
 

Author Comment

by:cp5c0tt
ID: 28575771
arobert11 you rock.  Thanks!  I believe my issue stemmed from the (incorrect) thought that the find was storing the results into $FILES (at least that is what I was trying to do) as an array based on the -printf "$f\n" statement.  

In anycase modifying the -printf to use ":" instead of "\n", allowed me to process the list using the IFS as a colon.  Awesome, Thanks again!
0
 

Author Comment

by:cp5c0tt
ID: 28576191
BTW Final code snippet: (If anyone cares :-P )
 ssh remotesystem 'cd /home/program/jboss-eap-4.2.CP07/jboss-as/server/customer/log_sec; IFS=$:; FILES=( $(find ./ -maxdepth 1 -type f -printf "%f:"  ) ); if [[ -n $FILES ]]; then for i in ${FILES[*]}; do  MD5=$(/usr/bin/md5sum $i); MD5VAL=${MD5:0:32 }; FNAME=${MD5:34}; echo "$MD5VAL,\"$FNAME\""; done; else echo -e "False\n${#FILES[*]}\n"; fi'

--------- Desired Output ----------------------------------
d41d8cd98f00b204e9800998ecf8427e,"this"
6607e4132c9a84526d2a11b27979ec0c,"this is also a file"
--------- End of Output -----------------------------------


0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month7 days, 8 hours left to enroll

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question