SSH Programatically


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SSH Programatically
# 1  
Old 04-24-2018
SSH Programatically

Hi Experts,

Need some help.


I am trying below code programatically to login to ssh ip from a csv file.
the below code logging into all servers and executing some command line syntax. But what I need is for example if i have 5 IP's ,if first one connects successfully and executes the commands then i want program to exit if not it has to login into 2nd IP check whether it will be able to access it and issue comands and get me output...like this...

and also the IP have .pem key authetication...so how would i modify below program to allow ssh with pem key


Code:
#!/bin/bash
USERNAME=someUser
HOSTS="host1 host2 host3"
SCRIPT="pwd; ls"
for HOSTNAME in ${HOSTS} ; do
    ssh -l ${USERNAME} ${HOSTNAME} "${SCRIPT}"
done



Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
It makes it far easier to read and preserves spaces for indenting or fixed-width data.

Last edited by rbatte1; 04-24-2018 at 10:17 AM.. Reason: Added CODE tags
# 2  
Old 04-24-2018
do it like this...

Code:
#!/bin/bash

USERNAME=someUser
HOSTS="host1 host2 host3"
SCRIPT="pwd; ls"
for HOSTNAME in ${HOSTS} ; do
ssh -l ${USERNAME} ${HOSTNAME} "${SCRIPT}" && break
done

&& break causes the shell when the prior command is successful to skip the rest of the loop immediately. Since you do not have any commands after the loop the program is ending then.

Notes
  • ${VAR} is no obligation here. $VAR will do too(The first one is 100% syntactically correct too; There are some cases however, when you have to use ${VAR}, e. g. when the following characters would extend the variable name like ${VAR}NAME. Without the braces, this would resolve another - possibly unset - variable named VARNAME)
  • I would avoid overwriting well defined variable Names like HOSTNAME. This variable is reserved for the real hostname of the system. Take any other variable name like "hostname"(lowercase) or "myhost"

Last edited by stomp; 04-24-2018 at 08:59 AM..
This User Gave Thanks to stomp For This Post:
# 3  
Old 04-24-2018
What if host1 and host3 has same password but host2 has different one
# 4  
Old 04-25-2018
The script does not handle passwords. Either the ssh prompts for a password, or an authorized ssh key has been set.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

AWK script for programatically modifying java files

Hi, I want to add a String variable to all java classes in my project. Assuming a class like public class Random { String var="Constant string"; ... ... ... } The text in bold is what I want to add to all java files in my workspace. I am an absolute newbie to AWK, and read somewhere that... (5 Replies)
Discussion started by: rocker86
5 Replies

2. HP-UX

[Solved] Not able to call a script from another programatically

Hi, I need to call a shell script from another. Let me explain you with an example. a1.sh and b1.sh are two scripts. a1.sh calls b1.sh internally. If i run a1.sh manually, it internally calls b1.sh but the problem is There is a process that calls this a1.sh at regular intervals,... (1 Reply)
Discussion started by: hadarapu
1 Replies

3. UNIX for Dummies Questions & Answers

How to findsuccessful job status completion in Autosys, programatically

We are using autosys for job scheduling. How to find successful job status completion in Autosys, programatically. Based on the successful job completion , mail has to be trigeered to Business users. Please help Thanks (0 Replies)
Discussion started by: vkalya
0 Replies

4. Solaris

Programatically read 'size' shown in top?

How can the 'size' of a process, that is shown by 'top', be read programatically? I'm fixing a memory leak in a large (20,000 lines) program. (The main.cpp is itself 7400 lines!). (3 Replies)
Discussion started by: douglaskbell
3 Replies

5. UNIX for Advanced & Expert Users

retrieve process state programatically

Assume I spawn a process on (csh) command line, like > du -a / >& /dev/null & which creates a process with id 1234. Now, I can suspend/resume that process with > kill -STOP 1234 > kill -CONT 1234 and can query the process state via 'jobs' or 'ps. How can I though query that state... (6 Replies)
Discussion started by: Andre_Merzky
6 Replies

6. OS X (Apple)

Programatically change which opens a file type

Hi there, If we have 2 apps on our customers machines that can both open files of a certain file extension, is there any way via shell commands to set which application should be the default? Essentially, I'm hoping to replicate Get Info > Open With > Change All, but using a script. Thanks... (0 Replies)
Discussion started by: davewg
0 Replies

7. Programming

how to programatically obtain informations about existing NICs on the system

hi, i would like to know how to programatically obtain informations about existing NICs (both configured and non-configured if possible) on *NIX? i need to write simple *NIX C/C++ program that will have these informations. i have tried to search forums (and also google) with no luck. any help... (2 Replies)
Discussion started by: nikoladsp
2 Replies

8. Programming

Add IpAddress Through Programatically

Hi all, Can u give me some suggestions how to add Ip Address through Programmatically , Actually I done it in Windows But now I want same thing in Unix................................ Can Any body Help to me....... (6 Replies)
Discussion started by: ykmraju
6 Replies

9. Programming

How to set the MSS programatically.

I'm sending a SYN packet and now I want to specify the MSS (The maximum segment size option in a TCP packet ) through it. How can I do that? Also, I'm not getting a SYN-ACK back from the remote host. TCPDUMP tells me that my packet is good and is a SYN. Then, why don't I get back the SYN-ACK? I... (0 Replies)
Discussion started by: zampya
0 Replies
Login or Register to Ask a Question