script with telnet write is not functioning












1














I have this program that access devices and change its hostname based from csv file but after entering the user/password script doesn't write any or seem like its tn.write is not running.



While logs from device shows that the python script can access the device. Also no error when running this script.



import csv
import telnetlib
import getpass
import sys
import time

##from prettytable import PrettyTable
def main():


#open csv file, then put to variable csvfile.
with open(input("Input the CSV filename: ")) as csvfile:
riphostCSV = csv.reader(csvfile, delimiter=',')

#Put the data as list from excel.
ipaddress =
nhostname =
## Menu = PrettyTable()
## Menu.field_names=['IPadd','Nhostname']

#Action - Put the data from excel to variable[list]
for col in riphostCSV:
ipadr = col[0]
nhost = col[1]

ipaddress.append(ipadr)
nhostname.append(nhost)

#List of devices from CSV file
print("LIST OF DEVICES")
for i in ipaddress:
print(i, nhostname[ipaddress.index(i)])

dev = ipaddress
print ("Host: ",dev)
user = input("Enter your username: ")
password=getpass.getpass("Enter Your Password Here: ")
## password = getpass.getpass()

print ("Now accessing the devices " + i)
dev = i.strip()
tn = telnetlib.Telnet(dev)
print("host:",dev)
tn.read_until(b"Username:")
tn.write(user.encode("ascii") + b"n")

if password:
tn.read_until(b"Password: ")
tn.write(password.encode("ascii") + b"n")
tn.read_until(b"#")
tn.write(b"conf t n")
time.sleep(1)
tn.write(b"hostname test33 n")
tn.write(b"exit n")
tn.write(b"wr mem n")
## tn.close()
## break
main()


OUTPUT



nput the CSV filename: iphost.csv
LIST OF DEVICES
192.168.137.50 lab-sw01
Host: ['192.168.137.50']
Enter your username: cisco



Warning (from warnings module):
File "C:UsersGlobalNOCAppDataLocalProgramsPythonPython37-32libgetpass.py", line 100
return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Enter Your Password Here: cisco
Now accessing the devices 192.168.137.50
host: 192.168.137.50











Thanks










