Skip to content

Oueta

/* Nifty tech journal */

  • Home
  • MAC address lookup
  • PCI database lookup
  • Unix time converter
  • My IP Address
  • Linux Commands Cheat Sheet

Posts

Posted on September 11, 2017August 11, 2021 by oueta

Telnet auto login on Linux and Unix with Expect

Telnet is obsolete because it’s insecure, an attacker that is eavesdropping your network traffic can easily steal your credentials, because telnet it’s a text-based protocol without encryption, your username and password will be sent in clear text over the network. If it’s available, always use SSH! (/linux/ssh-auto-login-on-linux-and-unix/), but if you must use telnet create and use the script from below.

Tested operating system:  Debian 9.1, CentOS 7.3, FreeBSD 11.1 and macOS 10.12


1. Check where is Expect installed, on FreeBSD 11.1  the PATH is /usr/local/bin/expect.

whereis expect

2. Create telnet.sh script and modify the username and password

#!/usr/bin/expect -f
if {[llength $argv] < 1} {
    puts "Usage: ./telnet.sh host";
    exit 1;
}
set timeout 10
set host [lindex $argv 0]
set user "my_user"
set password "my_password"
spawn telnet -l $user $host
expect {
    "?ser*" {
        send "$user\n"
        exp_continue
    }
    "?ogin*" {
        send "$user\n"
        exp_continue
    }
    "?assword*" {
        send "$password\n"
        interact
        exit 0;
    }
}
exit 1

3. Change permission and execute.

chmod 700 telnet.sh
./telnet.sh host

Warning: Keep your scripts in a safe place! Anyone who has access to your scripts can steal your credentials!

Posted on September 11, 2017August 11, 2021 by oueta

SSH auto login on Linux and Unix

Security it’s important, a host secured with a strong password is required, but sometimes we need to use other kind of authentification methods, in this article we will walk-through public and private key pair authentification and auto login with Expect script.

Tested operating systems:  Debian 9.1, CentOS 7.3, FreeBSD 11.1 and macOS 10.12


Method 1 – Public and private key pair authentification

1. Generate RSA public and private key pair on the client host, ~/.ssh/id_rsa private and ~/.ssh/id_rsa.pub public key will be generated.

ssh-keygen -t rsa -f ~/.ssh/id_rsa -N ''

2. Append the public key to the ~/.ssh/authorized_keys file stored on the remote host through SSH

cat ~/.ssh/id_rsa.pub | ssh remote_user@remote_host 'cat >> ~/.ssh/authorized_keys'

Ready! Now you can login into remote_user@remote_host without password.

(Optional) If multiple private keys are used for different servers, we need to generate multiple key pairs and create ~/.ssh/config file.

ssh-keygen -t rsa -f ~/.ssh/id_rsa_remote_host1 -N ''
ssh-keygen -t rsa -f ~/.ssh/id_rsa_remote_host2 -N ''

cat ~/.ssh/id_rsa_remote_host1.pub | ssh remote_user@remote_host1 'cat >> ~/.ssh/authorized_keys'
cat ~/.ssh/id_rsa_remote_host2.pub | ssh remote_user@remote_host2 'cat >> ~/.ssh/authorized_keys'

echo "Host remote_host1" >> ~/.ssh/config
echo "Hostname remote_host1_ip_address" >> ~/.ssh/config
echo "IdentityFile ~/.ssh/id_rsa_remote_host1" >> ~/.ssh/config

echo "Host remote_host2" >> ~/.ssh/config
echo "Hostname remote_host2_ip_address" >> ~/.ssh/config
echo "IdentityFile ~/.ssh/id_rsa_remote_host2" >> ~/.ssh/config

ssh remote_user@remote_host1
ssh remote_user@remote_host2

Method 2 – Auto login with Expect script

1. Make sure that you have installed Expect and the PATH from the first line of the script it’s correct for example on FreeBSD 11.1 the PATH is /usr/local/bin/expect.

whereis expect

2. Create ssh.sh script with your favorite text editor and modify the user, password and port.

#!/usr/bin/expect -f
if {[llength $argv] < 1} {
    puts "Usage: ./ssh.sh host";
    exit 1;
}
set timeout 10
set host [lindex $argv 0]
set user "my_user"
set password "my_password"
spawn ssh $user@$host -p 22
expect {
        "yes/no*" {
                send "yes\n"
                exp_continue
       }
        "?assword*" {
            send "$password\n"
            interact
            exit 0;
       }
}
exit 1

3. Change permission and execute.

chmod 700 ssh.sh
./ssh.sh host

Warning: Keep your private keys and scripts in secure places! Anyone who can read your files will be able to login into your hosts!

Posts navigation

Previous page Page 1 … Page 22 Page 23 Page 24 … Page 29 Next page

Categories

  • Cross Platform (1)
  • Kubernetes (7)
  • Linux (34)
    • Commands (4)
    • Docker (1)
    • Ethtool (1)
    • Networking (5)
    • NetworkManager (2)
    • Operating System (6)
    • Services (4)
    • Shell (1)
    • SSH (4)
    • Utilities (1)
  • macOS (3)
  • Microsoft (9)
  • Networking (1)
  • Unix (2)
  • VirtualBox (1)
  • Windows Driver (2)
  • Windows Third Party (3)
  • Windows Tweak (1)
  • Wordpress (2)

Recent Posts

  • Add Google reCAPTCHAv3 to WordPress comments without plugin
  • Google Drive on Linux with rclone
  • Using multiple kubeconfig files and how to merge to a single
  • Understanding Kubernetes ConfigMap with examples
  • Backup and restore etcd of a kubernetes cluster
  • Create kubeconfig with “normal user” and x509 client certificates
  • Create kubernetes cluster “The hard way”
  • Create a CentOS 8 or Rocky Linux 8 kubernetes cluster with kubeadm
  • How to connect to Wifi in Linux with nmcli
  • Check and enable wake on lan in Linux
Free typing test
  • Twitter
  • Email
Privacy Policy Proudly powered by WordPress
  • About
  • Privacy Policy
  • Terms and Conditions