Automating ssh connection


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Automating ssh connection
# 1  
Old 10-14-2017
Network Automating ssh connection

I want to create a script for the ssh connection that handles any input. Be it IP or DNS.
And instead of typing in giant commands, I'd like to downplay it and treat it.

I imagined the following command:

i machine
or
i machine.exemple.com.fr
or
i 111.222.333.4444

Login attempts should be made with devops, or with matheus.

I do not know much about shell script I'm reading a few hours and working on the script below.
Can you help me?
Thank you.

> /bin/i && chmod +x /bin/i && vi /bin/i
bash /bin/i

Code:
#!/bin/bash

if [ "$1" == "" ]
then
clear
echo "how to use:"
echo "i 111.222.333.444"
echo "i name"
echo "i name.exemple.com.fr"
echo ""
fi

#verifying that the input is a valid ip
if [[ $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
      clear
      echo "Success! Accessing $1 with devops"
      ssh devops@$1
 elif
      clear
      echo "Oops! Could not access with devops, trying to access as matheus"
      ssh -i /home/devops/.ssh/matheus_rsa matheus@$1
else
      clear
      echo "Could not connect! Here's some information to access the provider platform."
      whois $1 | grep -E "mnt-by|OrgName|nserver"
      getent hosts $1
fi

#-f If you have text
if [ -f $1 ]; then
      clear
      echo "Success! Accessing $1 with devops"
      ssh devops@$1.exemple.com.fr
  elif
      clear
      echo "[1] Trying to connect..."
      ssh devops@$1
  elif
      clear
      echo "Oops! Could not access with devops, trying to access as matheus"
      ssh -i /home/devops/.ssh/matheus_rsa matheus@$1.exemple.com.fr
  elif
      clear
      echo "[2] Trying to connect..."
      ssh -i /home/devops/.ssh/matheus_rsa matheus@$1
else
      echo "Could not connect! Here's some information to access the provider platform."
      whois $1 | grep -E "mnt-by|OrgName|nserver"
      getent hosts $1
fi


Last edited by developy; 10-16-2017 at 10:44 PM..
# 2  
Old 10-14-2017
Have you created ssh keys and placed them in your user's home directory in a new directory called .ssh You then have to place your public in the .ssh directory of the remote user.

DO NOT use root to connect. Bad idea security-wise. Plus you have to modify the sshd.conf file on any box you want to ssh into -- using root. By default ssh (openssh version 2) will not let root log in.

Also I don't understand what you are doing - looks like you are trying to subvert or get around security which is also another bad idea.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 10-14-2017
I have my key configured correctly.
I just want to handle these cases so I can connect, and if it is not possible, get the information from the provider so I can access the platform and find out what happened to the machine and maybe open a ticket at the technical support.
Thank you

Last edited by developy; 10-16-2017 at 10:09 PM..
# 4  
Old 10-15-2017
Me too, I don't really understand your purposes here. Why don't you let the resolver handle IPs and names for you? Plus, you have logical inconsistencies in your script, e.g. testing for correct IP structure and, if failed, connect as root? Plus, ssh by default uses a user's public key file - no need to specify it unless taken from somewhere else.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 10-16-2017
It would be safer to connect to a personal account and then use sudo or plain su to elevate your privileges once there. Can you explain a bit more about what you are really trying to achieve and why you need to get connected this way?

Just for clarity, with the keys in place that you already have set up, can you connect using the command line without putting in a passphrase?



Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 6  
Old 10-16-2017
Code:
#!/bin/bash

if [ "$1" == "" ]
then
clear
echo "how to use:"
echo "i 111.222.333.444"
echo "i name"
echo "i name.exemple.com.fr"
echo ""
fi

#verifying that the input is a valid ip
if [[ $1 =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
      clear
      echo "Success! Accessing $1 with devops"
      ssh devops@$1
 elif
      clear
      echo "Oops! Could not access with devops, trying to access as matheus"
      ssh -i /home/devops/.ssh/matheus_rsa matheus@$1
else
      clear
      echo "Could not connect! Here's some information to access the provider platform."
      whois $1 | grep -E "mnt-by|OrgName|nserver"
      getent hosts $1
fi

#-f If you have text
if [ -f $1 ]; then
      clear
      echo "Success! Accessing $1 with devops"
      ssh devops@$1.exemple.com.fr
  elif
      clear
      echo "[1] Trying to connect..."
      ssh devops@$1
  elif
      clear
      echo "Oops! Could not access with devops, trying to access as matheus"
      ssh -i /home/devops/.ssh/matheus_rsa matheus@$1.exemple.com.fr
  elif
      clear
      echo "[2] Trying to connect..."
      ssh -i /home/devops/.ssh/matheus_rsa matheus@$1
else
      echo "Could not connect! Here's some information to access the provider platform."
      whois $1 | grep -E "mnt-by|OrgName|nserver"
      getent hosts $1
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Ssh script to validate ssh connection to multiple serves with status

Hi, I want to validate ssh connection one after one for multiple servers..... password less keys already setup but now i want to validate if ssh is working fine or not... I have .sh script like below and i have servers.txt contains all the list of servers #/bin/bash for host in $(cat... (3 Replies)
Discussion started by: sreeram4
3 Replies

2. BSD

Connection SSH to remote by ssh

Hello guys! I am setting up a script to access a unix remote server. My problem is that when I put the ssh line "my host", the script does not wait for the server response asking for the password to execute the line in which I put the password, that is, I need to put a form in which script has a... (1 Reply)
Discussion started by: aroucasp
1 Replies

3. UNIX for Advanced & Expert Users

Connection-less using ssh

Hi! I know its a recurring problem, but I am failing to sort this out, I have two servers ( A and B), in which I am able to connect without having to put password from server B to server A, but the connect from server A to server B. takes 7 minutes to establish??? on Server A, I have the... (7 Replies)
Discussion started by: fretagi
7 Replies

4. UNIX for Advanced & Expert Users

How keep running a program n an another computer via a connection ssh when the connection is closed?

Hi everybody, I am running a program on a supercomputer via my personal computer through a ssh connection. My program take more than a day to run, so when I left work with my PC I stop the connection with the supercomputer and the program stop. I am wondering if someone know how I can manage... (2 Replies)
Discussion started by: TomTomGre
2 Replies

5. Red Hat

Ssh connection

hi, I have ssh connection between two servers for a functional Id for SFTP purpose. I aim is to setup this for is only work when below command is used by a .ksh script. ssh userid@servername:/directory Unfortunately users who have access to functional id are manually using above command... (2 Replies)
Discussion started by: maddy26615
2 Replies

6. Shell Programming and Scripting

Automating file transfer between two SSH enabled server.

Hi Experts, Few more words to the title, both the servers are ssh enabled but I have read only access to the second server, so I cannot automate SFTP process using RSA/DSA keys. I am using Control M to trigger the script and do not want any manual intervention to enter the password to complete... (4 Replies)
Discussion started by: nchourasiya
4 Replies

7. Shell Programming and Scripting

automating ssh session with password

Hi Can anyone help me in automate a ssh session with password using shell script (7 Replies)
Discussion started by: raghav288
7 Replies

8. UNIX for Dummies Questions & Answers

ssh connection

Hi @ all! I've a problem with a ssh-connection. I want to establish a ssh-connection between an AIX-System and an SunOS-System without a password. The Users are different one's. Command : user1@server1 /home/user1 > ssh user2@server2 Is it possible? Greetings olli-h (1 Reply)
Discussion started by: olli-h
1 Replies

9. UNIX for Advanced & Expert Users

ssh connection

pls how do i connect to my freebsd server via ssh from a windows client?? I have sshd running on d freebsd server. (9 Replies)
Discussion started by: lealyz
9 Replies
Login or Register to Ask a Question