You need expect scripting language to run this task.
-
In debian based distros run:
$ sudo apt install expect -
In MacOS:
$ brew install expect
- Create a file that will store your expect script:
$ touch ~/my_connection.exp
- Edit your file in your favorite editor:
#!/usr/bin/expect
set timeout 5
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh "$user\@$ip"
expect "Password:"
send "$password\r";
interact
- Modifiy permissions to your expect script:
$ chmod +x ~/my_connection.exp
- Run your script using arguments like this, use your own args:
$ ~/my_connection.exp 192.198.0.100 root p455w0rd
Avoid to expose your password exporting it from your .zshrc / .bashrc.
According to your $SHELL, use the proper file, in my case I'm using ZSH so I'll edit my ~/.zshrc.
- Export your password as an environment variable:
$ echo "export hid_pass='p455word'"
- Update your $Shell after every modification:
$ exec $SHELL or $ source ~/.zshrc
- Use your script without exposing your password:
$ ~/my_connection.exp 192.168.0.100 root $hid_pass
Create an alias to run the connection within preset args.
- Add your alias to your $SHELL (.zshrc / .bashrc):
$ echo "alias mycon='~/my_connection.exp 192.168.0.100 root $hid_pass'"
- Don't forget to update the $SHELL after every modification:
$ exec $SHELL or $ source ~/.zshrc
- Call the connection using your alias, and wait until the connection is done:
$ mycon