How to use python script to automatically detect the ip address of SSH server?

Some servers in the local area network have strange personalities. Every time they are shut down and restarted, their ip addresses will change, which is very unpredictable.

So every time we encounter a sudden power outage, our first reaction is "Wow, you can leave work early", and then the second reaction is "Fuck, tomorrow SSH server will not be connected again".

To know the new IP of the SSH server, you often need to enter the server room, endure the roar of the chassis, go around, toss, and turn upside down in the twisted USB, VGA, HDMI, and RJ45 lines, and connect the monitor, keyboard, Only use the mouse to enter "ifconfig" with the keyboard. After checking the ip address, unplug the monitor, keyboard, and mouse. After this setback, at least half an hour has passed. The makeup on the face will be spent. !

In fact, a python script can save you these troubles.

First, install paramiko and then import it.

import paramiko

Then, create a new SSHClient.

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

Finally, you can try to connect, if no error is reported, it means that the correct ip address has been found.

ssh.connect('ip address',port,'username','password')

How to judge whether an error is reported during the connection process? try...catch...that's it.

Guess you like

Origin blog.csdn.net/esa72ya/article/details/99291353