安装golang.org/x

直接去github上面,把https://github.com/zieckey/golang.org,把整个目录拷贝下来放到你的gopath下面即可。记住在gopath的src下面,一定是golang.org/x/... 

使用

package main

import (
"fmt"
"golang.org/x/crypto/ssh"
"log"
"net"
"time"
) //连接的配置
type ClientConfig struct {
Host string //ip
Port int64 // 端口
Username string //用户名
Password string //密码
Client *ssh.Client //ssh client
LastResult string //最近一次运行的结果
} func (cliConf *ClientConfig) createClient(host string, port int64, username, password string) {
var (
client *ssh.Client
err error
)
cliConf.Host = host
cliConf.Port = port
cliConf.Username = username
cliConf.Password = password
cliConf.Port = port //一般传入四个参数:user,[]ssh.AuthMethod{ssh.Password(password)}, HostKeyCallback,超时时间,
config := ssh.ClientConfig{
User: cliConf.Username,
Auth: []ssh.AuthMethod{ssh.Password(password)},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
Timeout: 10 * time.Second,
}
addr := fmt.Sprintf("%s:%d", cliConf.Host, cliConf.Port) //获取client
if client, err = ssh.Dial("tcp", addr, &config); err != nil {
log.Fatalln("error occurred:", err)
} cliConf.Client = client
} func (cliConf *ClientConfig) RunShell(shell string) string {
var (
session *ssh.Session
err error
) //获取session,这个session是用来远程执行操作的
if session, err = cliConf.Client.NewSession(); err != nil {
log.Fatalln("error occurred:", err)
} //执行shell
if output, err := session.CombinedOutput(shell); err != nil {
log.Fatalln("error occurred:", err)
} else {
cliConf.LastResult = string(output)
}
return cliConf.LastResult
} func main() {
cliConf := new(ClientConfig)
cliConf.createClient("xxxx.xxx.xx.xx", 22, "root", "xxxxxxxxx")
/*
可以看到我们这里每次执行一条命令都会创建一条session
这是因为一条session默认只能执行一条命令
并且两条命令不可以分开写
比如:
cliConf.RunShell("cd /opt")
cliConf.RunShell("ls")
这两条命令是无法连续的,下面的ls查看的依旧是~目录
因此我们可以连着写,使用;分割
*/
fmt.Println(cliConf.RunShell("cd /opt; ls -l"))
/*
total 20
drwxr-xr-x 3 root root 4096 Nov 18 14:05 hadoop
drwxr-xr-x 3 root root 4096 Nov 18 14:20 hive
drwxr-xr-x 3 root root 4096 Nov 18 15:07 java
drwxr-xr-x 3 root root 4096 Nov 4 23:01 kafka
drwxr-xr-x 3 root root 4096 Nov 4 22:54 zookeeper
*/
}

golang使用ssh远程连接服务器并执行命令的更多相关文章

  1. RSA加密算法和SSH远程连接服务器

    服务器端与客户端的密钥系统不一样,称为非对称式密钥系统 RSA算法的基础是模运算x mod n,事实上: [(a mod n) + (b mod n)] mod n = (a+b) mod n [(a ...

  2. Python通过ssh连接服务器并执行命令

    [本文出自天外归云的博客园] 脚本示例如下: # coding:utf-8 import time,paramiko,re,StringIO def exec_shell(command): ''' ...

  3. expect远程登录服务器并执行命令

    #!/usr/bin/expectset timeout 120            #设置执行超时时间,任何输入120秒后退出set password "password"  ...

  4. 使用VBS实现SSH远程登录并自动执行命令

    set ws=createobject("wscript.shell")ws.run "Putty所在路径\putty.exe -ssh -pw 你的密码 用户名@192 ...

  5. mac 通过 终端 ssh 远程连接 centos 服务器

    mac 通过 终端 ssh 远程连接 centos 服务器 在终端下输入 ssh -l root 204.74.*.*      就可以连接了,这是端口没变的情况,还是原来的22 ssh -p 448 ...

  6. 全新 Mac 安装指南(编程篇)(环境变量、Shell 终端、SSH 远程连接)

    注:本文专门用于指导对计算机编程与设计(尤其是互联网产品开发与设计)感兴趣的 Mac 新用户,如何在 Mac OS X 系统上配置开发与上网环境,另有<全新 Mac 安装指南(通用篇)>作 ...

  7. 远程连接Kali Linux使用PuTTY实现SSH远程连接

    远程连接Kali Linux使用PuTTY实现SSH远程连接 本书主要以在Android设备上安装的Kali Linux操作系统为主,介绍基于Bash Shell渗透测试.由于在默认情况下,在Andr ...

  8. SSH 远程连接

    ssh远程连接 准备工作: 1 准备两台linux pc 我们一般用的是VMware虚礼软件 2 这两台linux可以互通 3 linux1 :192.168.2.2 这台为你要连接的服务器 linu ...

  9. 虚拟机VMware网络类型&&SSH远程连接Linux

    前言: Linux专题是16年11月开始写,说来惭愧,已经5个月没学Linux,至今感觉连入门还没达到.暑假实习有投运维开发岗位,无奈对Linux不熟悉,校招简历也被刷了.so, 我打算先花1个月内的 ...

随机推荐

  1. K8s常用命令操作

    K8s常用命令操作 一.kubectl命令补全 1.master安装命令补全,并临时生效 yum install -y bash-completion source /usr/share/bash-c ...

  2. Django中使用Bootstrap----带view.py视图函数(也就是项目下的脚本文件)

    一.Django中使用Bootstrap 1.首先建立工程,建立工程请参照:https://www.cnblogs.com/effortsing/p/10394511.html 2.在Firstdja ...

  3. 使用 ServiceStack.Text 序列化 json

    相信做 .net 开发的朋友经常会遇到 json 序列化这样的需要,今天发篇文章总结下自己使用 ServiceStack.Text 来序列化 json.它的速度比 Newtonsoft.Json 快很 ...

  4. todolist形式的搜索框,分开组件写的,点击上下键时,框内显示当前选中的内容

    ### 首先  安装react 脚手架 cnpm  install  create-react-app  -g      //只需要在电脑上安装一次就好了,以后不用再下载了 ### 创建项目 crea ...

  5. 斑马打印机和欧姆龙CP1H串口通信打印

    欧姆龙CP1HPLC和斑马打印机通信 1. PLC 1.1PLC型号 CP1H 1.2通信方式 232通信,使用232扩展卡槽CP1W-CIF01. CP1W-CIF01是RS232选件板,通信距离最 ...

  6. 白嫖百度 Tesla V100 笔记(在 AI Studio 上使用 tensorflow 和 pytorch 的方法)

    登陆百度 AI Studio 并按照教程创建新项目 启动项目并进入控制台 下载 Anaconda3/Miniconda3 安装脚本 安装在 ~/work/*conda3 目录 输入命令 source ...

  7. golang web框架 beego 学习 (六) request body和module的映射

    router.go package routers import ( "gowebProject/controllers" "github.com/astaxie/bee ...

  8. 关于opencv的几个小总结

    关于opencv的几个小总结 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 说明:opencv是一个非常好用的开源图像处理与计算机视觉支持库,但是在实际使用 ...

  9. utf8 unicode 编码互转

    static function utf8_to_unicode($c) { switch(strlen($c)) { case 1: return ord($c); case 2: $n = (ord ...

  10. Java check是否是日期类型

    boolean checkFormate(string parm){ Pattern pattern = Pattern.compile("([0-9]{4})(0[1-9]|1[0-2]) ...