> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/internal-and-external-network-sec/enumeration/unstable-shell-to-interactive-shell.md).

# Unstable Shell to Interactive Shell

### **In Bash Shell**

#### **Step 1: Spawn a PTY Shell using Python**

If `python` is not working, use `python3`.

```bash
python -c 'import pty; pty.spawn("/bin/bash")'
```

OR

```bash
python3 -c 'import pty; pty.spawn("/bin/bash")'
```

#### **Step 2: Move the Shell to the Background**

Press **Ctrl + Z** to suspend the shell.

```bash
Ctrl + Z
```

#### **Step 3: Check Terminal and STTY Settings**

Run the following commands to get terminal type and size:

```bash
echo $TERM
stty -a
```

* **TERM type:** Example → `xterm-256color`
* **TTY size:** Example → `rows 37; columns 146`

#### **Step 4: Set the STTY to Raw Mode and Disable Echo**

```bash
stty raw -echo
```

#### **Step 5: Bring Shell to Foreground and Reset Terminal**

```bash
fg
reset
Ctrl + D
```

#### **Step 6: Match STTY Size to Terminal Window**

```bash
export SHELL=bash
export TERM=xterm256-color
stty rows 37 columns 146
bash -i
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export TERM=xterm
export SHELL=bash
cat /etc/profile; cat /etc/bashrc; cat ~/.bash_profile; cat ~/.bashrc; cat ~/.bash_logout; env; set
export PS1='[\u@\h \W]\$ '
```

***

### **In Zsh Shell**

#### **Step 1: Spawn a PTY Shell using Python**

```bash
python -c 'import pty; pty.spawn("/bin/sh")'
# OR
python3 -c 'import pty; pty.spawn("/bin/bash")'
```

#### **Step 2: Move the Shell to Background**

```bash
Ctrl + Z
```

#### **Step 3: Get Terminal Size**

```bash
stty -a | head -n1 | cut -d ';' -f 2-3 | cut -b2- | sed 's/;  /\n/'
```

#### **Step 4: Set Terminal to Raw Mode and Bring Shell to Foreground**

```bash
stty raw -echo
fg
reset
Ctrl + D
```

#### **Step 5: Match STTY Size and Export Environment Variables**

```bash
export SHELL=bash
export TERM=xterm256-color
stty rows 41 columns 173
bash -i
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export TERM=xterm
export SHELL=bash
cat /etc/profile; cat /etc/bashrc; cat ~/.bash_profile; cat ~/.bashrc; cat ~/.bash_logout; env; set
export PS1='[\u@\h \W]\$ '
```

***

### Getting Stable Shell Windows PC&#x20;

* **Use rlwrap**

```bash
sudo apt install rlwrap
```

```
rlwrap nc -nlvp 443
```
