Bash, Linux

my minimal zshrc and bashrc with completion

Install grml-zsh-config from repository, if you don’t want it minimal.

More minimal version is posted in the following link.
https://tech.wildduck.xyz/post/minimal-zshrc/

## Aliases
alias ls='ls --color=auto'
alias vi='vim'
alias ssh='TERM=xterm-256color ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'

# Paths
export PATH=$PATH:~/bin

# Prompt
PROMPT=[%n@%m\ %c\]%(!.#.$)\

# History
HISTFILE=.zsh_history
HISTSIZE=1000
SAVEHIST=1000
setopt EXTENDED_HISTORY
setopt HIST_FIND_NO_DUPS
setopt INC_APPEND_HISTORY

# Keybind
# For the control code, open your terminal and press ctrl+v and press whatever key you want to assign.
# Keybind
bindkey '^[OC' forward-word
bindkey '^[Oc' forward-word
bindkey '^[[1;5C' forward-word
bindkey '^[OD' backward-word
bindkey '^[Od' backward-word
bindkey '^[[1;5D' backward-word
bindkey '^[[1~' beginning-of-line
bindkey '^[[7~' beginning-of-line
bindkey '^[OH' beginning-of-line
bindkey '\033[H' beginning-of-line
bindkey '^[[1;6D' beginning-of-line
bindkey '^[[8~' end-of-line
bindkey '^[[4~' end-of-line
bindkey '^[OF' end-of-line
bindkey '\033[F' end-of-line
bindkey '^[[1;6C' end-of-line
bindkey '^[[3~' delete-char

# Completion
autoload -Uz compinit
compinit
# Spelling correction
setopt correct
# Auto cd
setopt autocd

bashrc


# History
HISTTIMEFORMAT="%d/%m/%Y %T "

# Bash-completion options
shopt -s autocd
shopt -s cdspell
shopt -s extglob
shopt -s histappend
shopt -s lithist
shopt -s dotglob
shopt -s nocaseglob
shopt -s nocasematch

Bash

SSH Login Script

Bash(expect) script to login remote machines via ssh.
Append -L:”local port”:”Remote host”:Remote port” to do port fowarding.
Associate sh files with /bin/mintty.exe if you are using cygwin.

To one host
#!/bin/bash

host=host
name=hostname
user=user
pass=pass
log=/cygdrive/c/Users/Public/logs/"$name"_`date +%d_%h_%Y_%H%M%S`
noansi=/cygdrive/c/Users/Public/script/no_ansi.pl

expect -c "
spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null-l $user $host
expect password:
send $pass\n
interact
" | tee -a "$log"_raw.txt
perl $noansi "$log"_raw.txt > "$log".txt

Send commands to more than one host(telnet)
#!/bin/bash

hosts="host host1 host2"
user=user
pass=pass

for host in $hosts; do
expect -c "
spawn telnet \"$host\"
expect login:
send \"$user\n\"
expect Password:
send \"$pass\n\"
expect $
send \"ping 10.0.0.1\n\"
expect $
send \"exit\n\"
interact
"
done

Perl script to remove all control characters from recorded files by tee.
Taken from http://unix.stackexchange.com/questions/14684/removing-control-chars-including-console-codes-colours-from-script-output

mp_ansi.pl
#!/usr/bin/perl
use encoding 'shiftjis', STDIN=>'shiftjis', STDOUT=>'shiftjis';
while () {
s/ \e[ #%()*+\-.\/]. |
\r | # Remove extra carriage returns also
(?:\e\[|\x9b) [ -?]* [@-~] | # CSI ... Cmd
(?:\e\]|\x9d) .*? (?:\e\\|[\a\x9c]) | # OSC ... (ST|BEL)
(?:\e[P^_]|[\x90\x9e\x9f]) .*? (?:\e\\|\x9c) | # (DCS|PM|APC) ... ST
\e.|[\x80-\x9f] //xg;
1 while s/[^\b][\b]//g; # remove all non-backspace followed by backspace
print;
}