Home > Openvz and Virtuozzo, Scripts > Script for ssh login to Virtuozzo vps using expect and bash

Script for ssh login to Virtuozzo vps using expect and bash



OBJECTIVE: Automate login to Virtuozzo vps


1) Linux Virtuozzo vps IP address
2) User’s password of node.


1) Package for ‘expect’ application is installed in your local machine
2) The linux vps is running
3) The USERNAME variable in bash script is set as “username”. Change it to your Virtuozzo node username which is used for logging in.
4) Both scripts “smtpvpscheck.sh” and “expect.ex” are put under the same directory.


1) The bash script accepts the linux VPS ip address as input. From vps ip it finds the node ip using ‘mtr’ command. After that it requests the users password to be entered. Here, the username is hard coded to the script “smtpvpscheck.sh”. If you want to change it to yours, then modify it directly in the script.


At the final section of the bash script, it calls the expect script “expect.ex” and pass the node’s ip, username(for ssh), password and the VPS ip to it.



Bash script: smtpvpscheck.sh



if [ $# -ne 1 ];then 
 echo "Usage: smtpvpscheck.sh "
 exit 1


NODEIP=`mtr -nr ${VPSIP}|tac|sed -n 2p|awk '{print $2}'`

read -p "Enter node password:" -s PWD

expect -f expect.ex $NODEIP $USERNAME $PWD $VPSIP

unset PWD


2) The expect script “expect.ex“, logs into the node using the username and password(passed on as arguments) and then switches as root user(using sudo su). After that from the VPS ip address it finds out the container ID(CTID) and then enters to it using “vzctl” command.



Expect script: expect.ex

log_user 0
set NODEIP [lrange $argv 0 0]
set USERNAME [lrange $argv 1 1] 
set PWD [lrange $argv 2 2]
set VPSIP [lrange $argv 3 3]

spawn ssh ${USERNAME}@${NODEIP}
expect "password"
send -- "${PWD}\r"
expect "${USERNAME}@"
send -- "sudo su\r"
expect "password" { send -- "${PWD}\r" }
expect "root@"
#send -- "/usr/sbin/vzctl enter `/usr/sbin/vzlist -o ctid,ip|grep ${VPSIP}|sed "s/\([0-9]\{1,9\}\).*/\1/"|sed -e "s/^\s*//"`\r"
send -- "/usr/sbin/vzctl enter `grep -il ${VPSIP} /etc/vz/conf/*.conf|cut -d\/ -f5|cut -d\. -f1`\r"
expect "CT-14748-bash"
send -- "\r\n"



reynoldp@w10:~/scripts/$ ./smtpvpscheck.sh
Enter node password:





Normally for troubleshooting SMTP spam abuses we first find the node of vps, then logs in to the node and then from there we switch to root user and then we find out the vps CTID from ip address and finally enters to the CTID using “vzctl” command. This will typically take some minutes, and using this script this time frame is reduced to a minute or two.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: