Table of Contents
Overview
Home Backend Development Python Tutorial Share an example of using Python to write an automatic SSH login to a remote server

Share an example of using Python to write an automatic SSH login to a remote server

Jun 25, 2017 am 10:22 AM
python server Log in automatic Remotely

Many times we like to directly ssh to the Linux server from the terminal of our own computer, rather than using the tool area with a UI interface to connect to our server. However, when using ssh in the terminal, we need to enter the account number and password every time, which is also a trouble, so we can simply create a small tool that runs on Linux/Mac os to automatically log in to the remote server through ssh.
Here is a GIF animation Let’s start with the example:

Overview

Let’s first sort out what functions we need:

1. 添加/删除连接服务器需要的IP,端口,密码2. 自动输入密码登录远程服务器
Copy after login

Yes, we will do it as simple as that Function

Start writing code
The code is relatively long, so I also put it on Github and Code Cloud. The address is at the bottom of the article:
1. Let’s create a module directory osnssh (Open source noob ssh), and then create two more directories below, one for storing the main program and call it bin, and one for saving login data (IP, port, password) called data.

-osnssh
    -bin
    -data
Copy after login

1. Setup program: add/delete IP, port, password. Create py file bin/setting.py:

#!/usr/bin/env python#-*-coding:utf-8-*-import re, base64, os, sys
path = os.path.dirname(os.path.abspath(sys.argv[0]))'''
选项配置管理
__author__ = 'allen woo'
'''def add_host_main():while 1:if add_host():break
        print("\n\nAgain:")def add_host():'''
    添加主机信息
    :return: 
    '''
    print("================Add=====================")
    print("[Help]Input '#q' exit")# 输入IP
    host_ip = str_format("Host IP:", "^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$")if host_ip == "#q":return 1# 输入端口
    host_port = str_format("Host port(Default 22):", "[0-9]+")if host_port == "#q":return 1# 输入密码
    password = str_format("Password:", ".*")if password == "#q":return 1# 密码加密
    password = base64.encodestring(password)# 输入用户名
    name = str_format("User Name:", "^[^ ]+$")if name == "#q":return 1elif not name:
        os.system("clear")
        print("[Warning]:User name cannot be emptyg")return 0# The alias# 输入别名
    alias = str_format("Local Alias:", "^[^ ]+$")if alias == "#q":return 1elif not alias:
        os.system("clear")
        print("[Warning]:Alias cannot be emptyg")return 0# 打开数据保存文件
    of = open("{}/data/information.d".format(path))
    hosts = of.readlines()# 遍历文件数据,查找是否有存在的Ip,端口,还有别名for l in hosts:
        l = l.strip("\n")if not l:continue
        l_list = l.split(" ")if host_ip == l_list[1] and host_port == l_list[2]:
            os.system("clear")
            print("[Warning]{}:{} existing".format(host_ip, host_port))return 0if alias == l_list[4]:
            os.system("clear")
            print("[Warning]Alias '{}' existing".format(alias))return 0
    of.close()# save# 保存数据到数据文件
    of = open("{}/data/information.d".format(path), "a")
    of.write("\n{} {} {} {} {}".format(name.strip("\n"), host_ip.strip("\n"), host_port, password.strip("\n"), alias.strip("\n")))
    of.close()
    print("Add the success:{} {}@{}:{}".format(alias.strip("\n"), name.strip("\n"), host_ip.strip("\n"), host_port, password.strip("\n")))return 1def remove_host():'''
    删除主机信息
    :return: 
    '''while 1:# 打开数据文件
        of = open("{}/data/information.d".format(path))
        hosts = of.readlines()
        of.close
        l = len(hosts)if l <= 0:
            os.system("clear")
            print("[Warning]There is no host")return

        print("================Remove================")
        print("+{}+".format("-"*40))
        print("|     Alias   UserName@IP:PORT")
        hosts_temp = []
        n = 0# 遍历输出所以信息(除了密码)供选择for i in range(0, l):if not hosts[i].strip():continue
            v_list = hosts[i].strip().split(" ")
            print("+{}+".format("-"*40))
            print("| {} | {}   {}@{}:{}".format(n+1, v_list[4], v_list[0], v_list[1], v_list[2]))
            n += 1
            hosts_temp.append(hosts[i])
        hosts = hosts_temp[:]
        print("+{}+".format("-"*40))
        c = raw_input("[Remove]Choose the Number or Alias(&#39;#q&#39; to exit):")
        is_alias = False
        is_y = Falsetry:
            c = int(c)if c > l or c < 1:
                os.system("clear")
                print("[Warning]:There is no")continuedel hosts[c-1]
            is_y = Trueexcept:
            is_alias = Trueif is_alias:if c.strip() == "#q":
                os.system("clear")break  
            n = 0for l in hosts:if c.strip() == l.split(" ")[4].strip():del hosts[n]
                    is_y = True 
                n += 1if not is_y:
            os.system("clear")
            print("[Warning]:There is no")continueelse: # save# 再次确认是否删除
            c = raw_input("Remove?[y/n]:")if c.strip().upper() == "Y":
                of = open("{}/data/information.d".format(path), "w")for l in hosts:
                    of.write(l)
                print("Remove the success!")
                of.close()def str_format(lable, rule):&#39;&#39;&#39;
    用于验证输入的数据格式
    :param lable: 
    :param rule: 
    :return: 
    &#39;&#39;&#39;while 1:
        print("{} (&#39;#q&#39; exit)".format(lable))
        temp = raw_input().strip()
        m = re.match(r"{}".format(rule), temp)if m:breakelif "port" in lable:
            temp = 22breakelif temp.strip() == "#q":
            os.system("clear")break
        os.system("clear")
        print("[Warning]:Invalid format")return temp