share|improve this question



























    1














    I have this program that access devices and change its hostname based from csv file but after entering the user/password script doesn't write any or seem like its tn.write is not running.



    While logs from device shows that the python script can access the device. Also no error when running this script.



    import csv
    import telnetlib
    import getpass
    import sys
    import time

    ##from prettytable import PrettyTable
    def main():


    #open csv file, then put to variable csvfile.
    with open(input("Input the CSV filename: ")) as csvfile:
    riphostCSV = csv.reader(csvfile, delimiter=',')

    #Put the data as list from excel.
    ipaddress =
    nhostname =
    ## Menu = PrettyTable()
    ## Menu.field_names=['IPadd','Nhostname']

    #Action - Put the data from excel to variable[list]
    for col in riphostCSV:
    ipadr = col[0]
    nhost = col[1]

    ipaddress.append(ipadr)
    nhostname.append(nhost)

    #List of devices from CSV file
    print("LIST OF DEVICES")
    for i in ipaddress:
    print(i, nhostname[ipaddress.index(i)])

    dev = ipaddress
    print ("Host: ",dev)
    user = input("Enter your username: ")
    password=getpass.getpass("Enter Your Password Here: ")
    ## password = getpass.getpass()

    print ("Now accessing the devices " + i)
    dev = i.strip()
    tn = telnetlib.Telnet(dev)
    print("host:",dev)
    tn.read_until(b"Username:")
    tn.write(user.encode("ascii") + b"n")

    if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode("ascii") + b"n")
    tn.read_until(b"#")
    tn.write(b"conf t n")
    time.sleep(1)
    tn.write(b"hostname test33 n")
    tn.write(b"exit n")
    tn.write(b"wr mem n")
    ## tn.close()
    ## break
    main()


    OUTPUT



    nput the CSV filename: iphost.csv
    LIST OF DEVICES
    192.168.137.50 lab-sw01
    Host: ['192.168.137.50']
    Enter your username: cisco



    Warning (from warnings module):
    File "C:UsersGlobalNOCAppDataLocalProgramsPythonPython37-32libgetpass.py", line 100
    return fallback_getpass(prompt, stream)
    GetPassWarning: Can not control echo on the terminal.
    Warning: Password input may be echoed.
    Enter Your Password Here: cisco
    Now accessing the devices 192.168.137.50
    host: 192.168.137.50











    Thanks










    share|improve this question

























      1












      1








      1







      I have this program that access devices and change its hostname based from csv file but after entering the user/password script doesn't write any or seem like its tn.write is not running.



      While logs from device shows that the python script can access the device. Also no error when running this script.



      import csv
      import telnetlib
      import getpass
      import sys
      import time

      ##from prettytable import PrettyTable
      def main():


      #open csv file, then put to variable csvfile.
      with open(input("Input the CSV filename: ")) as csvfile:
      riphostCSV = csv.reader(csvfile, delimiter=',')

      #Put the data as list from excel.
      ipaddress =
      nhostname =
      ## Menu = PrettyTable()
      ## Menu.field_names=['IPadd','Nhostname']

      #Action - Put the data from excel to variable[list]
      for col in riphostCSV:
      ipadr = col[0]
      nhost = col[1]

      ipaddress.append(ipadr)
      nhostname.append(nhost)

      #List of devices from CSV file
      print("LIST OF DEVICES")
      for i in ipaddress:
      print(i, nhostname[ipaddress.index(i)])

      dev = ipaddress
      print ("Host: ",dev)
      user = input("Enter your username: ")
      password=getpass.getpass("Enter Your Password Here: ")
      ## password = getpass.getpass()

      print ("Now accessing the devices " + i)
      dev = i.strip()
      tn = telnetlib.Telnet(dev)
      print("host:",dev)
      tn.read_until(b"Username:")
      tn.write(user.encode("ascii") + b"n")

      if password:
      tn.read_until(b"Password: ")
      tn.write(password.encode("ascii") + b"n")
      tn.read_until(b"#")
      tn.write(b"conf t n")
      time.sleep(1)
      tn.write(b"hostname test33 n")
      tn.write(b"exit n")
      tn.write(b"wr mem n")
      ## tn.close()
      ## break
      main()


      OUTPUT



      nput the CSV filename: iphost.csv
      LIST OF DEVICES
      192.168.137.50 lab-sw01
      Host: ['192.168.137.50']
      Enter your username: cisco



      Warning (from warnings module):
      File "C:UsersGlobalNOCAppDataLocalProgramsPythonPython37-32libgetpass.py", line 100
      return fallback_getpass(prompt, stream)
      GetPassWarning: Can not control echo on the terminal.
      Warning: Password input may be echoed.
      Enter Your Password Here: cisco
      Now accessing the devices 192.168.137.50
      host: 192.168.137.50











      Thanks










      share|improve this question













      I have this program that access devices and change its hostname based from csv file but after entering the user/password script doesn't write any or seem like its tn.write is not running.



      While logs from device shows that the python script can access the device. Also no error when running this script.



      import csv
      import telnetlib
      import getpass
      import sys
      import time

      ##from prettytable import PrettyTable
      def main():


      #open csv file, then put to variable csvfile.
      with open(input("Input the CSV filename: ")) as csvfile:
      riphostCSV = csv.reader(csvfile, delimiter=',')

      #Put the data as list from excel.
      ipaddress =
      nhostname =
      ## Menu = PrettyTable()
      ## Menu.field_names=['IPadd','Nhostname']

      #Action - Put the data from excel to variable[list]
      for col in riphostCSV:
      ipadr = col[0]
      nhost = col[1]

      ipaddress.append(ipadr)
      nhostname.append(nhost)

      #List of devices from CSV file
      print("LIST OF DEVICES")
      for i in ipaddress:
      print(i, nhostname[ipaddress.index(i)])

      dev = ipaddress
      print ("Host: ",dev)
      user = input("Enter your username: ")
      password=getpass.getpass("Enter Your Password Here: ")
      ## password = getpass.getpass()

      print ("Now accessing the devices " + i)
      dev = i.strip()
      tn = telnetlib.Telnet(dev)
      print("host:",dev)
      tn.read_until(b"Username:")
      tn.write(user.encode("ascii") + b"n")

      if password:
      tn.read_until(b"Password: ")
      tn.write(password.encode("ascii") + b"n")
      tn.read_until(b"#")
      tn.write(b"conf t n")
      time.sleep(1)
      tn.write(b"hostname test33 n")
      tn.write(b"exit n")
      tn.write(b"wr mem n")
      ## tn.close()
      ## break
      main()


      OUTPUT



      nput the CSV filename: iphost.csv
      LIST OF DEVICES
      192.168.137.50 lab-sw01
      Host: ['192.168.137.50']
      Enter your username: cisco



      Warning (from warnings module):
      File "C:UsersGlobalNOCAppDataLocalProgramsPythonPython37-32libgetpass.py", line 100
      return fallback_getpass(prompt, stream)
      GetPassWarning: Can not control echo on the terminal.
      Warning: Password input may be echoed.
      Enter Your Password Here: cisco
      Now accessing the devices 192.168.137.50
      host: 192.168.137.50











      Thanks







      python python-3.x network-programming






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 28 '18 at 11:04









      searching1searching1

      194




      194
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53957507%2fscript-with-telnet-write-is-not-functioning%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53957507%2fscript-with-telnet-write-is-not-functioning%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas