CODE BLOCK: SecureCRT Logon Scripts = Linux

Each week I aim to review a block of code, make improvements and present it to you here.

This week I’m reviewing my own SecureCRT Logon Script for Linux.

Please refer to my main article for information on these Logon scripts, such as installation, usage, and downloads:

Guide to Setting Up SecureCRT Logon Scripts-Here Are Mine! – 1System

This week I’ve built a Ubuntu 21.04 Virtual Machine for testing SSH access profiles in SecureCRT.

Below are the improved Linux scripts.

Summary of the My Scripts!

I have 3 scripts for each type of device I connect to.

  • Script 1: “Connect” – Session connects, runs basic status information, and presents the logon prompt
  • Script 2: “Backup” – cfg_backup – Session connects, runs basic status information, completes backup, collect routing, logoff
  • Script 3: “Custom” – save_cmds – Session connects, runs basic status information=>Run pre-configured command blocks-Backup, show tech, server operational status, etc=>Logoff
See them all below!

  • Script 1: “Connect” – Session connects, runs basic status information, and presents the logon prompt

    Filename: MultiSessionLogin-Linux.vbs

#$language = "VBScript"
#$interface = "1.0"

'~~~ SecureCRT VB logon script - Linux ~~~~~~~~~~~~~~~~~
' SecureCRT VB logon script for logging on to multiple servers
' AUTHOR: Brett Gardner - https://1system.online - LATEST RELEASE: 20210722
' CODE DISCLAIMER: https://1system.online/code-disclaimer/
' VENDOR & ORIGINAL BASE SCRIPT: VanDyke Software SecureCRT - https://www.vandyke.com/
' CUSTOMISED DEVICE TYPE: Check Point GAIA
' TESTED:
' Ubuntu 18.04.2 LTS
' Ubuntu 21.04

Sub Main()
    ' turn on synchronous mode
    crt.Screen.Synchronous = True

    ' login name
    crt.Screen.WaitForString "Username: "
    crt.Screen.Send "testuserid" & VbCr

    ' password
    crt.Screen.WaitForString ": "
    crt.Screen.Send "testpassword" & VbCr
		
	' Run login informative commands
	' Aim is to use built in generic tools
	crt.Screen.WaitForString "$ "
	crt.Screen.Send "PS1='$ '"
	crt.Screen.WaitForString "$ "
    crt.Screen.Send " " & VbCr
	crt.Screen.WaitForString "$ "
    crt.Screen.Send "PS1='===========================================================================" & VbCr
    crt.Screen.Send "$(whoami)@$(hostname):$(pwd):$ '" & VbCr
    crt.Screen.WaitForString "$ "
	crt.Sleep 50
    crt.Screen.Send "uname -n" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
    crt.Screen.Send "cat /proc/version" & VbCr
    crt.Screen.WaitForString "$ "
	crt.Sleep 50
    crt.Screen.Send "date" & VbCr
    crt.Screen.WaitForString "$ "
	crt.Sleep 50
    crt.Screen.Send "uptime" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "ifconfig -a | egrep 'Link|inet|UP'" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "netstat -rn |grep 0.0.0.0" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "top -b -n1 |head -10" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "df -kh | awk '0+$5 >= 85 {print}'" & VbCr ' Display partitions over 85% full
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "netstat -an |grep ESTABLISHED" & VbCr
	crt.Screen.WaitForString "$ "
    crt.Screen.Send "PS1='$(whoami)@$(hostname):$(pwd):$ '" & VbCr
    crt.Screen.WaitForString "$ "

	' Other commands that may work on some systems
		' htop 'not installed by default
		' iotop 'not installed by default
		' mpstat -P ALL 'not installed by default
		' iostat 'not installed by default
		' sar 'not installed by default
		' smartctl -a /dev/sda 'not installed by default
		' Ubuntu tool-Glances: sudo pip install Glances (Google instructions)

    ' turn off synchronous mode
    crt.Screen.Synchronous = False
End Sub

'~~~ SecureCRT VB logon script - Linux ~~~~~~~~~~~~~~~~~
  • Script 2: “Backup” – cfg_backup – Session connects, runs basic status information, completes backup, collect routing, logoff
Filename: MultiSessionLogin-Linux-config_backup.vbs
#$language = "VBScript"
#$interface = "1.0"

