通过telnet进行各种连接,有/无密码

时间:2015-11-04 20:51:33

标签: python telnet telnetlib

import telnetlib
import os
import re

current_dir = os.getcwd()
IP = <IP>

def login_as_root():

    print "logging in as root..."
    tn.read_until("login: ")
    tn.write("root\r")
    response = tn.expect([re.compile(b"Password:"),b"root@",])
    if "Password:" in response[2]:
        send_msg("root", "root@")
    else:
        print "logging without passwd"

def send_msg(msg, wait_str, print_msg=0):
    if print_msg:
        print msg
    tn.write("%s\n" % msg)
    return tn.read_until(wait_str)

def launch_update():
    try:
        print send_msg("mount -o remount,rw /", "root@ ", print_msg=1)
    except:
        Exception("Update configuring is failed")

def main():
    global tn
    try:
        tn = telnetlib.Telnet(IP)
        login_as_root()
    except:
        raise Exception("Connect failed")

    launch_update()

if __name__ == "__main__":
    main()

需要login_as_root()的帮助。以root身份登录有2种情况:使用密码且没有任何密码。我必须正确连接到主机,然后在launch_update()中启动几个命令。 mount命令后脚本卡住了。

0 个答案:

没有答案