Copy after login

2. Let’s add another function in setting.py for Output our information, that is, about me.

def about():&#39;&#39;&#39;
    输出关于这个程序的信息
    :return: 
    &#39;&#39;&#39;
    of = open("{}/bin/about.dat".format(path))
    rf = of.read()try:
        info = eval(rf)
        os.system("clear")
        print("================About osnssh================")for k,v in info.items():
            print("{}: {}".format(k, v))except:
        print("For failure.")return
Copy after login

Then create a file about.dat under the bin directory and write some of our information, such as:

{"auther":"Allen Woo","Introduction":"In Linux or MAC using SSH, do not need to enter the IP and password for many times","Home page":"","Download address":"https://github.com/osnoob/osnssh","version":"1.1.0","email":"xiaopingwoo@163.com"
}
Copy after login

Now the setup program is like this:

2. Automatically log in to the remote server program: Create a py file in bin called auto_ssh.py:
Note: Here we need to install a package called: pexpect, user terminal interaction, capture interaction information to automatically enter passwords.
Install pexpect:

pip install pexpect
Copy after login

Then start writing code:

#!/usr/bin/env python#-*-coding:utf-8-*-import os, sys, base64import pexpect
path = os.path.dirname(os.path.abspath(sys.argv[0]))def choose():# 打开我们的数据文件
    of = open("{}/data/information.d".format(path))
    hosts = of.readlines()
    hosts_temp = []for h in hosts:if h.strip():
            hosts_temp.append(h)
    hosts = hosts_temp[:]
    l = len(hosts)if l <= 0:
        os.system("clear")
        print("[Warning]Please add the host server")returnwhile 1:

        print("=================SSH===================")
        print("+{}+".format("-"*40))
        print("|     Alias   UserName@IP:PORT")for i in range(0, l):
            v_list = hosts[i].strip().split(" ")
            print("+{}+".format("-"*40))
            print("| {} | {}   {}@{}:{}".format(i+1, v_list[4], v_list[0], v_list[1], v_list[2]))
        print("+{}+".format("-"*40))
        c = raw_input("[SSH]Choose the number or alias(&#39;#q&#39; exit):")
        is_alias = False
        is_y = Falsetry:
            c = int(c)if c > l or c < 1:
                os.system("clear")
                print("[Warning]:There is no")continue
            l_list = hosts[c-1].split(" ")
            name = l_list[0]
            host = l_list[1]
            port = l_list[2]
            password = l_list[3]
            is_y = Trueexcept:
            is_alias = Trueif is_alias:if c.strip() == "#q":
                os.system("clear")returnfor h in hosts:if c.strip() == h.split(" ")[4].strip():
                    l_list = h.split(" ")
                    name = l_list[0]
                    host = l_list[1]
                    port = l_list[2]
                    password = l_list[3]
                    is_y = Trueif not is_y:continue# ssh# 将加密保存的密码解密
        password = base64.decodestring(password)
        print("In the connection...")# 准备远程连接,拼接ip:port
        print("{}@{}".format(name, host))if port == "22":
            connection("ssh {}@{}".format(name, host), password)else:
            connection("ssh {}@{}:{}".format(name, host, port), password)def connection(cmd, pwd):&#39;&#39;&#39;
    连接远程服务器
    :param cmd: 
    :param pwd: 
    :return: 
    &#39;&#39;&#39;
    child = pexpect.spawn(cmd)
    i = child.expect([".*password.*", ".*continue.*?", pexpect.EOF, pexpect.TIMEOUT])if( i == 0 ):# 如果交互中出现.*password.*,就是叫我们输入密码# 我们就把密码自动填入下去
        child.sendline("{}\n".format(pwd))
        child.interact()elif( i == 1):# 如果交互提示是否继续,一般第一次连接时会出现# 这个时候我们发送"yes",然后再自动输入密码
        child.sendline("yes\n")
        child.sendline("{}\n".format(pwd))#child.interact()    else:# 连接失败
        print("[Error]The connection fails")
