I have logined to a CentOS machine using below Python script:
import paramiko
username = input('Username: ')
password = input('Password: ')
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='192.168.30.129',username=username,password=password,look_for_keys=False)
remote_connection = ssh_client.invoke_shell()
remote_connection.send("mkdir test\n")
remote_connection.send("vi test.txt\n")
remote_connection.send("test\n")
remote_connection.send(":wq\n")
output = remote_connection.recv(65535).decode('ascii')
print (output)
ssh_client.close
What I did with this script:
- Login to the CentOS machine using paramiko
- Create a folder called 'test'
- Create a text file called 'test.txt' using vi and type 'test' in the file.
- Type ':wq' in order to save the change I made in 'test.txt' and quit vi.
Step 1 to 3 work perfectly for me, but I couldn't get step 4 done because I don't how to hit "esc" button to enter the Normal Mode of Vi before I'm able to type :wq.
So my question is: How to enter "ESC" key in a Python script?