'~~~ SecureCRT VB logon script - Linux ~~~~~~~~~~~~~~~~~
' SecureCRT VB logon script for logging on to multiple servers
' AUTHOR: Brett Gardner - https://1system.online - LATEST RELEASE: 20210722
' CODE DISCLAIMER: https://1system.online/code-disclaimer/
' VENDOR & ORIGINAL BASE SCRIPT: VanDyke Software SecureCRT - https://www.vandyke.com/
' CUSTOMISED DEVICE TYPE: Check Point GAIA
' TESTED:
' Ubuntu 18.04.2 LTS
' Ubuntu 21.04

Sub Main()
    ' turn on synchronous mode
    crt.Screen.Synchronous = True

    ' login name
    crt.Screen.WaitForString "Username: "
    crt.Screen.Send "testuserid" & VbCr

    ' password
    crt.Screen.WaitForString ": "
    crt.Screen.Send "testpassword" & VbCr
	
	' Run login informative commands
	' Aim is to use built in generic tools
	crt.Screen.WaitForString "$ "
	crt.Screen.Send "PS1='$ '"
	crt.Screen.WaitForString "$ "
    crt.Screen.Send " " & VbCr
	crt.Screen.WaitForString "$ "
    crt.Screen.Send "PS1='===========================================================================" & VbCr
    crt.Screen.Send "$(whoami)@$(hostname):$(pwd):$ '" & VbCr
    crt.Screen.WaitForString "$ "
	crt.Sleep 50
    crt.Screen.Send "uname -n" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
    crt.Screen.Send "cat /proc/version" & VbCr
    crt.Screen.WaitForString "$ "
	crt.Sleep 50
    crt.Screen.Send "date" & VbCr
    crt.Screen.WaitForString "$ "
	crt.Sleep 50
    crt.Screen.Send "uptime" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "ifconfig -a | egrep 'Link|inet|UP'" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "netstat -rn |grep 0.0.0.0" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "top -b -n1 |head -10" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "df -kh | awk '0+$5 >= 85 {print}'" & VbCr ' Display partitions over 85% full
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "netstat -an |grep ESTABLISHED" & VbCr
	crt.Screen.WaitForString "$ "

	' Routing
	crt.Screen.Send "netstat -rn" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "ip route" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50

' --- Backup-Ubuntu 21.04 -------------------------------------
' --- Backups are very much dependent on the individual systems, so please test before running
' -------------------------------------------------------------
	' declare variable
	'datet=Year(date) & Month(date) & Day(date)
	curMonth=right("00" & month(now),2)
	datet=Year(date) & curMonth & Day(date)

    crt.Screen.WaitForString "$ "
    crt.Screen.Send "lsb_release -d" & VbCr
	crt.Screen.WaitForString "$ "
	
	screenrow = crt.screen.CurrentRow - 2
	Dim servertype
	servertype = crt.Screen.Get(screenrow, 1, screenrow, 22 )
	'servertype = Trim(crt.Screen.Get(row, 1, row, 22)) ' Trim command also works
	'MsgBox servertype 'Keep for testing

	' Execute backup if the system is Ubuntu
    If Left(servertype, 22) = "Description:    Ubuntu" Then

		' Run Backup
		' https://askubuntu.com/questions/9135/how-to-backup-settings-and-list-of-installed-packages
		crt.Screen.Send "mkdir ~/backup-" & datet & VbCr
		crt.Screen.WaitForString "$ "
		crt.Sleep 50
		crt.Screen.Send "cd ~/backup-" & datet & VbCr
		crt.Screen.WaitForString "$ "
		crt.Sleep 50
		crt.Screen.Send "dpkg --get-selections > ~/backup-" & datet & "/Package.list" & VbCr
		crt.Screen.WaitForString "$ "
		crt.Sleep 50
		crt.Screen.Send "sudo cp -R /etc/apt/sources.list* ~/backup-" & datet & VbCr
		crt.Screen.WaitForString ": "
	    crt.Screen.Send "testpassword" & VbCr
		crt.Screen.WaitForString "$ "
		crt.Sleep 50
		' Backup your keys if you have them
			'crt.Screen.Send "sudo cp -R ~/.gnupg ~/backup-" & datet & "/gnupg" & VbCr
			'crt.Screen.WaitForString ": "
			'crt.Screen.Send "testpassword" & VbCr
			'crt.Screen.WaitForString "$ "
			'crt.Sleep 50
		' Setup your rsync folder to automatically backup your files to another location
			'rsync --progress /home/`whoami` /path/to/user/profile/backup/here
		
		'##  Reinstall now
		'rsync --progress /path/to/user/profile/backup/here /home/`whoami`
		'sudo apt-key add ~/Repo.keys
		'sudo cp -R ~/sources.list* /etc/apt/
		'sudo apt-get update
		'sudo apt-get install dselect
		'sudo dpkg --set-selections < ~/Package.list
		'sudo dselect
	
	End If ' End Conditional exection
	
