i need to write a script for telnet automation .. so i wrote a program but i am unable to change the directory using telnet

StackOverflow https://stackoverflow.com/questions/23493798

Question

according to my code ( it takes host from command line argument)

    #!usr/bin/python
    import sys
    import telnetlib
    import subprocess
    import os
    import commands

    user = "test"
    password = "test"
    tn = telnetlib.Telnet(sys.argv[1])

    tn.read_until("login: ")
    tn.write(user + "\n")
    tn.read_until("Password: ")
    tn.write(password + "\n")
    pipe = os.popen("pwd","w") 
    print pipe

    pipe3 = os.popen("cd /tftpboot/")
    print pipe3
    pipe1 = os.popen("pwd","w")
    print pipe1
    tn.write("exit\n")
    print "exit"

here the output for pipe is "/" and for pipe1 is also "/" unable to change directory through this script.

Was it helpful?

Solution

os.popen("cd ....") affect only the subprocess. Instead use os.chdir:

os.chdir("/tftpboot")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top