Pages

Friday 13 July 2018

Remote SSH in Linux

In this case, we will learn how we can access from own linux server to other linux server with using except script and more practically method.
First method is except script;
#!/usr/bin/env expect
set username “EWH”
set password “EWH”
set password2 “****”
set host “1.1.1.1”           ## you can use more hosts and usernames/passwords

spawn ssh $username@$host    ## you can use “spawn” command to connect with ssh
expect “Password:”           ## in here, you say that i want to see “Password:” word after set above command.
send — “$password\r”
sleep 5                      ## set timeout
expect “cli>”
send — “shell\r”
expect “$”
send — “su -\r”
expect “Password:”
send — “$password2\r”
expect “root>”
send — “bash /home/check_syslog_EWH/check_syslog.sh\r”      ## you can do anything what you want with “send” command
sleep 5
expect “root>”
send — “exit\r”
interact
— spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no $username@$host  >> Also you can use “StrictHostKeyChecking” in here,
if set “yes”, that is meant to give some protection against attacks (trojan horse).
if set “no”, that meant new hosts will be automatically added to known_host file.
My recommendation that it should be “ask”. In this scenario hosts will be added automatically after confirmation.
–Instance for my recommendation;
root@anakin:/home/egemenulus# ssh 1.1.1.1
The authenticity of host ‘1.1.1.1 ()’ can’t be established.
RSA key fingerprint is 38:e0:e0:e0:e0:ae:fe:2e:30:e0:0e:bd:b3:e0:e0:21.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘1.1.1.1’ (RSA) to the list of known hosts.
root@1.1.1.1’s password:
Execute commands on server which remotely with ssh;
ssh $host ls
ssh $host ls; pwd; cat /path/to/remote/file
ssh $host < script.sh
But before remote connection without password,
you need to login using ssky-keygen and ssh-copy-id like this;
1-root@anakin:~# ssh-keygen
2-root@anakin:~# ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
3-root@anakin:~# ssh remote-host
–Also, you can remote login with single command included password and username;
first, you need to install sshpass
Install sshpass under Debian / Ubuntu Linux
Type the following command:
$ sudo apt-get install sshpass
Install sshpass under RHEL/CentOS Linux
First, enable EPEL repo and type the following yum command:
$ sudo yum install sshpass
If you are using Fedora Linux, type:
$ sudo dnf install sshpass
Install sshpass under FreeBSD Unix
To install the port, enter:
# cd /usr/ports/security/sshpass/ && make install clean
To add the package, run:
# pkg install sshpass
Login to ssh server called 1.1.1.1 with password called EWH:
$ sshpass -p ‘EWH’ ssh anakin@1.1.1.1
Check below info about how you install except and tcl:
For Ubuntu 10.04 (Trusty Tahr);
sudo apt-get update
sudo apt-get install expect
For Rhel;
1) Download the expect package from the below link
http://sourceforge.net/projects/expect/
2) Install the required dependecy packages “Tcl/Tk” language toolkit
# yum install tcl
3) Install the “expect” package using the below commands
# tar -zxvf expectx.xx.tar.gz
# ./configure
# make
# make install

Please do not hesitate to ask any question about remote ssh with using my e-mail.

No comments:

Post a Comment