Up vote 0 down vote favorite share g+ share fb share tw.
I'm trying to ssh into a remote machine, obtain a directory listing, assign it to a variable, and then I want to be able to use that variable in the rest of the script on the local machine. After some research and setting up all the right keys and such, I can run commands via ssh just fine. Specifically, if I do: ssh -t user@server "ls /dir1/dir2/; exit; bash" I do get a directory listing.
If I instead do: ssh -t user@server "set var1=`ls /dir1/dir2/`; exit; bash" instead gives an ls error that the directory was not found. Also of note is that this happens before I am asked for the ssh key passphrase, which makes me think that it's executing locally somehow. Any idea on how I can create a local variable with a directory listing list of the remote host in a bash script?
Bash ssh link|improve this question asked Oct 5 '11 at 22:18CHP776 100% accept rate.
Simply var1=( $(ssh user@server ls /dir1/dir2) ) then test it: for line in "${var1@}"; do echo "$line"; done That said, I'd prefer ssh user@server find /dir1/dir2 -maxdepth 1 -print0 | xargs -0 This will deal a lot better with special filenames be more flexible (man find(1)) adding -type f to limit to files only.
For a working solution with -print0. – l0b0 Oct 6 '11 at 13:01 @l0b0: congrats with your 2^11 reputation! – sehe Oct 6 '11 at 13:02 Thanks, I was able to get it to work – CHP Oct 7 '11 at 2:32.
Mmmm ... listing=`ssh user@server "ls /dir1/dir2/"`` is working perfectly for me. Am I missing something? You can iterate the listing with: for I in $listing; do echo $i done.
Sorry I'm new to this and not able to try it out at the moment. I have two questions (I'll try tomorrow when I'm back on the system myself but until then...). My original thought about doing it the way you have here is that the ssh session would not log out, or, if I do log it out via a command, it would return additonal stuff in the listing like the exit message.
Also, when I ssh, there's login messages (server MOTDs etc) that come up that I can't control. Would those show up? Admittedly, I thought for sure those things would come up and didn't try, but clearly I could be very wrong.
– CHP Oct 6 '11 at 1:47.
Your command in quote is executed before executing the ssh command. Escaping the single quote should fix.
Just to be clear, that's one of these `, not one of these '. I did try escaping it with a \ (if that's even how you do it, I'm pretty new to this) but that didn't fix it, though I can't remember the error. The reason I put it there is because someone once told me I need it to make stuff into lists to loop through :) .
– CHP Oct 6 '11 at 2:01.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.