Copy after login

Okay, now we only need to start the file, which is the first menu after opening the program
3 .Create an osnssh.py file in the osnssh directory:

#!/usr/bin/env python#-*-coding:utf-8-*-import os, sys
sys.path.append("../")from bin import setting, auto_ssh
path = os.path.dirname(os.path.abspath(sys.argv[0]))&#39;&#39;&#39;
方便在LINUX终端使用ssh,保存使用的IP:PORT , PASSWORD
自动登录
__author__ = &#39;allen woo&#39;
&#39;&#39;&#39;def main():while 1:

        print("==============OSNSSH [Menu]=============")
        print("1.Connection between a host\n2.Add host\n3.Remove host\n4.About\n[Help]: q:quit   clear:clear screen")
        print("="*40)
        c = raw_input("Please select a:")if c == 1 or c == "1":
            auto_ssh.choose()if c == 2 or c == "2":
            setting.add_host_main()if c == 3 or c == "3":
            setting.remove_host()if c == 4 or c == "4":
            setting.about()elif c == "clear":
            os.system("clear")elif c == "q" or c == "Q" or c == "quit":
            print("Bye")
            sys.exit()else:
            print("\n")if __name__ == &#39;__main__&#39;:try:
        of = open("{}/data/information.d".format(path))except:
        of = open("{}/data/information.d".format(path), "w")
    of.close()
    main()
Copy after login

Finally finished writing, we can give it a try:

$python osnssh.py
Copy after login

The specific demonstration is that I put a GIF at the beginning of the article Animation picture source code group

If you encounter any problems during the learning process or want to obtain learning resources, welcome to join the learning exchange group
626062078, let’s learn Python together!

The above is the detailed content of Share an example of using Python to write an automatic SSH login to a remote server. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Can vscode run ipynb Can vscode run ipynb Apr 15, 2025 pm 07:30 PM

The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.

See all articles