LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   getting return code from command with piped input (https://www.linuxquestions.org/questions/linux-newbie-8/getting-return-code-from-command-with-piped-input-4175429610/)

noguru 09-29-2012 10:36 AM

getting return code from command with piped input
 
I have the following line in a bash script:
echo password > ssh root@domain command
I want to get the status (return code) from the SSH command, NOT the ECHO command. The SSH connects (it gives me a "success" message). The command is not working for some reason and I want to determine the reason. I have several commands, (each on a separate SSH line in the script) and want to be sure they all work, before concatenating them into one <compound-command> in a single script line.

I own multiple (about 35) Linux machines and need to manage them remotely. They all have the same id (root) & password for management. I am building an .ssh directory with my pubkey in the auhorized_list to do this more effectively in the future.

414N 09-29-2012 10:46 AM

I think your command line is wrong, namely the ">" is used to write to a file, not to pipe commands together.
Try switching to "|" (pipe).
The password you're echoing is for the ssh authentication on the remote host?

noguru 09-30-2012 12:21 PM

I tried switching to "|" and that while it appeared to make the ssh connection, the comand following it was not recognized. The command is "mkdir" with all the necessary parameters.

Yes, I am passing the password via echo (hence the >), to ssh, so I don't have to keep retyping it. I have been told that doing:
echo "something" > pgm
will act as if I were typing it from stdin.

414N 09-30-2012 12:38 PM

Let me tell you that you're doing it wrong ;)
Instead of using a password login with ssh switch to a password-less authentication.
There are tons of tutorials on how to do it on the web. This is just one of the many out there.

noguru 09-30-2012 01:31 PM

Quote:

Originally Posted by 414N (Post 4793350)
Let me tell you that you're doing it wrong ;)
Instead of using a password login with ssh switch to a password-less authentication.
There are tons of tutorials on how to do it on the web. This is just one of the many out there.

Thanks for the RSA advice, but it does not answer my question OR solve my problem. As I mentioned originally, I own about 35 PC's on a private network. They currently have no .ssh directory and thus no RSA keys installed. What I want to do is programmatically install MY public key on those PC's, THEN I can use the normal RSA-key mechanism. In the FUTURE of these PC's, their O/S images will include my key in an .ssh directory. I do NOT have the luxury of rebuilding them now (and won't until January) and I do not have time to login to each machine and type commands to install the keys.

And yes, if I had KNOWN I would need to do this remote access, I would have installed the keys before I put the machines to use.

cbtshare 09-30-2012 02:29 PM

for that to work I believe you have to write the script with expect

Quote:

#!/usr/bin/expect

spawn ssh root@server
expect "password"
send Fdefg43GFSe
spawn Mkdir etc
Interact

noguru 10-01-2012 06:57 AM

Quote:

Originally Posted by cbtshare (Post 4793418)
for that to work I believe you have to write the script with expect

I've installed "expect" and started looking for usage examples that might help me ocnvert my bash script (with a for..in loop) toan "expect" script. I need to loop through 35 of these ssh comands with substitutions of the ssh host. Can you point me to some examples that are more than simple while {1} loops and allow substitutions and string concatenation with variables (ie. the usual bash scripting actions)?


All times are GMT -5. The time now is 06:42 AM.