' --- Backup-Ubuntu 21.04 -------------------------------------

	' Exit
    crt.Screen.Send "exit" & VbCr

    ' turn off synchronous mode
    crt.Screen.Synchronous = False
End Sub

'~~~ SecureCRT VB logon script - Linux ~~~~~~~~~~~~~~~~~
  • Script 3: “Custom” – save_cmds – Session connects, runs basic status information=>Run pre-configured command blocks-Backup, show tech, server operational status, etc=>Logoff
Filename: MultiSessionLogin-Linux-save_cmds.vbs
#$language = "VBScript"
#$interface = "1.0"

'~~~ SecureCRT VB logon script - Linux ~~~~~~~~~~~~~~~~~
' SecureCRT VB logon script for logging on to multiple servers
' AUTHOR: Brett Gardner - https://1system.online - LATEST RELEASE: 20210722
' CODE DISCLAIMER: https://1system.online/code-disclaimer/
' VENDOR & ORIGINAL BASE SCRIPT: VanDyke Software SecureCRT - https://www.vandyke.com/
' CUSTOMISED DEVICE TYPE: Check Point GAIA
' TESTED:
' Ubuntu 18.04.2 LTS
' Ubuntu 21.04

Sub Main()
    ' turn on synchronous mode
    crt.Screen.Synchronous = True

    ' login name
    crt.Screen.WaitForString "Username: "
    crt.Screen.Send "testuserid" & VbCr

    ' password
    crt.Screen.WaitForString ": "
    'crt.Screen.Send "M1Staple4$" & VbCr
    crt.Screen.Send "testpassword" & VbCr
	
	' Run login informative commands
	' Aim is to use built in generic tools
	crt.Screen.WaitForString "$ "
	crt.Screen.Send "PS1='$ '"
	crt.Screen.WaitForString "$ "
    crt.Screen.Send " " & VbCr
	crt.Screen.WaitForString "$ "
    crt.Screen.Send "PS1='===========================================================================" & VbCr
    crt.Screen.Send "$(whoami)@$(hostname):$(pwd):$ '" & VbCr
    crt.Screen.WaitForString "$ "
	crt.Sleep 50
    crt.Screen.Send "uname -n" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
    crt.Screen.Send "cat /proc/version" & VbCr
    crt.Screen.WaitForString "$ "
	crt.Sleep 50
    crt.Screen.Send "date" & VbCr
    crt.Screen.WaitForString "$ "
	crt.Sleep 50
    crt.Screen.Send "uptime" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "ifconfig -a | egrep 'Link|inet|UP'" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "netstat -rn |grep 0.0.0.0" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "top -b -n1 |head -10" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "df -kh | awk '0+$5 >= 85 {print}'" & VbCr ' Display partitions over 85% full
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "netstat -an |grep ESTABLISHED" & VbCr
	crt.Screen.WaitForString "$ "
	
	' Routing & arp
	crt.Screen.Send "netstat -rn" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "ip route" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "arp -an" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	
	' Run commands you wish to run
	
    ' Check memory usage - Free > 15%
	crt.Screen.Send "free -ht" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "vmstat -s" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
    crt.Screen.Send "top -n 1" & VbCr
	crt.Screen.WaitForString "$ "
	' Check disk space not over 90%
	crt.Screen.Send "df -kh" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	' Check runing processes
	' Check for hung sessions
	crt.Screen.Send "ps -ef" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	' Other commands
	crt.Screen.Send "cat /proc/cpuinfo" & VbCr
	crt.Screen.WaitForString "$ "
	crt.Sleep 50
	crt.Screen.Send "cat /proc/meminfo" & VbCr
	crt.Screen.WaitForString "$ "

	' Exit
    crt.Screen.Send "exit" & VbCr

    ' turn off synchronous mode
    crt.Screen.Synchronous = False
End Sub

'~~~ SecureCRT VB logon script - Linux ~~~~~~~~~~~~~~~~~

Please let me know what you think!  Suggest improvements or join my site to get future updates.

This Post Has 3 Comments

  1. eldiablo

    Great instructions! keep it up, love your content !

  2. Brett Gardner

    Thanks ElDiablo. Look forward to bringing you more content. Let me know if you have any request for content!

  3. Brett

    For this command “top -1 -b -n1 |head -10”, I removed the “-1”. This displays each cpu’s details in Ubuntu (and most Linux’s I’ve used). In Redhat Linux, this doesn’t work, so the command errors. For a quick summary, this is probably excessive anyway.

Leave a Reply