Advertisement
j0h

ts7600LoginScript

j0h
Sep 8th, 2023
1,515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import serial
  3. import time
  4.  
  5. # Configuration
  6. port = "/dev/ttyUSB0"
  7. baudrate = 115200
  8. login_prompt = "ts7600-4a9f81 login:"
  9. password_prompt = "Password:"
  10. success_prompt = "root@ts7600-4a9f81:~#"
  11. commands = ["uname -a"]
  12.  
  13. # Open the serial port
  14. ser = serial.Serial(port, baudrate, timeout=1)
  15.  
  16. def send_command(command, prompt):
  17.     ser.write(command.encode('utf-8') + b'\n')
  18.     ser.flush()
  19.     while True:
  20.         response = ser.readline().decode('utf-8')
  21.         print(response, end='')
  22.         if prompt in response:
  23.             break
  24.  
  25. try:
  26.     # Wait for the "login:" prompt
  27.     send_command('', login_prompt)
  28.  
  29.     # Enter the username
  30.     ser.write("root\n".encode('utf-8'))
  31.  
  32.     # Wait for the "Password:" prompt
  33.     send_command('', password_prompt)
  34.  
  35.     # Enter the password
  36.     ser.write("asd123\n".encode('utf-8'))
  37.  
  38.     # Check for successful login
  39.     response = ser.readline().decode('utf-8')
  40.     if success_prompt not in response:
  41.         print("Login failed.")
  42.         ser.close()
  43.     else:
  44.         print("Login successful.")
  45.  
  46.         # Execute commands
  47.         for command in commands:
  48.             send_command(command, success_prompt)
  49.  
  50.     # Close the serial port
  51.     ser.close()
  52.  
  53. except serial.SerialException as e:
  54.     print("Error: ", e)
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement