Assistance to connect to servers via ssh once and collect various commands into separate variables

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Assistance to connect to servers via ssh once and collect various commands into separate variables
# 8  
ssh -qn is ssh -q -n where -n inhibits reading from stdin - good for ssh -q -n remotehost remotecommand. But it must be ssh -q remotehost < file and ssh -q remotehost << heredoc and ssh -q remotehost <<< herestring because here you want it to read from stdin.

Also I suspect that in a normal here-document the backtick expression `uname -r`is already locally evaluated, so the local result will be passed to the remote host.
A workaround is <<-"EOFSSH" where the quotes should suppress the immediate evaluation of backtick-expressions and all kinds of $-expressions in the following here-document.

Last edited by MadeInGermany; 10-10-2019 at 04:42 AM..
These 3 Users Gave Thanks to MadeInGermany For This Post:
# 9  
Thanks, MadeInGermany, for pointing that ssh -n option out. Still, the commands are passed as such (no backtics ``!) to the remote server, so uname -r will be executed remotely.

The script was tested successfully as given in post #4, admittedly on one single server only. The respective variables were correctly filled with the remote info. If something does not work as expected, start over and test stepwise:
- log into the respective remote server(s) and interactively run the commands as given. They should give a one line output each. Multiline output will make the approach fail.
- run the entire ssh command on one server and with one remote command, then with multiple commands, reading results into local variables
- test remote access on all servers, with one command, and then with multiple. If one server (the 50th?) fails, repeat former steps on this one. The error may be due to a remote inconsistency.

- include thorough debuggung / error handling between command (blocks).

I quickly verified the code reading multiple remote servers from a SOURCE_FILE, and it works. echo the remote IP before running the ssh command.
This User Gave Thanks to RudiC For This Post:
# 10  
Hello RudiC,

I've continued to work on this script using your suggestions and I'm very happy to report that your script suggestion has worked for me. I did as you suggested and tried your code on one VM and I was successful producing the variables in the HERE DOCUMENT with one connection to each server. This should greatly improve the speed and efficiency of my script.

Thanks very much to you and all who contributed to this thread! Very much appreciate your patience in explaining how to make this code work.
This User Gave Thanks to greavette For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #406
Difficulty: Medium
Cygwin was originally developed by Cygnus Solutions, which was later acquired by Microsoft.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Connect direct - SFTP - List of servers that I can connect

Greetings Experts, I am working for a bank client and have a question on connect-direct and SFTP. We are using Linux RedHat servers. We use connect-direct to transfer (NDM) files from one server to another server. At times, we manually transfer the files using SFTP from one server to another... (2 Replies)
Discussion started by: chill3chee
2 Replies

2. Shell Programming and Scripting

Find active SSH servers w/ ssh keys on LAN

Hi, I am trying to complete my bash script in order to find which SSH servers on LAN are still active with the ssh keys, but i am frozen at this step: #!/bin/bash # LAN SSH KEYS DISCOVERY SCRIPT </etc/passwd \ grep /bin/bash | cut -d: -f6 | sudo xargs -i -- sh -c ' && cat... (11 Replies)
Discussion started by: syrius
11 Replies

3. Shell Programming and Scripting

Shell script to connect to multiple ssh servers

Hello, I have access to several linux servers (mostly centos based) located in a DC in another country. from day to day I need to login to each of them to do some work (they dont have gui/window manager installed, I work only from console), or even to just do a check like df -h for disc usage.... (3 Replies)
Discussion started by: MaRiOsGR
3 Replies

4. UNIX for Advanced & Expert Users

Collect files from different servers to a single server and append them

Hi, I have on 3 servers. I want to collect output report generated by them to a single server and append all the reports. Please tell me how can i do this? (2 Replies)
Discussion started by: pratikm23
2 Replies

5. Shell Programming and Scripting

connect to multiple servers using SSH and execute commands

Requirement: Run a shell script with below inputs file name checksum path the script should go to multiple servers (around 35) and verify the input cksum and if there is a mismatch display a simple message to the user that cksum verification failed. host details, user id /... (1 Reply)
Discussion started by: amicableperson
1 Replies

6. UNIX for Dummies Questions & Answers

need assistance on Calling DB user from separate file in Shell script

Hi All, I need to execute a SQL via shell script and i am connecting to Oracle DB by this way $USERNAME1/$PASSWORD1@$STRING1 and i need to get username, password and string from someother file stored in the Unix Directory. $Username, $Password and $String is stored in File A in Path A and i want... (1 Reply)
Discussion started by:
1 Replies

7. Shell Programming and Scripting

Send Remote Commands via SSH with variables

Hi there I found the Command to send commands to other servers like: sv01> ssh user@sv02 'ps -ef' But I cant use Variables from a script i want to execute on another server like: sv01> ssh user@sv02 'cd $SCRIPTHOME' although the variable is set on sv01. How can I run commands on sv02 with... (2 Replies)
Discussion started by: DarkSwiss
2 Replies

8. Shell Programming and Scripting

Cant separate variables

Hey guys, new not being able to seperate variables. the code runs like this... OPTIONS=$(awk '{print $2}' /etc/fstab} a=$(zenity --list --text "Mount points selection" --radiolist --column "choice" --column "mountpt" FALSE $OPTIONS); echo $a Note: the result i get is that... (2 Replies)
Discussion started by: dplate07
2 Replies

9. Shell Programming and Scripting

Need assistance with appending strings using sed and variables

HI, Can't seem to find anything on the forums to fix this. I have a file, one line within this will not have a specific string at the end. I have the string, but need to append it to the specific line which has it missing. I need to use a variable for this, $string - I am using double... (13 Replies)
Discussion started by: mandriver
13 Replies

10. Shell Programming and Scripting

need script to connect sftp servers

Dear friends, i need to connect sftp server from my home directory using script . Please can anyone help me on this. (1 Reply)
Discussion started by: kittusri9
1 Replies