您的位置:首页 > 其它

不改变登陆账号,切换身份访问共享

2018-03-26 13:49 330 查看

使用场景

做运维时需要在用户桌面环境下,访问运维部门共享,切换用户较麻烦

方便来回测试用户共享权限设置等

注意使用完后记得切回原来用户身份,以免该用户使用你的权限操作共享!

注意使用完后记得切回原来用户身份,以免该用户使用你的权限操作共享!

基本原理

1 . 使用
net use
断开已有的用户连接

net use * /del /y


2 . 使用新账号
net use
到共享

REM 不一定要使用ipc$,可以用已有的路径
net use \\share-server-ip\ipc$


3 . 调用explorer.exe打开共享

explorer \\share-server-ip


工具下载

以下为C#版工具源码和示意图,可自行定制修改

https://download.csdn.net/download/leoforbest/10308818



autoit脚本

#include<GUIConstantsEx.au3>

$server = "\\192.168.241.135"
$NETBIOS_NAME = "TEST\"

MainGUI()

Func MainGUI()
Local $button,$msg
GUICreate("共享切换工具",300,200)

$button = GUICtrlCreateButton("下一步",100,100,100)
$label = GUICtrlCreateLabel("请确保本程序是复制到本地电脑再运行!",50,30)
$label2 = GUICtrlCreateLabel("请关闭所有打开的" & $server &"窗口!",50,50)
$button1 = GUICtrlCreateButton("下一步",100,150,100)
$nameL = GUICtrlCreateLabel("帐号:",50,70)
$passL = GUICtrlCreateLabel("密码:",50,90)
$name = GUICtrlCreateInput("",90,70,120)
$pass = GUICtrlCreateInput("",90,90,120)
GUICtrlSetState($button1,$GUI_HIDE)
GUICtrlSetState($nameL,$GUI_HIDE)
GUICtrlSetState($passL,$GUI_HIDE)
GUICtrlSetState($name,$GUI_HIDE)
GUICtrlSetState($pass,$GUI_HIDE)

Dim  $keys[1][2] = [["{Enter}",$button]]
Dim  $keys2[1][2] = [["{Enter}",$button1]]
GUISetAccelerators($keys)

GUISetState()

While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
ExitLoop
Case $msg = $button
GUICtrlSetData($label,"正在断开与" & $server & "的连接!")
GUICtrlDelete($button)
GUICtrlDelete($label2)
ProcessWaitClose(RunWait("net use * /del /y","",@SW_HIDE),30)
GUICtrlSetData($label,"   请正确输入用户名和密码!")
GUICtrlSetState($button1,$GUI_SHOW)
GUICtrlSetState($nameL,$GUI_SHOW)
GUICtrlSetState($passL,$GUI_SHOW)
GUICtrlSetState($name,$GUI_SHOW)
GUICtrlSetState($pass,$GUI_SHOW)
GUISetAccelerators($keys2)

Case $msg = $button1
$nameget = GUICtrlRead($name)
$passget = GUICtrlRead($pass)
GUICtrlDelete($label)
GUICtrlDelete($button1)
GUICtrlDelete($nameL)
GUICtrlDelete($passL)
GUICtrlDelete($name)
GUICtrlDelete($pass)
GUICtrlCreateLabel("正在建立与" & $server & "的新的连接!",50,90)
ProcessWaitClose(Run("net use " & $server & "\ipc$ " & $passget & " /user:" & $NETBIOS_NAME & $nameget,"",@SW_HIDE),30)
If @extended = 0 Then
Run("explorer.exe " & $server)
Exit(1)
Else
MsgBox(0,"共享盘切换?","帐号密码错误,或者" & $server & "未完全关闭,请重试!")
Exit(0)
EndIf
EndSelect
WEnd
EndFunc


c#客户端

部分关键代码

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.tbServer.Text = server;
}

private static string server = @"192.168.241.135";
// 输入你的域NETBIOS名,非域环境为空
private static string domain = @"";
private delegate void setTextHandler(string text);
private delegate void enableLoginHandler();
private Thread th;
private Process p;

private void setText(string text)
{
if(this.lblLog.InvokeRequired)
{
setTextHandler handler = new setTextHandler(setText);
this.lblLog.Invoke(handler, text);
}
else
{
this.lblLog.Text = text;
}
}

private void enableLogin()
{
if (this.btnLogin.InvokeRequired)
{
enableLoginHandler handler = new enableLoginHandler(enableLogin);
this.btnLogin.Invoke(handler);
}
else
{
this.btnLogin.Enabled = true;
}
}

private void doMain()
{
p = new Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();

setText("正在断开与服务器旧的连接!");
p.StandardInput.WriteLine("net use * /del /y");
setText("正在使用新账号密码连接服务器!");

p.StandardInput.WriteLine(String.Format(@"net use \\{0}\ipc$ {1} /user:{2}{3}", server, tbPwd.Text, domain, tbAccount.Text));
p.StandardInput.WriteLine("exit");
string ret = p.StandardError.ReadToEnd();
setText("正在打开共享!");
if (ret.Contains("不允许一个用户使用一个以上用户名与服务器或共享资源的多重连接"))
{
setText("访问共享失败!");
MessageBox.Show("失败,请关闭所有打开的共享目录或文件!" + ret, "访问共享失败");
}
else if (ret == "")
{
setText("访问共享成功!");
Process.Start("explorer.exe", String.Format(@"\\{0}", server));
}
else
{
setText("访问共享失败!");
MessageBox.Show(ret, "访问共享失败");
}
p.WaitForExit();
enableLogin();
}

private void btnLogin_Click(object sender, EventArgs e)
{
lblLog.Text = "";
if (tbAccount.Text == "" || tbPwd.Text == "")
{
lblLog.Text = "用户名或密码不能为空!";
return;
}

btnLogin.Enabled = false;
th = new Thread(doMain);
th.Start();
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if(p != null)
p.Close();
}

private void tbAccount_TextChanged(object sender, EventArgs e)
{
lblLog.Text = "";
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: